1
我正在學習圖形並嘗試使用PaintComponent
繪製一些形狀,以下是代碼。我正在嘗試一個小時,但仍然無法正常工作。這個簡單問題的解決方案是什麼?PaintComponent不適用於繪製形狀
public class MyPainting extends JPanel
{
public void PaintComponent (Graphics g)
{
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(100, 100, 10, 20);
}
public static void main (String [] args)
{
MyPainting p = new MyPainting();
JFrame f= new JFrame();
f.setSize(300,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.setVisible(true);
}
}
當我運行程序有空JFrame
,我也嘗試g.drawString, ImageIcon
但每次什麼是可見的。
OMG ,,我這麼寬鬆。爲此浪費了1個小時。 –
爲什麼編譯器沒有給這個錯誤? –
,因爲你didnt添加@Override註釋 – Reimeus