为什么在paint方法里会执行2次?

rao3324180 2014-04-02 05:37:05
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Snake {

public static void main(String[] args) {
new Snasm().make();
}
}

class Snasm extends Frame{

public void make(){
this.setLocation(200,200);
this.setSize(200,200);
this.addWindowListener(new WindowAdapter(){

@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}

});
this.setVisible(true);
}

public void paint(Graphics g){
System.out.println("sd");
}
}

为什么paint里面会打印2个"sd"
...全文
288 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
只打印一个sd,估计是你对窗口进行操作了,然后就继续打印了。
Inhibitory 2014-04-03
  • 打赏
  • 举报
回复
paint的执行次数是你没法控制的。
u011050058 2014-04-03
  • 打赏
  • 举报
回复
this.setLocation(200,200);方法调用了Component里的setLocation(200,200),而这个方法一次进入回调用repaintParentIfNeeded()方法, private void repaintParentIfNeeded(int oldX, int oldY, int oldWidth,int oldHeight) { if (parent != null && peer instanceof LightweightPeer && isShowing()) { // Have the parent redraw the area this component occupied. parent.repaint(oldX, oldY, oldWidth, oldHeight); // Have the parent redraw the area this component *now* occupies. repaint(); } } 这里重新打印了,repaint
  • 打赏
  • 举报
回复
swing表示不清楚
stevenice85 2014-04-03
  • 打赏
  • 举报
回复
setsize的时候调用了paint吧

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧