swing 刷新问题

luffyday luffyday 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
package scf.luffy;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Bell extends JFrame {

private int x1 = 100;
private int y1 = 100;
private int x2 = 100;
private int y2 = 100;

private static final long serialVersionUID = -8672941220924959402L;

Bell() {
setLayout(new GridLayout(1,1));
setSize(500, 500);
setLocation(100, 100);
setResizable(false);
setVisible(true);
Thread t = new GoGoGo();
SwingUtilities.updateComponentTreeUI(this);
t.start();

}

public void paint(Graphics g) {
/**/


Graphics2D g2d = (Graphics2D) g;
setBackground(Color.PINK);
g2d.setColor(Color.red);


g.drawRect(x1, y1, x2, y2);// 画矩形


}

class GoGoGo extends Thread {
public void run() {
for (int i = 0; i < 10; i++) {
x1 += 10;
x2 += 10;
y1 += 10;
y2 += 10;
System.out.println(x1);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}

repaint();
validate();
}
}
}

public static void main(String[] args) {
new Bell();
}
}

以上代码运行后 会画出许多 图形,但是实际上就是一个,没有刷新而已。

怎么让他能刷新啊~

...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
luffyday
Java SE 62,620 成员 307,251 内容