我意识到这段代码看起来毫无意义,我只是去掉了不相关的东西以显示结构
class Drawer extends JComponent {
public Drawer(int[] data) {
System.out.println("drawer");
for(int x = 0; x < data.length; x++){}
//work out what to draw
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("drawerpC"); //check to see if it is called
//draw stuff
}
}
在单独的文件中,定期调用 Drawer
的新实例。每次调用时,data
都不一样,所以每次调用Drawer
时,都需要调用paintComponent
。
我在另一个文件中有这段代码:
Drawer d = new Drawer(data);
myGUI.con.add(d); //myGUI.con is a previously set up container
repaint()
不会导致 paintComponent
被调用(否则你会看到标准输出),那么我如何强制 paintComponent
每次调用 Drawer
时调用?
最佳答案
将 int[]
存储为类属性。将 //work out what to draw
移动到 paintComponent(Graphics)
中。
关于java - 强制调用 paintComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12883955/