gpt4 book ai didi

java - 为什么 paint 方法运行两次?

转载 作者:行者123 更新时间:2023-11-30 09:03:18 26 4
gpt4 key购买 nike

import java.applet.Applet;
import java.awt.Graphics;

public class MyApplet extends Applet {
public void paint(Graphics g) {
System.out.println("This the test.");
g.drawString("This the test.", 10, 20);
}
}

控制台的输出是:

This the test.
This the test.

最佳答案

在 GUI 编程中,paint() 方法将根据需要多次调用。如果您在 GUI 上放置另一个窗口,则会调用 paint() 方法。如果您随后最小化该窗口并使您的 GUI 再次可见,那么将再次调用 paint() 方法。等等。

因此,如果多次调用 paint() 方法会出现问题,那么您就做错了。不要那样做。 paint() 方法应该只根据现有数据重新绘制其目标,它永远不必进行计算来确定要绘制的内容。

关于java - 为什么 paint 方法运行两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25661008/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com