package tast2;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class A2 extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
A2 frame = new A2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public A2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 458, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTextArea textArea = new JTextArea();
JTextArea textArea_1 = new JTextArea();
JLabel label = new JLabel("\u8F93\u5165\u59D3\u540D");
label.setBounds(10, 36, 69, 15);
contentPane.add(label);
JButton btnNewButton = new JButton("\u62BD \u53D6");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String[] strArray;
strArray=textArea.getText().split("\n{1,}");
int index = (int)(Math.random()*strArray.length);
textArea_1.append("抽取到幸运观众:\n"+strArray[index]+'\n');
}
});
btnNewButton.setBounds(302, 32, 69, 23);
contentPane.add(btnNewButton);
textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()!='\n')
return;
String name = textField.getText();
if(name.isEmpty()) {
return;
}
textArea.append(name+'\n');
textField.setText("");
}
});
textField.setBounds(74, 33, 218, 21);
contentPane.add(textField);
textField.setColumns(10);
textArea.setBounds(74, 66, 128, 169);
contentPane.add(textArea);
textArea_1.setBounds(243, 65, 128, 170);
contentPane.add(textArea_1);
}
}