博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
点名器
阅读量:5902 次
发布时间:2019-06-19

本文共 3361 字,大约阅读时间需要 11 分钟。

package com;

import java.awt.Color;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class MainFrame extends JFrame {

private List<String> names = null;// 8
private JLabel labShowName = null;
private JButton jbnStartEnd = null;

public MainFrame() {

this.setSize(220, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(2, 1));
labShowName = new JLabel("天地");
labShowName.setFont(new Font(null, 0, 20));
labShowName.setForeground(Color.blue);
jbnStartEnd = new JButton("开始");
this.add(labShowName);
this.add(jbnStartEnd);
names = new ArrayList<String>();
this.init();
labShowName.setText("共 " + names.size() + "人");

jbnStartEnd.addActionListener(new ActionListener() {

ShowNameByRandomThread showNameByRandomThread = null;
ShanNameThread shanNameThread = null;

public void actionPerformed(ActionEvent e) {

JButton es = (JButton) e.getSource();
String text = es.getText();
if (text.equals("开始")) {
es.setText("结束");

if (shanNameThread != null) {

shanNameThread.close();
shanNameThread = null;
}

showNameByRandomThread = new ShowNameByRandomThread(names,

labShowName);
new Thread(showNameByRandomThread).start();

} else if (text.equals("结束")) {

es.setText("开始");
if (showNameByRandomThread != null) {
showNameByRandomThread.close();
showNameByRandomThread = null;
}
shanNameThread = new ShanNameThread(labShowName);
new Thread(shanNameThread).start();

}

}
});

this.setLocationRelativeTo(null);

this.setVisible(true);
}

private class ShowNameByRandomThread implements Runnable {

private List<String> names = null;
private JLabel labShowName = null;

public ShowNameByRandomThread(List<String> names, JLabel labShowName) {

this.names = names;
this.labShowName = labShowName;
}

private boolean sw = true;

public void run() {

while (sw) {
String ranName = names
.get((int) (Math.random() * names.size()));
labShowName.setText(ranName);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public void close() {

this.sw = false;
}
}

private class ShanNameThread implements Runnable {

private JLabel labShowName = null;
private boolean sw = true;

public ShanNameThread(JLabel labShowName) {

this.labShowName = labShowName;
}

public void run() {

labShowName.setForeground(Color.red);
while (sw) {
labShowName.setVisible(false);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
labShowName.setVisible(true);

try {

Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public void close() {

this.sw = false;
}
}

private void init() {

try {
InputStream is = MainFrame.class.getClassLoader()
.getResourceAsStream("names.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String name = null;
while ((name = in.readLine()) != null) {
if (name != null && name.trim().length() > 0) {
names.add(name);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {

new MainFrame();
}
}

转载于:https://www.cnblogs.com/zhanggaosong/archive/2013/03/14/2959157.html

你可能感兴趣的文章
SCCM2012SP1---资产管理和远程管理
查看>>
Android Activity 之 startActivityForResult 的使用
查看>>
org.springframework.util 类 Assert的使用
查看>>
java提供类与cglib包实现动态代理
查看>>
flask上传多个文件,获取input中的数组
查看>>
更改UIView的背景
查看>>
JLNotebookView
查看>>
StackPanel
查看>>
SPUserResizableView
查看>>
UML类图示例
查看>>
sh ./ 执行区别
查看>>
宏定义(#ifndef+#define+#endif)的作用
查看>>
Prometheus安装部署以及配置
查看>>
Oracle存储过程大冒险-2存储过程常用语法
查看>>
taobao-pamirs-schedule-2.0源码分析——类设计
查看>>
10位程序员眼中的2007:寻找软件开…
查看>>
Stream API
查看>>
Web开发之-DOM操作对象
查看>>
APUE第15章学习扎记之程序的存储区布局试验
查看>>
ubuntu升级16.04 inter idea 中文输入法无效
查看>>