用java写一个人员信息增加里面包含编号,姓名,性别,部门,职位,其他以上都需带一个文本框进行信息输入,拥有增加,清空和退出的功能按钮,并实现其功能。代码帮我整合在一起
时间: 2023-10-02 13:06:52 浏览: 144
好的,以下是Java代码实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UserInfoAdd extends JFrame implements ActionListener {
JLabel labelNo, labelName, labelGender, labelDept, labelPosition, labelOther;
JTextField textNo, textName, textGender, textDept, textPosition, textOther;
JButton buttonAdd, buttonClear, buttonExit;
public UserInfoAdd() {
super("人员信息增加");
labelNo = new JLabel("编号");
labelName = new JLabel("姓名");
labelGender = new JLabel("性别");
labelDept = new JLabel("部门");
labelPosition = new JLabel("职位");
labelOther = new JLabel("其他");
textNo = new JTextField(10);
textName = new JTextField(10);
textGender = new JTextField(10);
textDept = new JTextField(10);
textPosition = new JTextField(10);
textOther = new JTextField(10);
buttonAdd = new JButton("增加");
buttonClear = new JButton("清空");
buttonExit = new JButton("退出");
JPanel panelNo = new JPanel();
panelNo.add(labelNo);
panelNo.add(textNo);
JPanel panelName = new JPanel();
panelName.add(labelName);
panelName.add(textName);
JPanel panelGender = new JPanel();
panelGender.add(labelGender);
panelGender.add(textGender);
JPanel panelDept = new JPanel();
panelDept.add(labelDept);
panelDept.add(textDept);
JPanel panelPosition = new JPanel();
panelPosition.add(labelPosition);
panelPosition.add(textPosition);
JPanel panelOther = new JPanel();
panelOther.add(labelOther);
panelOther.add(textOther);
JPanel panelButton = new JPanel();
panelButton.add(buttonAdd);
panelButton.add(buttonClear);
panelButton.add(buttonExit);
JPanel panelMain = new JPanel(new GridLayout(6, 1));
panelMain.add(panelNo);
panelMain.add(panelName);
panelMain.add(panelGender);
panelMain.add(panelDept);
panelMain.add(panelPosition);
panelMain.add(panelOther);
add(panelMain, BorderLayout.CENTER);
add(panelButton, BorderLayout.SOUTH);
buttonAdd.addActionListener(this);
buttonClear.addActionListener(this);
buttonExit.addActionListener(this);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == buttonAdd) {
String no = textNo.getText();
String name = textName.getText();
String gender = textGender.getText();
String dept = textDept.getText();
String position = textPosition.getText();
String other = textOther.getText();
// TODO: 将信息保存到文件或数据库中
System.out.println("编号:" + no + ",姓名:" + name + ",性别:" + gender + ",部门:" + dept + ",职位:" + position + ",其他:" + other);
} else if (event.getSource() == buttonClear) {
textNo.setText("");
textName.setText("");
textGender.setText("");
textDept.setText("");
textPosition.setText("");
textOther.setText("");
} else if (event.getSource() == buttonExit) {
System.exit(0);
}
}
public static void main(String[] args) {
new UserInfoAdd();
}
}
```
这是一个简单的窗口应用程序,窗口中包含6个文本框和3个按钮,实现了增加、清空和退出的功能。这里还没有实现将信息保存到文件或数据库中,你可以根据自己的需求添加相应的代码。
阅读全文