java随机点名_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();

}

}

}

}

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值