import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class Main2 extends JFrame {
String[] name = new String[]{
"张三", "李四", "王五", "赵六", "赵东选"
};
ArrayList<String> list = new ArrayList<>(Arrays.stream(name).toList());
//
boolean make = true;
public Main2() {
//设置窗口
this.setSize(600, 600);
this.setLocationRelativeTo(null);
this.setTitle("抽人");
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//窗口面板
JPanel windowJPanel = new JPanel();
this.add(windowJPanel);
//窗口窗口 网格布局
windowJPanel.setLayout(new GridLayout(3, 1));
//标签开头(JLabel)
JLabel initioJLabel = new JLabel("新的幸运儿已经产生了", JLabel.CENTER);
initioJLabel.setFont(new Font("宋体", Font.BOLD, 30));
windowJPanel.add(initioJLabel);
//创建Jpanel面板
JPanel JPanel = new JPanel();
windowJPanel.add(JPanel);
//JPanel面板流式布局
JPanel.setLayout(new FlowLayout());
//创建单行文本(JTextField)
JTextField JTextField = new JTextField(30);
JPanel.add(JTextField);
//创建Jpanel选项面板
JPanel JPanel2 = new JPanel();
windowJPanel.add(JPanel2);
//optionJPanel面板流式布局
JPanel2.setLayout(new FlowLayout());
//设置 随机点名 按钮(JButton)
JButton logJButton = new JButton("随机点名");
JPanel2.add(logJButton);
//设置 随机点名 按钮(JButton)
JButton logJButton2 = new JButton("停止");
JPanel2.add(logJButton2);
logJButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
@Override
public void run() {
if (list.size() == 0) {
make = false;
JOptionPane.showMessageDialog(null, "没人了");
return;
} else {
make = true;
}
while (make) {
Random random = new Random();
JTextField.setText(list.get(random.nextInt(list.size())));
}
}
}).start();
}
});
logJButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (list.size() == 0) {
JOptionPane.showMessageDialog(null, "没人了");
return;
}
make = false;
Random random = new Random();
JTextField.setText(list.remove(random.nextInt(list.size())));
}
});
//将窗口现形
this.setVisible(true);
}
public static void main(String[] args) {
new Main2();
}
}
JAVA GUI 随机抽人
最新推荐文章于 2024-09-07 12:59:11 发布