【Java】【组件及事件处理】利用数组随机抽取幸运观众

做出以下界面

在这里插入图片描述
MyFrame类:

package com.itheima;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
public class MyFrame extends JFrame{
    
    
    JPanel panel1,panel2;
    JTextField text;
    JTextArea area1,area2;
    JButton button1,button2;
    PoliceListen policeListen;
    public MyFrame() {
    
    
        init();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    void init(){
    
    
        setLayout(new FlowLayout());
        text = new JTextField(12);
        area1 = new JTextArea(10,12);
        area2 = new JTextArea(11,14);
        button1 = new JButton("抽取");
        button2 = new JButton("清除");
        panel1 = new JPanel();
        panel1.setBorder(new TitledBorder(new EtchedBorder(),"输入在场观众按回车"));
        panel1.setPreferredSize(new Dimension(150,220));
        panel1.add(text);
        panel1.add(new JScrollPane(area1));
        panel2 = new JPanel();
        panel2.setBorder(new TitledBorder(new EtchedBorder(),"选取观众人员"));
        panel2.setPreferredSize(new Dimension(160,220));
        panel2.add(new JScrollPane(area2));
        add(panel1);
        add(panel2);
        add(button1);
        add(button2);
        policeListen = new PoliceListen();
        policeListen.setText(text);
        policeListen.setArea1(area1);
        policeListen.setArea2(area2);
        policeListen.setButton1(button1);
        policeListen.setButton2(button2);
        text.addActionListener(policeListen);
        button1.addActionListener(policeListen);
        button2.addActionListener(policeListen);
    }
}

PoliceListen类:

package com.itheima;

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

public class PoliceListen implements ActionListener {
    
    
    JTextField text;
    JTextArea area1,area2;
    JButton button1,button2;

    public void setArea1(JTextArea area1) {
    
    
        this.area1 = area1;
    }

    public void setArea2(JTextArea area2) {
    
    
        this.area2 = area2;
    }

    public void setText(JTextField text) {
    
    
        this.text = text;
    }

    public void setButton1(JButton button1) {
    
    
        this.button1 = button1;
    }

    public void setButton2(JButton button2) {
    
    
        this.button2 = button2;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    
    
        if(e.getSource() == text){
    
    
            String name = text.getText();
            area1.append(name+"\n");
            text.setText("");
        }
        if(e.getSource() == button1){
    
    
            String perstring = area1.getText();
            String personArray[] = perstring.split("\n{1,}");
            int index = (int)(Math.random() * personArray.length);
            String award = "本次抽取观众人员:" + personArray[index]+"\n"+
                    "奖励过期的酸奶二十箱。";
            area2.setText(award);
        }
        if(e.getSource() == button2){
    
    
            text.setText("");
            area1.setText("");
            area2.setText("");
        }
    }
}

Main类:

package com.itheima;
public class Main {
    
    
    public static void main(String[] args) {
    
    
        MyFrame win = new MyFrame();
        win.setBounds(100,100,350,300);
        win.setTitle("利用数组随机抽奖幸运观众");
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_48180029/article/details/111843644