【发布时间】:2016-11-18 20:49:45
【问题描述】:
当我运行此代码时,永远不会调用 PaintComponent,因为永远不会打印“绘制”消息,我不知道为什么?谁能帮忙?
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(panel) -
谢谢老兄这工作
-
@PDPIG,这只是解决方案的一半。
标签: java swing paintcomponent imageicon