JAVA GUI 随机抽人

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值