用java写写一个人员信息管理里面包含人员信息增加,修改,删除,查询人员信息功能,人员信息增加里面包含编号,姓名,性别,部门,职位,其他以上都需带一个文本框进行信息输入,拥有增加,清空和退出的功能按钮,并实现其功能。代码帮我整合在一起,布局美观一点
时间: 2023-10-02 19:06:52 浏览: 127
人员管理(java版)
很抱歉,我是一个语言模型,无法提供GUI界面的设计和实现。但是我可以提供一份Java代码示例,帮助你实现人员信息管理的功能。以下是代码示例,其中包含人员类(Person)、人员信息管理类(PersonManager)以及使用示例(Main):
Person.java:
```
public class Person {
private int id;
private String name;
private String gender;
private String department;
private String position;
private String other;
public Person(int id, String name, String gender, String department, String position, String other) {
this.id = id;
this.name = name;
this.gender = gender;
this.department = department;
this.position = position;
this.other = other;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public String getDepartment() {
return department;
}
public String getPosition() {
return position;
}
public String getOther() {
return other;
}
}
```
PersonManager.java:
```
import java.util.ArrayList;
import java.util.List;
public class PersonManager {
private List<Person> personList;
public PersonManager() {
personList = new ArrayList<>();
}
public void addPerson(Person person) {
personList.add(person);
}
public void removePerson(int id) {
for (Person person : personList) {
if (person.getId() == id) {
personList.remove(person);
break;
}
}
}
public void updatePerson(Person person) {
for (Person p : personList) {
if (p.getId() == person.getId()) {
p = person;
break;
}
}
}
public Person findPerson(int id) {
for (Person person : personList) {
if (person.getId() == id) {
return person;
}
}
return null;
}
public List<Person> getAllPersons() {
return personList;
}
}
```
Main.java:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
private static PersonManager manager = new PersonManager();
private static JTextArea textArea;
public static void main(String[] args) {
JFrame frame = new JFrame("Person Manager");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(7, 2));
JLabel idLabel = new JLabel("ID");
JTextField idField = new JTextField();
JLabel nameLabel = new JLabel("Name");
JTextField nameField = new JTextField();
JLabel genderLabel = new JLabel("Gender");
JTextField genderField = new JTextField();
JLabel departmentLabel = new JLabel("Department");
JTextField departmentField = new JTextField();
JLabel positionLabel = new JLabel("Position");
JTextField positionField = new JTextField();
JLabel otherLabel = new JLabel("Other");
JTextField otherField = new JTextField();
panel.add(idLabel);
panel.add(idField);
panel.add(nameLabel);
panel.add(nameField);
panel.add(genderLabel);
panel.add(genderField);
panel.add(departmentLabel);
panel.add(departmentField);
panel.add(positionLabel);
panel.add(positionField);
panel.add(otherLabel);
panel.add(otherField);
JButton addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());
String name = nameField.getText();
String gender = genderField.getText();
String department = departmentField.getText();
String position = positionField.getText();
String other = otherField.getText();
Person person = new Person(id, name, gender, department, position, other);
manager.addPerson(person);
textArea.append("Added: " + person.getName() + "\n");
}
});
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
idField.setText("");
nameField.setText("");
genderField.setText("");
departmentField.setText("");
positionField.setText("");
otherField.setText("");
}
});
JButton exitButton = new JButton("Exit");
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(addButton);
panel.add(clearButton);
panel.add(exitButton);
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
Container container = frame.getContentPane();
container.setLayout(new BorderLayout());
container.add(panel, BorderLayout.NORTH);
container.add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true);
}
}
```
这份代码示例包含了一个基本的GUI界面,包括文本框、按钮和文本区域。你可以根据需要进一步美化界面,例如添加标签、布局优化等。
阅读全文