一个简单的关于线程的问题(在线等) 使用线程 ,将一个panel 的背景每200ms改变一次 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不需要线程,使用TimerTask就行了// BKColorPanel.javaimport java.awt.*;import java.util.*;import javax.swing.*;public class BKColorPanel extends JPanel { // 使用TimerTask改变颜色 class BKChangeTask extends TimerTask{ JComponent target; BKChangeTask(JComponent target){ this.target = target; } public void run(){ // 生成随机颜色 float r = (float)Math.random(); float g = (float)Math.random(); float b = (float)Math.random(); // 改变背景颜色 target.setBackground(new Color(r,g,b)); } } public BKColorPanel(){ // 创建timer java.util.Timer timer = new java.util.Timer(); timer.schedule(new BKChangeTask(this), 200, 200); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container container = frame.getContentPane(); container.add(new BKColorPanel(), BorderLayout.CENTER); frame.setPreferredSize(new Dimension(300, 200)); frame.pack(); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int x = (screen.width - frame.getWidth()) / 2; int y = (screen.height - frame.getHeight()) / 2; frame.setLocation(x, y); frame.setVisible(true); }} 楼上的说得对阿,用Timer就行了 换成下面代码就行了。 class BKChangeTask extends Thread{ public void run(){ while (true) { // 生成随机颜色 float r = (float)Math.random(); float g = (float)Math.random(); float b = (float)Math.random(); // 改变背景颜色 setBackground(new Color(r,g,b)); try { sleep(200); } catch (Exception e) { } } } } public BKColorPanel1(){ Thread t = new BKChangeTask(); t.start(); } 各位大牛,请教一下,我想做一个模拟商场 有关java连接数据库 基于JMF的视频聊天软件 请各路大大进来一下,请问怎么用SWING实现滑动 请教关于JComBox和JTable的两个问题 jacob如何转换word文件为html?帮忙看看下面的代码 中文处理问题 一个简单的问题 怎样在java程序中显示中文字体,比如在窗口上显示楷体、隶书等字体? 如何配置才能在tomcat下使用javabena,help,在线等待! 求<java核心技术>电子书下载地址 请教一个二维数组的两种写法,占内存空间大小的问题!~~~
// BKColorPanel.javaimport java.awt.*;
import java.util.*;
import javax.swing.*;public class BKColorPanel extends JPanel {
// 使用TimerTask改变颜色
class BKChangeTask extends TimerTask{
JComponent target; BKChangeTask(JComponent target){
this.target = target;
}
public void run(){
// 生成随机颜色
float r = (float)Math.random();
float g = (float)Math.random();
float b = (float)Math.random(); // 改变背景颜色
target.setBackground(new Color(r,g,b));
}
} public BKColorPanel(){
// 创建timer
java.util.Timer timer = new java.util.Timer();
timer.schedule(new BKChangeTask(this), 200, 200);
} public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = frame.getContentPane(); container.add(new BKColorPanel(), BorderLayout.CENTER); frame.setPreferredSize(new Dimension(300, 200)); frame.pack(); Dimension screen =
Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width - frame.getWidth()) / 2;
int y = (screen.height - frame.getHeight()) / 2; frame.setLocation(x, y); frame.setVisible(true);
}
}
public void run(){
while (true) {
// 生成随机颜色
float r = (float)Math.random();
float g = (float)Math.random();
float b = (float)Math.random(); // 改变背景颜色
setBackground(new Color(r,g,b)); try {
sleep(200);
} catch (Exception e) {
}
}
}
} public BKColorPanel1(){
Thread t = new BKChangeTask();
t.start();
}