java - 一个简单的小程序中的错误

标签 java applet awt paint

下面的代码应该打印

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/

相关文章:

java - 如何在具有安全性的 Spring boot Rest Controller 中使用 "/images"路径?

java - 使用 g.drawString 显示新行

mysql - Java Applet需要读取MySQL

java - 如何使图像水平反弹?

java - jbutton在不同电脑上的文本颜色问题

java - 从字符串中删除重音

java - 有关即将推出的 fork-join 框架的资源

java - 在 HTML 中使用 Java applet 方法

Java Graphics SetColor 给出 NullPointerException

java - 在java中绘制一个三角形