所以我想在Eclipse中做一個項目,我的Eclipse沒有顯示任何東西。 我的意思是,代碼很好,但它只是打開一個窗口,它不顯示任何內容,當它應該顯示一行。Java圖形不顯示任何東西
FirstGraph.java
import java.awt.Graphics;
import javax.swing.JFrame;
public class FirstGraph extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
JFrame f = new JFrame();
f.setName("First Graph");
f.setSize(300, 300);
f.setVisible(true);
f.setBounds(30, 30, 300, 300);
f.getContentPane().add(new MyCanvas());
f.setVisible(true);
}
public void Paint(Graphics g) {
g.drawLine(50, 50, 250, 280);
}
}
MyCanvas.java
import java.awt.Graphics;
import javax.swing.JComponent;
public class MyCanvas extends JComponent{
private static final long serialVersionUID = -2508263540396372610L;
public void Paint(Graphics g){
int[] x;
x = new int[]{40, 125, 256, 290};
int[] y;
y = new int[]{50, 115, 279, 295};
g.drawPolygon(x, y, x.length);
}
}
問題是什麼?
更改'public void Paint(Graphics g){'to'@Override public void Paint(Graphics g){'.. – 2014-10-09 12:29:29
「而不是'paint'使用'paintComponent'方法,使用'@ Override'註解。 – alex2410 2014-10-09 12:30:18
'f.setBounds(30,30,300,300);'最好讓'MyCanvas'返回一個合理的首選大小和'pack()'框架。此外,不要嘗試覆蓋框架中的任何繪畫.. – 2014-10-09 12:30:39