package CrescentMoonPackage;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ChouJiang extends JFrame{
JLabel[] shuzi=new JLabel[6];
ShowShuZi[] show=new ShowShuZi[6];
JButton btnStart=new JButton("开始");
JButton btnEnd=new JButton("结束");
Container c;
private void init(){
c=this.getContentPane();
c.setLayout(new FlowLayout());
for(int i=0;i<6;i++){
shuzi[i]=new JLabel("0");
shuzi[i].setFont(new Font("黑体",Font.BOLD,30));
shuzi[i].setForeground(new Color(225,0,0));
c.add(shuzi[i]);
}
c.add(btnStart);
c.add(btnEnd);
btnStart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
for(int i=0;i<6;i++){
show[i]=new ShowShuZi(shuzi[i]);
show[i].start();
}
}
});
btnEnd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
for(int i=0;i<6;i++){
show[i].setFlag(false);
}
}
});
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400,300);
setVisible(true);
}
public ChouJiang(){
init();
}
public static void main(String args[]){
ChouJiang cj=new ChouJiang();
}
}
class ShowShuZi extends Thread{
JLabel shuzi;
boolean flag=true;
public void setFlag(boolean flag){
this.flag=flag;
}
public ShowShuZi(JLabel shuzi){
this.shuzi=shuzi;
}
public void run(){
while(flag){
int i=(int)(Math.random()*10);
shuzi.setText(String.valueOf(i));
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}