littledeer1974
Garbage Collector
CJSDN高级会员
发贴: 1517
积分: 122
|
于 2004-08-27 16:24
[code] import java.awt.Color; import java.awt.Container; import java.awt.GridLayout;
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel;
class question01 extends JFrame { question01() { this.getContentPane().add(new MyPanel());
}
public static void main(String args[]) { question01 f = new question01(); f.setSize(300, 300); f.show(); }
}
class MyPanel extends JPanel implements Runnable { JButton jb1, jb2, jb3, jb4;
JPanel panel1, panel2, panel3, panel4;
static int index = 0;
Container cont;
MyPanel() { // panel1 = new JPanel(); panel2 = new JPanel(); panel3 = new JPanel(); panel4 = new JPanel(); // jb1 = new JButton("1");
jb2 = new JButton("2");
jb3 = new JButton("3");
jb4 = new JButton("4"); //
panel1.add(jb1); panel2.add(jb2); panel3.add(jb3); panel4.add(jb4);
setLayout(new GridLayout(2, 2));
this.setNewColor();
this.add(panel1); this.add(panel2); this.add(panel3); this.add(panel4); Thread thread = new Thread(this); thread.start(); }
public void setNewColor() {
// panel1.setBackground(new Color((int) (Math.random() * 255), (int) (Math .random() * 255), (int) (Math.random() * 255))); panel2.setBackground(new Color((int) (Math.random() * 255), (int) (Math .random() * 255), (int) (Math.random() * 255)));
panel3.setBackground(new Color((int) (Math.random() * 255), (int) (Math .random() * 255), (int) (Math.random() * 255))); panel4.setBackground(new Color((int) (Math.random() * 255), (int) (Math .random() * 255), (int) (Math.random() * 255)));
}
public void run() {
while (true) { try { Thread.sleep(150); this.setNewColor(); System.out.print("background color is changed!\n"); } catch (Exception e) { e.printStackTrace(); } } }
}[/code]
不知道这个是不是你想要的效果
littledeer1974 edited on 2004-10-25 09:42
|