一个关于线程的简单问题(在线等)

scarletg 2006-06-30 08:48:28
使用线程 ,将一个panel 的背景每200ms改变一次
...全文
163 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nanazhu 2006-07-01
  • 打赏
  • 举报
回复
package exercise;
import java.awt.*;
import javax.swing.*;

public class threadexercise extends JFrame{
public threadexercise()
{
}

public static void main(String args[])
{
threadexercise tf = new threadexercise();
tf.setSize(200, 300);
tf.setVisible(true);
int i = 0;
try
{
while(true)
{

Color color = new Color(i);
tf.getContentPane().setBackground(color);
Thread.sleep(1500);
i += 50;
}
}
catch(InterruptedException e)
{
System.out.println("123");
}
}
}
success_dream 2006-07-01
  • 打赏
  • 举报
回复
大家都写的不错啊!楼主用上面的几个程序试下啊!
amandag 2006-06-30
  • 打赏
  • 举报
回复
import java.awt.*;
import javax.swing.*;

public class Test extends JFrame{
public Test() {
this.getContentPane().add(new MyPanel());
this.setSize(200, 300);
this.show();
}

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

class MyPanel extends JPanel implements Runnable{
Thread thread;

public MyPanel(){
new Thread(this).start();
}

private static final Color[] colors = {
Color.BLACK, Color.BLUE, Color.CYAN,
Color.DARK_GRAY, Color.GRAY, Color.GREEN,
Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
Color.PINK, Color.RED, Color.WHITE,Color.YELLOW
};
public void paint(Graphics g) {
super.paint(g);
int i = (int) Math.round((Math.random() * 12));
this.setBackground(colors[i]);

}

public void run(){
while(true){
try{
repaint();
Thread.sleep(200);
}
catch(InterruptedException e){
System.out.println(e.getMessage());
}
}
}
}
rianting 2006-06-30
  • 打赏
  • 举报
回复
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Container;
import java.awt.Color;

class Bg extends JFrame implements Runnable
{
private JPanel pan = new JPanel();
public Bg()
{
this.setTitle("demo");
this.setSize(500,300);
}
public void run()
{
try
{
for(int i = 0; i < 100; i ++)
{
if(i % 2== 0)
{
pan.setBackground(Color.red);
Container con = this.getContentPane();
con.add(pan);
}
else
{
pan.setBackground(Color.blue);
Container con = this.getContentPane();
con.add(pan);
}
Thread.sleep(2000);
}

}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}

public class demo
{
public static void main(String[] args)
{
Bg bb = new Bg();
Thread th = new Thread(bb);
th.start();
bb.show(true);

}
}
amandag 2006-06-30
  • 打赏
  • 举报
回复
up
yyjzsl 2006-06-30
  • 打赏
  • 举报
回复
关注,顶下
mac1982129 2006-06-30
  • 打赏
  • 举报
回复
用不着线程吧
hoszone 2006-06-30
  • 打赏
  • 举报
回复
用一个timer

62,615

社区成员

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

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