0
当我运行这段代码时PaintComponent永远不会被调用,因为“绘制”消息永远不会被打印,我不知道为什么?谁能帮忙?PaintComponent不会被JPanel调用
public class DisplayManager extends JPanel {
public static final int WIDTH = 700, HEIGHT = 900;
public Bottle bottle1 = new Bottle("res/bottleimage.png");
public Slider slider1 = new Slider();
public void initDisplay()
{
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(WIDTH, HEIGHT));
frame.add(panel);
frame.setVisible(true);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
bottle1.imageIcon.paintIcon(this, g, 50, 50);
System.out.println("painted");
}
}
如果你的'DisplayManager'应该是'JPanel',你必须将'DisplayManager'添加到'Frame'而不是一个新的'JPanel'。 'frame.add(this)'而不是'frame.add(面板)' –
感谢伙计这工作 – PDPIG
@PDPIG,这只是解决方案的一半。 – camickr