运行效果图:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e2bbb48a61a253df1fb21943fd9f32bb.png)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class Test extends JFrame {
JPanel conventPane;
JTextField nameField;
JTextArea personArea;
JTextArea ansowerArea;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
try {
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
Test Frame = new Test();
Frame.setVisible(true);
}
});
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public Test() {
conventPane = new JPanel();
conventPane.setLayout(null);
conventPane.setBorder(new EmptyBorder(5, 5, 5, 5));
this.setTitle("利用数组随机抽取幸运观众");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(conventPane);
this.setBounds(100, 100, 498, 300);
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(null,"将现场观众的姓名添加到抽奖箱中,按回车键确认",
TitledBorder.LEADING,TitledBorder.TOP,null,new Color(0,0,255)));
panel.setBounds(10, 10, 174, 242);
panel.setLayout(new BorderLayout(0,5));
conventPane.add(panel);
nameField = new JTextField();
nameField.setColumns(10);
panel.add(nameField,BorderLayout.NORTH);
nameField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
do_Text_KetPressed(e);
}
});
JScrollPane scrollPane = new JScrollPane();
panel.add(scrollPane);
personArea = new JTextArea();
personArea.setEditable(false);
scrollPane.setViewportView(personArea);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null,"选取的观众人员是",TitledBorder.LEADING,
TitledBorder.TOP,null,new Color(255,0,0)));
panel_1.setLayout(new BorderLayout(0,0));
panel_1.setBounds(183, 10, 219, 242);
conventPane.add(panel_1);
ansowerArea = new JTextArea();
ansowerArea.setEditable(false);
ansowerArea.setLineWrap(true);
JScrollPane scrollPane_1 = new JScrollPane();
panel_1.add(scrollPane_1);
scrollPane_1.setViewportView(ansowerArea);
JButton button = new JButton("抽取");
button.setBounds(407, 164, 63, 25);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
do_button_actionPerformed();
}
});
conventPane.add(button);
JButton button_1 = new JButton("退出");
button_1.setBounds(407, 215, 63, 25);
button_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
do_button_1_actionPerformed();
}
});
conventPane.add(button_1);
}
protected void do_button_1_actionPerformed() {
dispose();
}
protected void do_button_actionPerformed() {
String perstring = personArea.getText();
String[] personnelArray = perstring.split("\n{1,}");
int index = (int) (Math.random() * personnelArray.length);
String formatArg = "本次抽取观众人员:\n\t%1$s\n恭喜%1$s成为本次观众抽奖的大奖得主。"
+ "\n\n我们将为%1$s颁发:\n\t过期的酸奶二十箱。";
String info = String.format(formatArg, personnelArray[index]);
ansowerArea.setText(info);
}
protected void do_Text_KetPressed(KeyEvent e) {
if (e.getKeyChar() != '\n')
return;
String name = nameField.getText();
if (name.isEmpty())
return;
personArea.append(name + "\n");
personArea.selectAll();
}
}