多线程的简单应用----彩票摇号器

效果图:

 

package game;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class YaoHao extends JFrame {

	Container c;
	JMenuBar mb;
	JMenu JFile,JHelp;
	JMenuItem mExit,mCopyright;
	JPanel pl;
	JLabel num[]=new JLabel[6];
	JButton begin,end;
	boolean flag=true;
	
	private void init(){
		c=this.getContentPane();
		c.setLayout(new BorderLayout()); 
		
		mb=new JMenuBar();
		JFile=new JMenu("文件(F)");
		JHelp=new JMenu("帮助(H)");
		mb.add(JFile);mb.add(JHelp);
		mExit=new JMenuItem("退出(E)");
		mCopyright=new JMenuItem("版权信息");
		JFile.add(mExit);JHelp.add(mCopyright);
		c.add(mb,BorderLayout.NORTH);
		
		 mCopyright.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					JOptionPane.showMessageDialog(null, "本游戏由暗伤无痕出品", "版权声明", JOptionPane.INFORMATION_MESSAGE);
				}
			});
		 mExit.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					YaoHao.this.dispose();
				}
			});
		
		 
		 pl=new JPanel();pl.setLayout(null);
		 c.add(pl);
         for(int i=0;i<6;i++){
        	 num[i]=new JLabel("0");
        	 pl.add(num[i]);
        	 num[i].setBounds((i+2)*25,30,60,30);
         }
         begin=new JButton("开始");
         end=new JButton("结束");
         pl.add(begin);pl.add(end);
         begin.setBounds(220,30,60,30);
         end.setBounds(290,30,60,30);
         begin.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					flag=true;
					Numble n=new Numble();
						n.start();
						
					
				}
			});
         
         end.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					Numble n=new Numble();
						flag=false;
					
				}
			});
         
		this.setSize(400,300);
		this.setVisible(true);
	}
	public YaoHao(String title){
		super(title);
		init();
	}
	class Numble extends Thread{
		Random random=new Random();
		public void run(){
			while(flag){
			for(int i=0;i<6;i++){
				num[i].setText(Integer.toString(random.nextInt(10)));
			}}
			try {
				sleep(10000);
				} catch (InterruptedException e) {
				e.printStackTrace();
				}
		}
		
	}
	public static void main(String[] args) {
		YaoHao yh=new YaoHao("彩票摇号器");

	}

}


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2013-11-05 18:35  dingxiaoyue  阅读(303)  评论(0编辑  收藏  举报