用java 写一个职工工资管理系统的代码要求为相关信息包括有:职工号、姓名、月工资、岗位、年龄、性别等信息。 系统要求实现如下功能: 1)系统功能以菜单方式工作(用Swing设计界面); 2)数据的输入; 3)职工信息数据修改; 4)数据的列表展示; 5)数据的删除; 6)数据的查找,通常以两种方式进行查找,如姓名、记录号(或学号、职工号等); 7)退出系统。
时间: 2024-03-22 15:41:58 浏览: 25
好的,下面是一个简单的职工工资管理系统的Java代码示例,实现了您提到的功能:
```java
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class EmployeeSalarySystem {
private ArrayList<Employee> employeeList;
private JFrame mainFrame;
private JTable employeeTable;
public EmployeeSalarySystem() {
employeeList = new ArrayList<>();
initialize();
}
private void initialize() {
mainFrame = new JFrame("Employee Salary System");
mainFrame.setSize(800, 600);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create menu bar
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem exitMenuItem = new JMenuItem("Exit");
exitMenuItem.addActionListener(e -> System.exit(0));
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
mainFrame.setJMenuBar(menuBar);
// create main panel
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainFrame.add(mainPanel);
// create input panel
JPanel inputPanel = new JPanel();
inputPanel.setBorder(BorderFactory.createTitledBorder("Input Employee Info"));
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
mainPanel.add(inputPanel);
JTextField idField = new JTextField(10);
JTextField nameField = new JTextField(10);
JTextField salaryField = new JTextField(10);
JTextField positionField = new JTextField(10);
JTextField ageField = new JTextField(10);
JTextField genderField = new JTextField(10);
JPanel idPanel = new JPanel();
idPanel.add(new JLabel("ID: "));
idPanel.add(idField);
inputPanel.add(idPanel);
JPanel namePanel = new JPanel();
namePanel.add(new JLabel("Name: "));
namePanel.add(nameField);
inputPanel.add(namePanel);
JPanel salaryPanel = new JPanel();
salaryPanel.add(new JLabel("Salary: "));
salaryPanel.add(salaryField);
inputPanel.add(salaryPanel);
JPanel positionPanel = new JPanel();
positionPanel.add(new JLabel("Position: "));
positionPanel.add(positionField);
inputPanel.add(positionPanel);
JPanel agePanel = new JPanel();
agePanel.add(new JLabel("Age: "));
agePanel.add(ageField);
inputPanel.add(agePanel);
JPanel genderPanel = new JPanel();
genderPanel.add(new JLabel("Gender: "));
genderPanel.add(genderField);
inputPanel.add(genderPanel);
JButton addButton = new JButton("Add");
addButton.addActionListener(e -> {
Employee employee = new Employee(
Integer.parseInt(idField.getText()),
nameField.getText(),
Double.parseDouble(salaryField.getText()),
positionField.getText(),
Integer.parseInt(ageField.getText()),
genderField.getText());
employeeList.add(employee);
refreshTable();
clearInputFields();
});
inputPanel.add(addButton);
// create table panel
JPanel tablePanel = new JPanel();
tablePanel.setBorder(BorderFactory.createTitledBorder("Employee List"));
tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.Y_AXIS));
mainPanel.add(tablePanel);
String[] columnNames = {"ID", "Name", "Salary", "Position", "Age", "Gender"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
employeeTable = new JTable(model);
JScrollPane scrollPane = new JScrollPane(employeeTable);
tablePanel.add(scrollPane);
// create button panel
JPanel buttonPanel = new JPanel();
mainPanel.add(buttonPanel);
JButton editButton = new JButton("Edit");
editButton.addActionListener(e -> {
if (employeeTable.getSelectedRow() != -1) {
int selectedRow = employeeTable.getSelectedRow();
Employee employee = employeeList.get(selectedRow);
idField.setText(String.valueOf(employee.getId()));
nameField.setText(employee.getName());
salaryField.setText(String.valueOf(employee.getSalary()));
positionField.setText(employee.getPosition());
ageField.setText(String.valueOf(employee.getAge()));
genderField.setText(employee.getGender());
}
});
buttonPanel.add(editButton);
JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(e -> {
if (employeeTable.getSelectedRow() != -1) {
int selectedRow = employeeTable.getSelectedRow();
employeeList.remove(selectedRow);
refreshTable();
}
});
buttonPanel.add(deleteButton);
JButton searchNameButton = new JButton("Search by Name");
searchNameButton.addActionListener(e -> {
String name = JOptionPane.showInputDialog(null, "Enter Name to Search:");
if (name != null) {
for (int i = 0; i < employeeList.size(); i++) {
if (name.equals(employeeList.get(i).getName())) {
employeeTable.setRowSelectionInterval(i, i);
return;
}
}
JOptionPane.showMessageDialog(null, "Employee with Name " + name + " not found!");
}
});
buttonPanel.add(searchNameButton);
JButton searchIdButton = new JButton("Search by ID");
searchIdButton.addActionListener(e -> {
String id = JOptionPane.showInputDialog(null, "Enter ID to Search:");
if (id != null) {
for (int i = 0; i < employeeList.size(); i++) {
if (id.equals(String.valueOf(employeeList.get(i).getId()))) {
employeeTable.setRowSelectionInterval(i, i);
return;
}
}
JOptionPane.showMessageDialog(null, "Employee with ID " + id + " not found!");
}
});
buttonPanel.add(searchIdButton);
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(e -> clearInputFields());
buttonPanel.add(clearButton);
mainFrame.setVisible(true);
}
private void refreshTable() {
DefaultTableModel model = (DefaultTableModel) employeeTable.getModel();
model.setRowCount(0);
for (Employee employee : employeeList) {
Object[] rowData = {employee.getId(), employee.getName(), employee.getSalary(),
employee.getPosition(), employee.getAge(), employee.getGender()};
model.addRow(rowData);
}
}
private void clearInputFields() {
JTextField[] fields = {idField, nameField, salaryField, positionField, ageField, genderField};
for (JTextField field : fields) {
field.setText("");
}
}
public static void main(String[] args) {
new EmployeeSalarySystem();
}
}
class Employee {
private int id;
private String name;
private double salary;
private String position;
private int age;
private String gender;
public Employee(int id, String name, double salary, String position, int age, String gender) {
this.id = id;
this.name = name;
this.salary = salary;
this.position = position;
this.age = age;
this.gender = gender;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public String getPosition() {
return position;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
}
```
这个示例代码使用了Swing库来实现窗口界面和交互,使用了JTable来显示职工数据列表。您可以根据需要对代码进行修改和完善。