zoukankan      html  css  js  c++  java
  • 点名器

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

  • 相关阅读:
    周4早上搜索引擎分析 crmim.com| MSCRM开发者之家
    Bat命令学习
    sqlserver日期函数
    ubunto应用软件
    sql for xml
    win7x64 连接oracle 客户端 vs 2010调试 提示“ORA12154: TNS: 无法解析指定的连接标识符 ”ORA06413 问题(转)
    CentOS Rsync服务端与Windows cwRsync客户端实现数据同步
    怎么引导2岁孩子洗手问题
    Libnfcinstallation
    Asterisk资料
  • 原文地址:https://www.cnblogs.com/zhanggaosong/p/2959157.html
Copyright © 2011-2022 走看看