动态随机抽奖程序的实现

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;


public class Demo implements ActionListener{

	/**
	 * @param args
	 */
	static JButton middleJB,westJB,southJB,northJB,eastJB;
	static JFrame jf;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		jf = new JFrame("麻将");
		jf.setSize(800,600);
		jf.setIconImage(new ImageIcon("east.gif").getImage());
		jf.setLayout(new BorderLayout());
		
//		String a[] = {"east","west","south","north","middle"};
		
//		for(int i=0;i<5;i++){
//			System.out.println(a[i]);
//			jf.add(BorderLayout.WEST,new JButton(new ImageIcon("west.jpg")));
//		}
		
		ImageIcon eastImg = new ImageIcon("east.gif");
		eastJB = new JButton(eastImg);
		jf.add(BorderLayout.EAST,eastJB);
		
		
		ImageIcon westImg = new ImageIcon("west.jpg");
		westJB = new JButton(westImg);
		jf.add(BorderLayout.WEST,westJB);
		//jf.add(BorderLayout.WEST,new JButton(new ImageIcon("west.jpg")));
	
		
		ImageIcon southImg = new ImageIcon("south.gif");
		southJB = new JButton(southImg);
		jf.add(BorderLayout.SOUTH,southJB);
		//jf.add(BorderLayout.SOUTH,new JButton(new ImageIcon("south.gif")));
		
		
		ImageIcon northImg = new ImageIcon("north.gif");
		northJB = new JButton(northImg);
		jf.add(BorderLayout.NORTH,northJB);
		//jf.add(BorderLayout.NORTH,new JButton(new ImageIcon("north.gif")));
		
		
		
		ImageIcon middleImg = new ImageIcon("middle.gif");
		middleJB = new JButton(middleImg);
		jf.add(BorderLayout.CENTER,middleJB);
		//jf.add(BorderLayout.CENTER,new JButton(new ImageIcon("middle.gif"))); 简化
		
		middleJB.addActionListener(new Demo());
		
		
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
		

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		Random r = new Random();
		int num=0;
		while(num<(r.nextInt(10)+1)*2){
			System.out.println(num);
			final int j = num % 4;
			Thread t1 = new Thread(new Runnable(){
				public void run() {
					switch(j){
					case 0:
						eastJB.setBackground(Color.yellow);
						westJB.setBackground(Color.gray);
						southJB.setBackground(Color.gray);
						northJB.setBackground(Color.gray);
						break;
					case 1:
						eastJB.setBackground(Color.gray);
						westJB.setBackground(Color.yellow);
						southJB.setBackground(Color.gray);
						northJB.setBackground(Color.gray);
						break;
					case 2:
						eastJB.setBackground(Color.gray);
						westJB.setBackground(Color.gray);
						southJB.setBackground(Color.yellow);
						northJB.setBackground(Color.gray);
						break;
					case 3:
						eastJB.setBackground(Color.gray);
						westJB.setBackground(Color.gray);
						southJB.setBackground(Color.gray);
						northJB.setBackground(Color.yellow);
						break;
					}
					jf.update(jf.getGraphics());
				}
			});
		
			t1.start();
			
			try {
				t1.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			num++;
		
		}
	}

}
原文地址:https://www.cnblogs.com/zpchcbd/p/11820862.html