【发布时间】:2013-07-15 09:41:54
【问题描述】:
我在学习图形并尝试使用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,但每次都看不到任何东西。
【问题讨论】:
标签: java swing jframe paintcomponent