在程序启动时绘制图形; PaintComponent从未调用过

时间:2015-02-05 07:05:42

标签: java swing graphics paintcomponent

这看起来像是一个简单的问题,但不知怎的,我还没有能够谷歌答案。教程似乎从头开始浏览,我看不出他们的程序与我的程序有什么不同。我要做的就是创建一个JPanel并在程序启动时使用Graphics类在其上绘制内容。

我创建的程序的超简化版本也不起作用:

public class Thing 
{
    public static void main(String[] args) 
    {
        JFrame mainFrame = new JFrame("Test");
        mainFrame.setResizable(false);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        OtherThing panel = new OtherThing();

        mainFrame.getContentPane().add(panel);
        mainFrame.pack();
        mainFrame.setVisible(true);
    }
}

public class OtherThing extends JPanel
{
    public OtherThing()
    {
        setBackground(Color.black);
        setPreferredSize(new Dimension(400,400));
        repaint();
    }
    public void PaintComponent(Graphics g)
    {
        super.paintComponents(g);
        setBackground(Color.red);
        setForeground(Color.red);
        System.out.println("start");
        g.drawOval(0,0,50,50);
        g.drawLine(0,0 , 100, 100);
        g.drawString("This is my custom Panel!",10,20);
        System.out.println("After");

    }
}

Sytem.out.println永远不会打印出来。永远不会调用PaintComponent。在我看过的一些教程中,他们使声音听起来就像重绘调用paintcomponent一样简单,但在我的程序中,paintcomponent从不被调用。

我只想在启动时绘制图形。

1 个答案:

答案 0 :(得分:4)

Java是关键敏感的。

public void PaintComponent(Graphics g)

必须是

public void paintComponent(Graphics g)
相关问题