java实现的抽奖的小程序熟悉一下线程

package connection.thread.lottery;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Lottery extends Thread{
	
	private JLabel label1,label2,label3;
	private JButton b1= new JButton("开始抽奖");
	// 显示抽奖人
	private JPanel jpanel1 = new JPanel();
	// 显示奖品
	private JPanel jpanel2 = new JPanel();
	// 抽奖过程的操作者
	private static Participator p = Participator.getParticipator();
	
	private Random randoms;
	// 名字随机
	private static int random1 ;
	// 奖品随机
	private static int random2 ;
	private Dimension dimension = new Dimension(80,50);
	private Start start = null;
	private int clickCount = 0;

	public void init(){
		randoms = new Random();
		label1 = new JLabel("",JLabel.LEFT);
		label2 = new JLabel("",JLabel.RIGHT);
		label3 = new JLabel("奖品剩余"+p.prizeList.size()+"个",JLabel.CENTER);
		label1.setPreferredSize(dimension);
		label1.setMinimumSize(dimension);
		label2.setPreferredSize(dimension);
		label2.setMinimumSize(dimension);
		label3.setPreferredSize(dimension);
		label3.setMinimumSize(dimension);
		jpanel2.add(label1);
		jpanel2.add(label2);
		jpanel2.add(label3);
		jpanel2.setSize(100, 100);
		jpanel1.add(b1);
		b1.addActionListener(new StartL());
	}

	public void go(){
		while(true){
			try {
				sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if(p.runFlag) {
					if(p.prizeList.size() > 0){
						synchronized(this){
							random1 = randoms.nextInt(p.nameList.size());
							label1.setText(p.nameList.get(random1));
							random2 = randoms.nextInt(p.prizeList.size());
							label2.setText(p.prizeList.get(random2));
						}
					}
			}
		}
	}
	/**
	 * 抽奖按钮添加开始抽奖 和停止抽奖事件
	 * @author hanyd
	 *
	 */
	class StartL implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			
			if(clickCount == 0){
				// 第一次按下开始按钮时 线程启动
				if(p.runFlag){
					if (start == null)
						// Lottery.this是内部类里边调用外部类实例的一种方式
						start = new Start(Lottery.this);
				}
			} else {
				// 停止抽奖按钮按下时
				if(start != null){
					// 设置抽奖开始与结束 并删除上次的中奖信息
					p.removeOne(random1,random2);
					label3.setText("奖品剩余"+p.prizeList.size()+"个");
				}

				// 显示按钮连续点击的不同文本 和奖品的剩余信息
				if(clickCount > 0 && clickCount%2==0){
					b1.setText("停止抽奖");
					label3.setText("奖品剩余"+p.prizeList.size()+"个");
				} else {
					b1.setText("开始抽奖");
					label3.setText("奖品剩余"+(p.prizeList.size()-1)+"个");
					// 当奖品全部被抽走时 抽奖按钮设置为disable
					if(p.prizeList.size() - 1 == 0){
						b1.setText("抽奖结束");
						b1.setEnabled(false);
					}
				}
			}
			clickCount++;
		}
	}

	public static void main(String[] args) {
			Lottery lottery = new Lottery();
			JFrame frame = new JFrame();
			frame.setLayout(new BorderLayout());
			frame.setSize(400,300);
			lottery.init();
			frame.add(lottery.jpanel2,BorderLayout.NORTH);
			frame.add(lottery.jpanel1,BorderLayout.CENTER);
			frame.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e){
					System.exit(0);
				}
			});
			frame.setVisible(true);
	}
}
/**
 * 抽奖按钮按下 线程启动
 * @author hanyd
 *
 */
class Start extends Thread{
	private Lottery lottery = null;
	public Start(Lottery lottery){
		this.lottery = lottery;
		start();
	}
	public void run(){
		lottery.go();
	}
}

 

package connection.thread.lottery;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Participator {
	
	private static Participator participator = null;
	// 开始抽奖和停止抽奖的判断
	public boolean runFlag = true;
	private String [] participatorName = {"Ton","Jory","Anry","Helen","Jim","Aric","张三","李四"};
	private String [] prize = {"特等奖","一等奖","二等奖","三等奖","抚慰奖"};
	// 抽奖者
	public List nameList = new ArrayList();
	// 奖品
	public List prizeList = new ArrayList();
	/**
	 * 在此构造函数中设置抽奖者和奖品的列表
	 * 
	 */
	private Participator(){
		// 注意Arrays.asList(Array)返回的List 并不是我们ArrayList
		// 它返回的List是不可以进行insert,remove,delete的 ,这是java的设计者在设计Arrays的时候
		// 处于其他原因大的考虑造成的,还有对应的一个容器的操作Collections类也是如此
		// 所以我们要转化一下,不然就会发生UnsupportedOperationException
		nameList = new ArrayList(Arrays.asList(participatorName));
		prizeList = new ArrayList(Arrays.asList(prize));
	}

	/**
	 * 单例在这有点大才小用了
	 * 
	 * @return participator
	 */
	public static Participator getParticipator(){
		if(participator == null){
			participator = new Participator();
		}
		return participator;
	}

	/**
	 * 开始抽奖和停止抽奖的一个判断
	 * runFlag = true 的话抽奖开始
	 * runFlag = false 的话抽奖结束
	 */
	private void invertFlag(){
		runFlag = !runFlag;
		
	}
	
	/**
	 * 对已经获奖了的人 和奖品进行移除
	 * 
	 * @param num1 获奖人的下标
	 * @param num2 奖品的下标
	 */
	public void removeOne(int num1,int num2){
		// 对已经获奖了的人 和奖品进行移除
		if(!runFlag){
			synchronized(this){
				removeOne(num1);
				removePrize(num2);
			}
		}

		// 开始抽奖和停止抽奖的设置
		invertFlag();
	}
	/**
	 * 移除已经获奖人的名字
	 * @param num 获奖人的下标
	 */
	private void removeOne(int num){
		nameList.remove(num);
	}
	
	/**
	 * 移除已经被抽走的奖品
	 * @param num 奖品的下标
	 */
	private void removePrize(int num){
		prizeList.remove(num);
	}
}


 

你可能感兴趣的:(java,线程,单例)