import java.awt.*;
import java.awt.event.*;
/*import java.awt.event.ActionListener;
*/
import javax.swing.*;
/*import javax.swing.JFrame;
import javax.swing.JLabel;
*/
public class CaiPiao extends JFrame{
JLabel[] lblNum=new JLabel[6];
JButton btnStart=new JButton("开始");
JButton btnEnd=new JButton("停止");
ShowNum[] th=null;
public static boolean flag=true;
private void init(){
setLayout(new FlowLayout());
for(int i=0;i<6;i++){
lblNum[i]=new JLabel();
add(lblNum[i]);
lblNum[i].setText("0");
}
add(btnStart);add(btnEnd);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
btnStart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
th=new ShowNum[6];
for(int i=0;i<6;i++){
th[i]=new ShowNum(lblNum[i]);
//th[i].SetFlag(true);
CaiPiao.flag=true;
th[i].start();
}
}
});
btnEnd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
for(int i=0;i<6;i++){
//th[i].SetFlag(false);
CaiPiao.flag=false;
}
}
});
}
public CaiPiao(){
super("体育彩票");
init();
this.setSize(450,500);
this.setVisible(true);
}
public static void main(String args[]){
CaiPiao cp=new CaiPiao();
}
class ShowNum extends Thread{
private JLabel lblNum=null;
boolean flag=true;
public ShowNum(JLabel lblNum){
this.lblNum=lblNum;
}
public void SetFlag(boolean flag){
this.flag=flag;
}
public void run(){
while(CaiPiao.flag){
int a=(int)(Math.random()*10);
lblNum.setText(String.valueOf(a));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
希望可以帮到你!