1. 程式人生 >實用技巧 >其它 >利用swing寫了一個隨機點名小程式,超級實用!!!

利用swing寫了一個隨機點名小程式,超級實用!!!


利用swing做一個隨機點名的小程式,可以新增姓名後點擊查詢,有一個查詢的動態過程渲染的過程,最後停留在隨機找到的那個人的框裡

程式碼

mport javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class RandomNameFrame extends JFrame implements ActionListener {
    private JTextField TextField_1, TextField_2;
    private JButton Button_1, Button_2;
    JTextField field;
    static ExecutorService executorService = Executors.newFixedThreadPool(5);

    JPanel panel_2;
    static List<String> names = new ArrayList<>();

    static List<JTextField> jTextFields = new ArrayList<>();


    public RandomNameFrame() {

        super("隨機數序列,查詢重複值");
        this.setSize(400, 200);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel panel_1 = new JPanel(new FlowLayout());
        JLabel Label_1 = new JLabel("新增學生姓名");
        TextField_1 = new JTextField("", 4);

        Button_1 = new JButton("確定");
        this.Button_1.addActionListener(this);
        Button_2 = new JButton("查詢");
        this.Button_2.addActionListener(this);

        panel_1.add(Label_1);
        panel_1.add(TextField_1);
        panel_1.add(Button_1);
        panel_1.add(Button_2);
        this.getContentPane().add(panel_1, "North");

        panel_2 = new JPanel(new GridLayout(4, 5));
        this.getContentPane().add(panel_2, "Center");


        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == this.Button_1) {
            String name = TextField_1.getText();
            names.add(name);
            panel_2.add(field = new JTextField(name));
            jTextFields.add(field);
        }
        if (event.getSource() == this.Button_2) {
            executorService.execute(()->{
                try {
                    extracted();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        this.setVisible(true);
    }

    private void extracted() throws InterruptedException {
        int n = 0;
        while (n < 30) {
            Random random = new Random();
            int i = random.nextInt(jTextFields.size());
            JTextField jTextField = jTextFields.get(i);
            jTextField.setBackground(new Color(248, 42, 6));
            n++;
            this.setVisible(true);
            if (n<10) {
                Thread.sleep(500);
            }
            if (n>10&&n<15){
                Thread.sleep(300);
            }
            if (n>15&&n<42){
                Thread.sleep(200);
            }
            if (n>42){
                Thread.sleep(100);
            }
            jTextField.setBackground(new Color(246, 243, 242));
            this.setVisible(true);
        }

        Random random = new Random();
        int i = random.nextInt(jTextFields.size());
        JTextField jTextField = jTextFields.get(i);
        jTextField.setBackground(new Color(219, 192, 20));
        Thread.sleep(100);
        this.setVisible(true);
        jTextField.setBackground(new Color(248, 42, 6));
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new RandomNameFrame();
    }
}

執行結果

利用swing寫了一個隨機點名小程式,超級實用!!!