Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对paintComponent的强制调用_Java_User Interface_Awt - Fatal编程技术网

Java 对paintComponent的强制调用

Java 对paintComponent的强制调用,java,user-interface,awt,Java,User Interface,Awt,我意识到这段代码看起来毫无意义,我只是去掉了不相关的东西来显示结构 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 paintC

我意识到这段代码看起来毫无意义,我只是去掉了不相关的东西来显示结构

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
  }  
}

repaint()
不会导致调用
paintComponent
(否则您会看到stdout),因此如何强制每次调用
paintComponent
,都调用
Drawer

调用
repaint
在该组件成为显示层次结构的一部分之前都没有效果。当构造函数仍在执行时,它不可能是层次结构的一部分


一个选项是在显示层次结构的根目录中添加一个,然后在那里重新绘制。但是,我还不清楚您想要实现什么,这可能不是最好的方法。

调用
重新绘制
在组件成为显示层次结构的一部分之前不会有任何效果。当构造函数仍在执行时,它不可能是层次结构的一部分


一个选项是在显示层次结构的根目录中添加一个,然后在那里重新绘制。但是,我还不清楚您要实现什么,这可能不是最好的方法。

int[]
存储为类属性。移动
//计算将
绘制到
paintComponent(Graphics)
中的内容将
int[]
存储为类属性。移动
//计算在
paintComponent(Graphics)中绘制什么

对不起,忘了说它是如何实现的。我已经回答了这个问题。@ACarter-好吧,我关于在构造函数中调用
repaint
(很抱歉调用它
redraw
)的观点仍然成立。为什么不在调用
myGUI.con.repaint()
(或
myGUI.con.repaint()
)之后立即调用
d.repaint()
(或
myGUI.con.repaint()
)和
myGUI.repaint
,但都没有成功:(抱歉,忘了说它是如何实现的。我已经回答了这个问题。@ACarter-好吧,我关于在构造函数中调用
repaint
(抱歉调用
redraw
)的观点仍然成立。为什么不在调用
myGUI.con.add(d)之后立即调用
d.repaint()
(或者
myGUI.con.repaint()
?刚刚尝试了
d.repaint()
myGUI.con.repaint()
、和
myGUI.repaint
,但都没有成功:(若要更快地获得更好的帮助,请发布一条。@AndrewThompson我不能同时使其兼容和简短,所以我选择了简短。只需再多几行代码就可以使其成为SSCCE。'short'可以轻松地生成100行代码,而大多数人不会抱怨。若要更快地获得更好的帮助,请发布一条。@AndrewThompson我不能真正使其兼容和简短。)同时也很短,所以我选择了短代码。只需要再多几行代码就可以成为SSCCE。“短代码”可以很容易地包含100行代码,而大多数人都不会抱怨。
Drawer d = new Drawer(data);
myGUI.con.add(d); //myGUI.con is a previously set up container