Java做随机点名系统

如何做到点击开始按钮 按名字开始滑动 点击暂停会停留在一个名字处 就是那种恐怖点名

小魔女参考了bing和GPT部分内容调写:
要实现这个需求,首先需要准备一个存储名字的数据结构,比如数组。然后在点击开始按钮时,设置一个定时器,每隔一段时间,从数组中取出一个名字,显示到界面上,然后继续取下一个名字,直到取完数组中的所有名字,再重新从头开始取。点击停止按钮时,清除定时器,停止取名字,并将当前显示的名字保存起来,显示到界面上。

// 存储名字的数组
var names = ["张三", "李四", "王五", "赵六"];
// 定时器
var timer;

// 点击开始按钮时
function start() {
    // 设置定时器,每隔一段时间取出一个名字,显示到界面上
    timer = setInterval(function () {
        var name = names.shift();
        // 显示名字
        showName(name);
        // 将取出的名字放回数组末尾
        names.push(name);
    }, 1000);
}

// 点击停止按钮时
function stop() {
    // 清除定时器
    clearInterval(timer);
    // 保存当前显示的名字
    var name = getName();
    // 显示名字
    showName(name);
}

回答不易,记得采纳呀。

参考GPT:


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

public class NameRoller extends JFrame {

    private JTextArea nameTextArea; // 显示名字的文本框
    private JButton startButton; // 开始按钮
    private JButton pauseButton; // 暂停按钮

    private boolean isRunning; // 是否正在滚动名字列表
    private boolean isPaused; // 是否暂停滚动

    private String[] names = {"张三", "李四", "王五", "赵六", "钱七"}; // 名字列表

    public NameRoller() {
        // 初始化 GUI 界面
        nameTextArea = new JTextArea();
        startButton = new JButton("开始");
        pauseButton = new JButton("暂停");

        // 添加事件监听器
        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                startRolling();
            }
        });

        pauseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                pauseRolling();
            }
        });

        // 添加组件到 GUI 界面中
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(nameTextArea, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(startButton);
        buttonPanel.add(pauseButton);
        panel.add(buttonPanel, BorderLayout.SOUTH);

        add(panel);

        // 初始化其他变量
        isRunning = false;
        isPaused = false;
    }

    // 启动滚动名字列表的线程
    private void startRolling() {
        if (!isRunning) {
            isRunning = true;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int index = 0;
                    while (isRunning) {
                        // 滚动名字列表
                        index = (index + 1) % names.length;
                        String name = names[index];
                        // 显示当前名字
                        nameTextArea.setText(name);
                        // 休眠一段时间
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        // 如果暂停了,则等待恢复
                        while (isPaused) {
                            try {
                                Thread.sleep(10);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }).start();
        }
    }


    // 暂停滚动名字列表的线程
    private void pauseRolling() {
        isPaused = true;
    }

    public static void main(String[] args) {
        NameRoller nameRoller = new NameRoller();
        nameRoller.setSize(400, 300);
        nameRoller.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        nameRoller.setVisible(true);
    }
}

GPT内容调写:


import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class HelloWorld extends JFrame {
    private static final long serialVersionUID = 1L;
    // 定义GUI组件
    private JButton startButton, stopButton;
    private JLabel nameLabel;
    private volatile boolean running = false; // 是否需要继续随机点名
    public HelloWorld() {
        // 初始化GUI组件
        startButton = new JButton("开始");
        stopButton = new JButton("暂停");
        nameLabel = new JLabel("请点击开始按钮", JLabel.CENTER);
        nameLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        // 添加GUI组件到窗口中
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(startButton, BorderLayout.WEST);
        panel.add(stopButton, BorderLayout.EAST);
        getContentPane().add(panel, BorderLayout.NORTH);
        getContentPane().add(nameLabel, BorderLayout.CENTER);
        // 窗口设置
        setTitle("随机点名器");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        // 添加事件监听器
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 点击开始按钮后,启动一个线程来随机选择名字
                Thread t = new Thread() {
                    public void run() {
                        String[] names = {"张三", "李四", "王五", "赵六", "钱七"};
                        running = true;
                        while(running) {
                            // 生成随机数
                            int index = new Random().nextInt(names.length);
                            // 在界面上显示点名结果
                            nameLabel.setText(names[index]);
                            // 线程休眠一段时间,模拟点名的效果
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                // 线程被中断,退出循环
                                break;
                            }
                        }
                    }
                };
                t.start();
            }
        });
        stopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 点击暂停按钮后,停止随机点名的效果
                running = false;
            }
        });
    }
    public static void main(String[] args) {
        HelloWorld app = new HelloWorld();
        app.setVisible(true);
    }
}

html版参考:https://blog.csdn.net/weixin_44563573/article/details/107503777

Java版参考:

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

public class RandomNamePicker extends JFrame implements ActionListener {
    private JTextArea textArea;
    private JButton startButton, pauseButton, resumeButton;
    private Timer timer;
    private ArrayList<String> names;
    private Random random;

    public RandomNamePicker(ArrayList<String> names) {
        this.names = names;
        this.random = new Random();
        initComponents();
    }

    private void initComponents() {
        setTitle("Random Name Picker");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        textArea = new JTextArea();
        textArea.setEditable(false);
        add(textArea, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel(new FlowLayout());

        startButton = new JButton("Start");
        startButton.addActionListener(this);
        buttonPanel.add(startButton);

        pauseButton = new JButton("Pause");
        pauseButton.addActionListener(this);
        pauseButton.setEnabled(false);
        buttonPanel.add(pauseButton);

        resumeButton = new JButton("Resume");
        resumeButton.addActionListener(this);
        resumeButton.setEnabled(false);
        buttonPanel.add(resumeButton);

        add(buttonPanel, BorderLayout.SOUTH);

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == startButton) {
            startButton.setEnabled(false);
            pauseButton.setEnabled(true);
            timer = new Timer(50, new TimerListener());
            timer.start();
        } else if (e.getSource() == pauseButton) {
            pauseButton.setEnabled(false);
            resumeButton.setEnabled(true);
            timer.stop();
            String selectedName = names.get(random.nextInt(names.size()));
            textArea.setText(selectedName);
        } else if (e.getSource() == resumeButton) {
            pauseButton.setEnabled(true);
            resumeButton.setEnabled(false);
            timer.restart();
        }
    }

    private class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String selectedName = names.get(random.nextInt(names.size()));
            textArea.setText(selectedName);
        }
    }

    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        names.add("David");
        RandomNamePicker picker = new RandomNamePicker(names);
    }
}