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(); }}