Java 在程序开始时绘制图形;PaintComponent从未调用过
这似乎是一个很简单的问题,但不知何故,我一直无法用谷歌搜索答案。教程似乎略过了开头,我看不出他们的程序和我的有什么不同。我所要做的就是在程序启动时创建一个JPanel并使用Graphics类在其上绘制东西 我创建了一个超级简化版本的程序,但它也不起作用:Java 在程序开始时绘制图形;PaintComponent从未调用过,java,swing,graphics,paintcomponent,Java,Swing,Graphics,Paintcomponent,这似乎是一个很简单的问题,但不知何故,我一直无法用谷歌搜索答案。教程似乎略过了开头,我看不出他们的程序和我的有什么不同。我所要做的就是在程序启动时创建一个JPanel并使用Graphics类在其上绘制东西 我创建了一个超级简化版本的程序,但它也不起作用: public class Thing { public static void main(String[] args) { JFrame mainFrame = new JFrame("Test");
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");
}
}
系统输出。打印项从未打印出来。从不调用PaintComponent。在我看过的一些教程中,它们听起来就像重新绘制调用paintcomponent一样简单,但在我的程序中,paintcomponent从未被调用
我只想在启动时绘制图形。Java对键敏感。
public void PaintComponent(Graphics g)
一定是
public void paintComponent(Graphics g)
Java是键敏感的。
public void PaintComponent(Graphics g)
一定是
public void paintComponent(Graphics g)
将
public void PaintComponent(Graphics g)
更改为@Override public void PaintComponent(Graphics g)
,以获取有用的编译器消息..我很困惑。。。它告诉我没有什么可以忽略的?JPanel没有PaintComponent?请将公共无效PaintComponent(图形g)
更改为@覆盖公共无效PaintComponent(图形g)
,以获取有用的编译器消息..我很困惑。。。它告诉我没有什么可以忽略的?JPanel没有PaintComponent?,不需要是公共的
,也不需要是公共的