我有以下簡單的代碼在一個JFrame構造的java的JFrame圖形
super(name);
setBounds(0,0,1100,750);
setLayout(null);
setVisible(true);
g = this.getGraphics();
int[] x =new int[]{65, 122, 77, 20, };
int[] y =new int[]{226, 258, 341, 310};
g.setColor(Color.RED);
g.drawPolygon (x, y, x.length);
System.out.println(g);
我得到控制檯的輸出爲:
sun.java2d.SunGraphics2D [字體= java.awt.Font中[family = Dialog,name = Dialog,style = plain,size = 12],color = java.awt.Color [r = 255,g = 0,b = 0]]
但是沒有繪製紅色的多邊形在JFrame上,但只是空白的JFrame。
爲什麼?
這不是畫在JFrame的正確方法。您應該繼承JPanel並重寫'paintComponent(Graphics)',並在通過那裏的圖形對象上繪畫。 – Vulcan