package Main;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
public class MainApp {
private static JLabel jLabel;
private static ArrayList userList;
public static void main(String[]args){
File file=new File("D:/user.txt");
userList=new ArrayList();
try {
BufferedReader br=new BufferedReader(new FileReader(file));
String content=null;
while ((content=br.readLine())!=null){
userList.add(content);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JFrame jframe=new JFrame();
jframe.setTitle("点名器");
jframe.setSize(500,500);
/**获取屏幕大小*/
Dimension scree=Toolkit.getDefaultToolkit().getScreenSize();
int width=(int)(scree.getWidth()-jframe.getWidth());
int height=(int)(scree.getHeight()-jframe.getHeight());
jframe.setLocation(width/2,height/2);
addLabel(jframe);
addButton(jframe);
jframe.setVisible(true);
}
/**添加显示面板*/
public static void addLabel(JFrame jFrame){
jLabel=new JLabel("请点击按钮,开始点名!",JLabel.CENTER);
jLabel.setSize(50,50);
jLabel.setFont(new Font("宋体", Font.PLAIN, 25));
jFrame.add(jLabel);
}
/**添加按钮*/
public static void addButton(JFrame jFrame){
JPanel jPanel=new JPanel();
JButton jButton=new JButton("开始点名");
jButton.setSize(30,20);
jPanel.add(jButton);
jFrame.add(jPanel,BorderLayout.SOUTH);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
@Override
public void run() {
long begin=System.currentTimeMillis()/1000;
while (true){
Random random=new Random();
jLabel.setText((String) userList.get(random.nextInt(userList.size() - 1)));
long end=System.currentTimeMillis()/1000;
if (end-begin>=2){
try {
Thread.sleep(200);
if (end-begin>=3){
Thread.sleep(400);
if (end-begin>=4){
break;
}
}
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
}
}
}).start();
}
});
}
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史