java---随机点名

package com.day01;

import java.awt.Font;
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;

public class CallName extends JFrame implements Runnable{
	
	private  JLabel JName;//设置名字到标签
	private JButton start;//启动线程
	private JButton stop;//停止线程
	public static String[] names={"许锦迪","白小龙","赵帅","王启明","冯赟","曹正明","杜光明","王金龙",
			"李法勇","崔超波","何仁梁","朱东洋","韩高峰","杨蒙蒙","孙翠翠","李世杰","吴超","芦肖杨","蒲文涛","朱壮志","张孟晖","吴创创",
			"韩朋欢","顾豪","刘睿","曲良芯","董振坤","舒攀科","闫平平","徐孟博","高运来","王家宝","李晓旭","郭政良","常军凯","高明",
			"贾旺旺","马儒博","周淼","宋梦雪","周旭峰","赵鑫","胡生晓","徐豪","王玉皎","张梦雪"};
	public static boolean flag=true;	
	private static Thread thread;
	private static CallName call;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 call=new CallName();
		 thread=new Thread(call);
	
	}
	
	/**
	 * 构造方法
	 */
	public CallName() {
		//初始化标签
		this.setLayout(null);
		//设置显示第一次的姓名
		Random num=new Random();
		int index=num.nextInt(names.length);
		JName=new JLabel(names[index]);
		
		JName.setSize(100, 40);
		JName.setFont(new Font("微软雅黑",Font.BOLD,30));
		JName.setLocation(95,110);
		
		start=new JButton("start");
		start.setFont(new Font("微软雅黑",Font.BOLD,16));
		start.setSize(80, 30);
		start.setLocation(40, 20);
		start.setFocusable(false);
		start.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
								
				flag=true;
				if (flag) {
					thread.start();	
				}
			
			}
		});
		
		stop=new JButton("stop");
		stop.setFont(new Font("微软雅黑",Font.BOLD,16));
		stop.setSize(80, 30);
		stop.setFocusable(false);
		stop.setLocation(180, 20);
		stop.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				flag=false;
				
			}
		});
		
		this.add(JName);
		this.add(start);
		this.add(stop);
		this.setTitle("点名器");
		this.setSize(300, 250);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			if(flag){
			try {
				Thread.sleep(60);
				Random num=new Random();
				int index=num.nextInt(names.length);
				JName.setText(names[index]);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		 }
		}
	
	}


	
}

  

转载于:https://www.cnblogs.com/qurui1997/p/10501729.html

你可能感兴趣的:(java---随机点名)