幸运观众抽奖

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 javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JLabel;

public class LuckyDog {

private JFrame frame;
private JTextField textField;
private JScrollPane scrollPane;
private JLabel label_1;
JTextArea textArea = new JTextArea();
private JTextArea textArea_1;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LuckyDog window = new LuckyDog();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public LuckyDog() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("利用数组随机抽取幸运观众");
frame.setBounds(100, 100, 500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JLabel label = new JLabel("\u8F93\u5165\u5728\u573A\u89C2\u4F17\u59D3\u540D\u6309\u56DE\u8F66");
label.setBounds(10, 10, 132, 15);
frame.getContentPane().add(label);

textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
do_textField_keyPressed(e);
}
});
textField.setBounds(10, 35, 132, 21);
frame.getContentPane().add(textField);
textField.setColumns(10);

scrollPane = new JScrollPane();
scrollPane.setBounds(10, 66, 132, 185);
frame.getContentPane().add(scrollPane);

textArea_1 = new JTextArea();
scrollPane.setViewportView(textArea_1);

label_1 = new JLabel("\u9009\u53D6\u89C2\u4F17\u4EBA\u5458\uFF1A");
label_1.setBounds(180, 10, 132, 15);
frame.getContentPane().add(label_1);


textArea.setBounds(180, 34, 214, 217);
frame.getContentPane().add(textArea);

JButton button = new JButton("\u62BD\u53D6");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_actionPerformed(e);
}
});
button.setBounds(404, 187, 70, 23);
frame.getContentPane().add(button);

JButton button_1 = new JButton("\u9000\u51FA");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_1_actionPerformed(e);
}
});
button_1.setBounds(404, 228, 70, 23);
frame.getContentPane().add(button_1);
}
protected void do_textField_keyPressed(KeyEvent e) {
if (e.getKeyChar() != '\n')// 不是回车字符不做处理
return;
String name = textField.getText();
if (name.isEmpty())// 如果文本框没有字符串不做处理
return;
textArea_1.append(name + "\n");// 把输入人名与回车符添加到人员列表
textField.selectAll();// 选择文本框所有字符
}
protected void do_button_actionPerformed(ActionEvent e) {
String perstring = textArea_1.getText();// 获取人员列表文本
String[] personnelArray = perstring.split("\n{1,}");// 获取人员数组
int index = (int) (Math.random() * personnelArray.length);// 生成随机数组索引
// 定义包含格式参数的中奖信息
String formatArg = "本次抽取观众人员:\n\t%1$s\n恭喜%1$5s成为本次观众抽奖的大奖得主。"
+ "\n\n我们将为%1$5s颁发:\n\t过期的酸奶二十箱。";
// 为中奖信息添加人员参数
String info = String.format(formatArg, personnelArray[index]);
textArea.setText(info);// 在文本域显示中奖信息
}
protected void do_button_1_actionPerformed(ActionEvent e) {
System.exit(0);
}
}

posted @ 2015-05-30 22:25  sunshinewxz  阅读(282)  评论(0编辑  收藏  举报