1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import java.awt.Rectangle;
5
6 public class Test4dialog {
7
8 JFrame jframe = new JFrame("个人信息录入器");
9 String Mexs[] = { "男", "女" };
10 JTextArea ta = new JTextArea("");
11 JTextField tfname = new JTextField(6);
12 JTextField tfage = new JTextField(6);
13 JComboBox cmex = new JComboBox(Mexs);
14 JButton ok = new JButton("确定");
15 JPanel jp = new JPanel();
16 JLabel Name = new JLabel("姓名");
17 JLabel Mex = new JLabel("性别");
18 JLabel Age = new JLabel("年龄");
19
20 public static void main(String args[]) {
21 new Test4dialog();
22 }
23
24 Test4dialog() {
25
26 jframe.setSize(400, 200);
27 jframe.setLayout(new BorderLayout());
28 jframe.add(jp, BorderLayout.NORTH);
29 jframe.add(ta, BorderLayout.CENTER);
30
31 ta.setBounds(new Rectangle(75, 75, 80, 30));
32
33 jp.setLayout(new FlowLayout());
34 jp.add(Name);
35 jp.add(tfname);
36 jp.add(Mex);
37 jp.add(cmex);
38 jp.add(Age);
39 jp.add(tfage);
40 jp.add(ok);
41
42 ok.addActionListener(new ActionListener() {
43 public void actionPerformed(java.awt.event.ActionEvent e) {
44 if (tfname.getText().trim().equals("")) {
45 ta.setText("姓名不能为空");
46 } else {
47 ta.setText("姓名:" + tfname.getText() + "\n性别:"
48 + cmex.getSelectedItem().toString() + "\n年龄:"
49 + tfage.getText());
50 }
51 }
52 });
53
54 jframe.setVisible(true);
55 jframe.setDefaultCloseOperation(jframe.EXIT_ON_CLOSE);
56 }
57 }
58
59