package gui;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class Demo extends JFrame {
JFrame frame;
JPanel pane1, pane2, pane3, pane4, pane5;
JTextField text1, text2, text3;
JLabel label1, label2, label3, label4, label5, label6;
JButton button1, button2;
JComboBox combobox, combobox1, combobox2;
JTextArea textarea;
public Demo() {
pane1 = new JPanel();// 面板
pane2 = new JPanel();
pane3 = new JPanel();
pane4 = new JPanel();
pane5 = new JPanel();
// 设置pane1的边框和标题
pane1.setBorder(new TitledBorder(new EtchedBorder(), "基本信息"));
label1 = new JLabel("姓名:");
text1 = new JTextField(10);// 文本框
label2 = new JLabel("性别:");
// 下拉框
String[] xb = { "男", "女" };
combobox = new JComboBox(xb);
// 设置pane2的边框和标题
pane2.setBorder(new TitledBorder(new EtchedBorder(), "籍贯"));
label3 = new JLabel("省份:");
String[] box1 = { "湖南", "北京", "上海", "河南" };
combobox1 = new JComboBox(box1);
label4 = new JLabel("市区:");
String[] box2 = { "开福区", "岳麓区", "周口", "南阳", "信阳" };
combobox2 = new JComboBox(box2);
// 设置pane3的边框和标题
pane3.setBorder(new TitledBorder(new EtchedBorder(), "通讯地址"));
label5 = new JLabel("通讯地址:");
text2 = new JTextField(10);
label6 = new JLabel("邮箱:");
text3 = new JTextField(10);
// 信息展示
textarea = new JTextArea(30, 24);
button1 = new JButton("保存");
button2 = new JButton("取消");
// 按钮制作监听器
button1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
String label1 = text1.getText().trim();
String label5 = text2.getText().trim();
String label6 = text3.getText().trim();
text1.setText("");
text2.setText("");
text3.setText("");
// getSelectedItem() 返回当前所选项 jComboBox.getSelectedItem()
textarea.append("姓名:" + label1 + "\r\n性别:" + combobox.getSelectedItem() + "\r\n省份:"
+ combobox1.getSelectedItem() + "\r\n市区:" + combobox2.getSelectedItem() + "\r\n通讯地址:" + label5
+ "\r\n邮箱:" + label6 + "\r\n");
}
});
button2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
this.setLayout(new GridLayout(6, 2, 10, 10));
pane1.add(label1);
pane1.add(text1);
pane1.add(label2);
pane1.add(combobox);
pane2.add(label3);
pane2.add(combobox1);
pane2.add(label4);
pane2.add(combobox2);
pane3.add(label5);
pane3.add(text2);
pane3.add(label6);
pane3.add(text3);
pane4.add(button1);
pane4.add(button2);
pane5.add(textarea);
this.add(pane1);
this.add(pane2);
this.add(pane3);
this.add(pane4);
this.add(pane5);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(200, 200, 500, 750);
// 按钮定义监听器
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo c = new Demo();
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.