下面的代码应该打印
在init()
内--在start()
内--在paint()
内。
但是它在paint()内打印了最后一部分
两次!这是为什么?
public class SampleApplet extends Applet {
String msg;
@Override
public void init(){
setBackground(Color.BLACK);
setForeground(Color.yellow);
msg = "Inside init()-- ";
}
@Override
public void start(){
msg += "Inside start()-- ";
}
@Override
public void paint(Graphics g){
msg += "Inside paint().";
g.drawString(msg, 10, 30);
}
}
最佳答案
引自:Paint() :
the paint() method will be called as many times as necessary. If you put another window over your GUI then the paint() method will be called. If you then minimize that window and make your GUI visible again then the paint() method will be called again. And so on.
So if you have something that is a problem if the paint() method is called more than once, you have done it wrong. Don't do it that way. The paint() method should only redraw its target from existing data, it should never have to do calculations to figure out what to paint.
关于java - 一个简单的小程序中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14427639/