JAVA版随机抽人

2024-01-01 19:48:09

主函数

public class Main {
    public static void main(String[] args) {
        //这里存入数据
        String[] data = {"土一","李二","张三","李四","乔冠宇","王五"};
        MyJFrame frame = new MyJFrame(data);
    }
}

界面类

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class MyJFrame extends JFrame {
    String[] strings;
    Thread thread;

    public MyJFrame() throws HeadlessException {
        JOptionPane.showMessageDialog(null,"数据未传入","数据异常",JOptionPane.ERROR_MESSAGE);
    }

    public MyJFrame(String[] strings) throws HeadlessException {
        this.strings = strings;
        this.setSize(400,400);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setTitle("抽取幸运儿");

        JPanel jPanel_body = new JPanel(new GridLayout(3,1));

        JPanel jPanel_top = new JPanel(new GridLayout(2,1));
        JPanel jPanel_top_top = new JPanel();
        jPanel_top.add(jPanel_top_top);
        JPanel jPanel_top_bottom = new JPanel();
        JLabel jLabel = new JLabel("新的幸运儿已经产生");
        jLabel.setFont(new Font("宋体",Font.BOLD,20));
        jPanel_top_bottom.add(jLabel);
        jPanel_top.add(jPanel_top_bottom);

        JPanel jPanel_center = new JPanel();
        JTextField field = new JTextField(18);
        field.setFont(new Font("宋体",Font.PLAIN,20));
        jPanel_center.add(field);

        JPanel jPanel_bottom = new JPanel();
        JButton button = new JButton("暂停");
        button.setPreferredSize(new Dimension(80, 32));
        jPanel_bottom.add(button);


        JButton jButton = new JButton("开始随机点人");
        jButton.setPreferredSize(new Dimension(80, 32));
        jPanel_bottom.add(jButton);
        jButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MyTask task = new MyTask(field,strings);
                if(thread==null){
                    thread = new Thread(task);
                    thread.start();
                }
            }
        });

        button.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(thread!=null){
                    thread.interrupt();
                    thread = null;
                }
            }
        });

        jPanel_body.add(jPanel_top);
        jPanel_body.add(jPanel_center);
        jPanel_body.add(jPanel_bottom);
        this.add(jPanel_body);
        this.setVisible(true);
    }
}

线程类

import javax.swing.*;
import java.util.Random;

public class MyTask implements Runnable{
    JTextField field;
    String[] strings;
    static Object object = new Object();

    public MyTask(JTextField field, String[] strings) {
        this.field = field;
        this.strings = strings;
    }

    @Override
    public void run() {
        synchronized (object){
            Random random = new Random();
            while (true){
                int index = random.nextInt(strings.length);
                field.setText(strings[index]);
            }
        }
    }
}

程序展示

对代码进行解释

这里主要是运用到了JAVA中的GUI和线程的知识,?在界面类中,需要注意的是事件监听这里,两个按钮分别控制了线程的关闭和启动,而在线程类中,我们要注意的是创建新任务时传参的问题。

文章来源:https://blog.csdn.net/2301_77523019/article/details/135324691
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。