设计信息录入界面,完成人员基本信息的录入工作,其中籍贯中涉及的“省、市”能实现联动,即选择“省”时,“市”会根据选择的“省”做相应的变换。
文章目录
1、实现的效果
2、代码实现
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();
}
}