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();
}
}以上代码运行后 会画出许多 图形,但是实际上就是一个,没有刷新而已。怎么让他能刷新啊~
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();
}
}以上代码运行后 会画出许多 图形,但是实际上就是一个,没有刷新而已。怎么让他能刷新啊~
解决方案 »
- WIN7和JDK1.5环境下获取系统时间错误
- 急!高手们谁能推荐一些Java中GUI方面的书或者是视频哈!
- 关于java编程思想
- 从showInputDialog对话框中取值到数据库中怎么取啊,谢谢
- Applet
- 所有的分问一问题:为什么我用jbuilder编的application程序,在提示符下输入javaw application时弹出一个错误框说“can not find the mai
- 怎么输出文件列表???/
- JAVA要学到什么程序 才能到软件公司去应聘?
- 用JAVA如何读写文件
- java 调用javah命令无法生成头文件吗
- 关于java i18n实现中文显示的问题
- 界面`代码有错误~~
加句
g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
意思也很清楚了吧?
记得结帖哦 :)
/**/ g.clearRect(0, 0, 500, 500);
Graphics2D g2d = (Graphics2D) g;
setBackground(Color.PINK);
g2d.setColor(Color.red);
g.drawRect(x1, y1, x2, y2);// 画矩形
}
加句
g2d.clearRect(0, 0, this.getWidth(), this.getHeight());----------------------
正解了
1,让你的类实现Runnable接口,重写它的run()方法,
public void run() {
while(true) {
repaint();//不停的重画
try {
Thread.sleep(100) ;//每画一次让线程睡眠100ms,也可以是其他的时间,自定义
}catch(Exception e) {
e.printStackTrace() ;
}
}
}
}2,另外写一个内部类,实现Runnable接口,重写它的run()方法,重写的方法是一样的当然你要在某个时候把线程启动,启动的方法是
对于后者:Thread t = new Thread(new repaintThread()) ;//repaintThread() 是那个内部类
t.start() ;
对于前者:在main方法里这样处理
Bell b = new Bell() ;
b.start() ;