[英]Run a method only once in paint () (Java)
Im trying to run a method only once when I call it in the paint ()
method, but when I call it normally it just runs over and over. 我试图在
paint ()
方法中调用一个方法时只运行一次 ,但是当我正常调用它时,它只会反复运行。 Example: 例:
private void someMethod ()
{
System.out.println ("Success");
}
@Override
public void paintComponent (Graphics g)
{
someMethod ();
repaint ();
}
This will output "Success" forever, I only can output it once. 这将永远输出“成功”,我只能输出一次。
I think you should write the same bridge 我想你应该写同样的桥
Try this: 尝试这个:
public class someClass{
private boolean hasDone = false;
public void paintComponent(Graphics g){
super.paintComponent(g);
if(!hasDone){
someMethod();
hasDone = true;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.