[Java动态随机点名器(通过读取文件获取姓名)分享,也请大佬看看有没有可以改进的
程序员文章站
2022-06-24 23:54:43
...
#这是一个Java做的动态点名器,名字滚动出现,运用简单的多线程,刚好有需要的可以看看,也请大佬给我提出一些改进意见
下面是主代码
package SwingTest;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import java.awt.Component;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Color;
import java.util.*;
import javax.swing.JRadioButton;
import java.awt.Toolkit;
public class SwingTest1 extends JFrame implements Runnable {
private JPanel contentPane;
//重复点名单选框
public JRadioButton rdbtnNewRadioButton;
//不重复点名单选框
public JRadioButton rdbtnNewRadioButton_1;
//显示姓名信息文本框
private static JTextField textField;
//线程运行标志
private static boolean flag=true;
public static SwingTest1 swingtest;
public static Thread thread;
/**
* 定义点名的班级成员
*/
public static List<Student>students=new ArrayList<Student>();
//点名次数
public int number=0;
//动态获取students班级人数
private static int num=0;
//随机数,students集合下标
private static int index=-1;
//用于改变button的开始与停止点名
public static int buttonflag=1;
//获取最初的班级人数,用于后面判断点名结束
public static int beginnum=0;
//判断是否选中了重复点名单选框
public boolean rdbtnNewRadioButtonFlag=false;
//判断是否选中了不重复点名单选框
public boolean rdbtnNewRadioButton_1Flag=false;
//设置set集合,用于避免重复点名
public Set<Integer> setindex=new HashSet<Integer>();
public void run() {
// TODO 自动生成的方法存根
Random random=new Random();
//初次运行线程时获取人数
num=students.size();
while(flag) {
index=random.nextInt(num);
//不重复点名选中
if(rdbtnNewRadioButton_1Flag) {
//判断随机数是否在setindex集合中,不在则输出
if(!setindex.contains(index))
{
textField.setText(students.get(index).toString());
}
else {
//setindex集合有随机数,则重新产生随机数
while(flag) {
index=random.nextInt(num);
if(!setindex.contains(index))
{
textField.setText(students.get(index).toString());
break;
}
}
}
}
//重复点名单选框选中
if(rdbtnNewRadioButtonFlag) {
textField.setText(students.get(index).toString());
}
try {
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
} //每次点到名字的随机数加到集合中,用于当不重复点名时的判断
setindex.add(index);
/**
* Launch the application.
*/
}
public static void main(String[] args) {
swingtest=new SwingTest1();
thread=new Thread(swingtest);
}
/**
* Create the frame.
*/
public SwingTest1() {
setResizable(false);
setTitle("\u70B9\u540D\u56682.0");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 520, 408);
contentPane = new JPanel();
contentPane.setBackground(new Color(135, 206, 250));
contentPane.setForeground(Color.CYAN);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
/**
* 选择班级标签
*/
JLabel lblNewLabel = new JLabel("\u9009\u62E9\u73ED\u7EA7\uFF1A");
lblNewLabel.setForeground(new Color(30, 144, 255));
lblNewLabel.setFont(new Font("楷体", Font.BOLD, 25));
lblNewLabel.setBounds(32, 13, 170, 52);
contentPane.add(lblNewLabel);
/**
* 班级选择下拉框
*/
JComboBox comboBox = new JComboBox();
comboBox.setFont(new Font("方正粗黑宋简体", Font.PLAIN, 18));
comboBox.setBounds(155, 25, 154, 35);
contentPane.add(comboBox);
/**
* 开始和停止标签
*/
JButton btnNewButton = new JButton("\u5F00\u59CB");
btnNewButton.setForeground(Color.BLACK);
btnNewButton.setFont(new Font("楷体", Font.PLAIN, 30));
btnNewButton.setBounds(32, 259, 170, 83);
contentPane.add(btnNewButton);
/**
* 显示姓名信息文本框
*/
textField = new JTextField();
textField.setEditable(false);
textField.setForeground(new Color(255, 0, 0));
textField.setFont(new Font("新宋体", Font.PLAIN, 22));
textField.setBounds(155, 122, 308, 35);
contentPane.add(textField);
textField.setColumns(10);
/**
* 总人数标签
*/
JLabel lblNewLabel_3 = new JLabel("");
lblNewLabel_3.setFont(new Font("华文楷体", Font.BOLD, 22));
lblNewLabel_3.setBounds(183, 73, 92, 36);
contentPane.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("\u603B \u4EBA \u6570\uFF1A");
lblNewLabel_4.setFont(new Font("宋体", Font.PLAIN, 25));
lblNewLabel_4.setBounds(42, 78, 146, 27);
contentPane.add(lblNewLabel_4);
/**
* 姓名信息标签
*/
JLabel lblNewLabel_1 = new JLabel("\u59D3\u540D\u4FE1\u606F\uFF1A");
lblNewLabel_1.setFont(new Font("幼圆", Font.PLAIN, 25));
lblNewLabel_1.setBounds(32, 119, 154, 40);
contentPane.add(lblNewLabel_1);
/**
* 点名次数标签
*/
JLabel label = new JLabel("\u70B9\u540D\u6B21\u6570\uFF1A");
label.setFont(new Font("幼圆", Font.PLAIN, 25));
label.setBounds(32, 184, 154, 40);
contentPane.add(label);
/**
* 点名计数标签
*/
JLabel lblNewLabel_2 = new JLabel("\u672A\u5F00\u59CB\u70B9\u540D");
lblNewLabel_2.setForeground(Color.RED);
lblNewLabel_2.setFont(new Font("华文行楷", Font.PLAIN, 22));
lblNewLabel_2.setBounds(191, 190, 297, 32);
contentPane.add(lblNewLabel_2);
/**
* 退出按键Button
*/
JButton btnNewButton_1 = new JButton("\u9000\u51FA");
btnNewButton_1.setFont(new Font("楷体", Font.PLAIN, 30));
btnNewButton_1.setBounds(282, 259, 170, 83);
contentPane.add(btnNewButton_1);
/**
* rdbtnNewRadioButton为重复点名单选框
*/
JRadioButton rdbtnNewRadioButton = new JRadioButton("\u91CD\u590D\u70B9\u540D");
rdbtnNewRadioButton.setBackground(new Color(135, 206, 250));
rdbtnNewRadioButton.setFont(new Font("宋体", Font.PLAIN, 20));
rdbtnNewRadioButton.setBounds(363, 36, 121, 23);
contentPane.add(rdbtnNewRadioButton);
/**
* rdbtnNewRadioButton_1为不重复点名单选框
*/
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("\u4E0D\u91CD\u590D\u70B9\u540D");
rdbtnNewRadioButton_1.setBackground(new Color(135, 206, 250));
rdbtnNewRadioButton_1.setFont(new Font("宋体", Font.PLAIN, 20));
rdbtnNewRadioButton_1.setBounds(363, 75, 139, 23);
contentPane.add(rdbtnNewRadioButton_1);
setVisible(true);
//获取总班级文本文件内容
List<String>ls=test1.getClassName();
//声明一个字符串数组,用于显示下拉框信息
String[] classNames=new String[ls.size()];
//将集合ls转为数组
ls.toArray(classNames);
//给下拉框设置值
comboBox.setModel(new DefaultComboBoxModel(classNames));
//students默认获得下拉框第一个信息
students=test3.getClassName((String)comboBox.getSelectedItem());
//获取最初的班级人数,用于后面判断点名结束,默认获得下拉框第一个信息
beginnum=students.size();
//输出总人数
lblNewLabel_3.setText(beginnum+"人");
//重复点名单选框事件
rdbtnNewRadioButton.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
// TODO 自动生成的方法存根
//如果重复单选框选中,则取消不重复点名单选框的选中
if(rdbtnNewRadioButton.isSelected()) {
rdbtnNewRadioButton_1.setSelected(false);
}
//点名按钮改为可用
btnNewButton.setEnabled(true);
}
});
//不重复点名单选框事件
rdbtnNewRadioButton_1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
// TODO 自动生成的方法存根
//如果不重复单选框选中,则取消重复点名单选框的选中
if(rdbtnNewRadioButton_1.isSelected()) {
rdbtnNewRadioButton.setSelected(false);
}
//点名按钮改为可用
btnNewButton.setEnabled(true);
}
});
//下拉框点击事件
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
//每点击一次下拉框就重新给setindex集合内容清除
setindex.clear();
//每点击一次下拉框就根据下拉框的班级名字重新给班级成员students赋值
students=test3.getClassName((String)comboBox.getSelectedItem());
//获取最初的班级人数,用于后面判断点名结束
beginnum=students.size();
//输出总人数
lblNewLabel_3.setText(beginnum+"人");
//文本框内容初始化
textField.setText("");
//每重新选择一次班级,点名计数归0
number=0;
//重新选择班级后,开始点名的button变为可用,并改变显示信息
btnNewButton.setEnabled(true);
lblNewLabel_2.setText("未开始点名");
btnNewButton.setText("开始");
}
});
//button开始点名事件
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取重复点名单选框和不重复点名单选框状态
rdbtnNewRadioButtonFlag=rdbtnNewRadioButton.isSelected();
rdbtnNewRadioButton_1Flag=rdbtnNewRadioButton_1.isSelected();
//两个点名方式单选框状态相等则说明未选择,不能开始点名
if(rdbtnNewRadioButtonFlag==rdbtnNewRadioButton_1Flag) {
textField.setText("未选择点名方式!");
//点名按钮禁用
btnNewButton.setEnabled(false);
}
else {
//如果点名次数number与获取最初的班级人数beginnum相等,并且不重复点名单选框选中,则结束当前点名,不相等则运行线程
if((beginnum<=number)&&(setindex.size()==beginnum)&&(rdbtnNewRadioButton_1Flag)) {
//点名button按键禁用
btnNewButton.setEnabled(false);
textField.setText("名字已点完!请重新选择班级!");
lblNewLabel_2.setText(" "+(String)comboBox.getSelectedItem()+"名字已点完,共点了"+number+"次");
}
else {//判断button的运行标志
if(buttonflag==1) {
//线程标志改为true
flag=true;
//改当前button文本信息为停止
btnNewButton.setText("停止");
//开启线程
new Thread(swingtest).start();
//将下拉框和两个单选框改为不可选择
comboBox.setEnabled(false);
rdbtnNewRadioButton.setEnabled(false);
rdbtnNewRadioButton_1.setEnabled(false);
}
//先把button运行标志值加一为2,再点击停止时加一为3,线程停止
buttonflag++;
if(buttonflag==3) {
//线程标志为false,停止
flag=false;
//停止一次,即点名一次
number++;
lblNewLabel_2.setText(" "+(String)comboBox.getSelectedItem()+"已点名"+number+"次");
//修改按钮文本信息
btnNewButton.setText("再点一次");
//将button运行标志改为1,下次点击运行线程
buttonflag=1;
//停止点名,可以改变下拉框信息和两个单选框
comboBox.setEnabled(true);
rdbtnNewRadioButton.setEnabled(true);
rdbtnNewRadioButton_1.setEnabled(true);
}
}
}
}
});
//button退出事件
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//将flag设置为false,并退出程序
flag=false;
System.exit(0);
}
});
}
}
建立的学生类代码
package SwingTest;
import java.io.Serializable;
/**
* 学生类
* @author Administrator
*
*/
public class Student implements Serializable {
public static long getSerialversionid() {
return serialVersionID;
}
public void setId(String id) {
this.id = id;
}
private static final long serialVersionID = 1L;
private String id;//学号
private String name;//姓名
public Student(String id ,String name){
super();
this.id = id;
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return id +"\t"+ name ;
}
}
读取班级的名字
package SwingTest;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class test1 {
public static List<String> getClassName(){
List<String> classes = new ArrayList<String>();
try{
BufferedReader finput = new BufferedReader(
new FileReader("data\\className.txt"));
String s = "";
while((s = finput.readLine()) != null){
classes.add(s);
}
finput.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return classes;
}
}
根据名字获取里面信息的
package SwingTest;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class test3 {
public static List<Student> getClassName(String className){
List<Student> students = new ArrayList<Student>();
try{
BufferedReader finput = new BufferedReader(new FileReader("data\\"+className));
String s = "";
while((s = finput.readLine()) != null){
String[] str = s.split(" ");
Student stu = new Student(str[0],str[1]);
students.add(stu);
}
finput.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return students;
}
}
下面是文件的内容和运行图片
班级名字的内容
各班级名单
欢迎大家提出意见!
下一篇: easypoI模板导出