Java swing 用户管理系统增删查改功能代码
时间: 2023-08-18 09:09:19 浏览: 94
以下是一个简单的Java Swing用户管理系统的增删查改功能代码示例:
```java
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class UserManagementSystem {
private JFrame frame;
private JTextField nameField;
private JTextField emailField;
private JTextField ageField;
private JTable table;
private DefaultTableModel model;
private ArrayList<User> userList = new ArrayList<User>();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UserManagementSystem window = new UserManagementSystem();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public UserManagementSystem() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel nameLabel = new JLabel("Name:");
nameLabel.setBounds(10, 10, 80, 25);
panel.add(nameLabel);
nameField = new JTextField();
nameField.setBounds(100, 10, 160, 25);
panel.add(nameField);
nameField.setColumns(10);
JLabel emailLabel = new JLabel("Email:");
emailLabel.setBounds(10, 40, 80, 25);
panel.add(emailLabel);
emailField = new JTextField();
emailField.setBounds(100, 40, 160, 25);
panel.add(emailField);
emailField.setColumns(10);
JLabel ageLabel = new JLabel("Age:");
ageLabel.setBounds(10, 70, 80, 25);
panel.add(ageLabel);
ageField = new JTextField();
ageField.setBounds(100, 70, 160, 25);
panel.add(ageField);
ageField.setColumns(10);
JButton addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addUser();
}
});
addButton.setBounds(10, 100, 80, 25);
panel.add(addButton);
JButton updateButton = new JButton("Update");
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateSelectedUser();
}
});
updateButton.setBounds(100, 100, 80, 25);
panel.add(updateButton);
JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteSelectedUser();
}
});
deleteButton.setBounds(190, 100, 80, 25);
panel.add(deleteButton);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 135, 414, 116);
panel.add(scrollPane);
table = new JTable();
model = new DefaultTableModel(new Object[]{"Name", "Email", "Age"}, 0);
table.setModel(model);
scrollPane.setViewportView(table);
}
private void addUser() {
String name = nameField.getText();
String email = emailField.getText();
int age = Integer.parseInt(ageField.getText());
User user = new User(name, email, age);
userList.add(user);
model.addRow(new Object[]{name, email, age});
nameField.setText("");
emailField.setText("");
ageField.setText("");
}
private void updateSelectedUser() {
int selectedRowIndex = table.getSelectedRow();
if (selectedRowIndex >= 0) {
String name = nameField.getText();
String email = emailField.getText();
int age = Integer.parseInt(ageField.getText());
User user = userList.get(selectedRowIndex);
user.setName(name);
user.setEmail(email);
user.setAge(age);
model.setValueAt(name, selectedRowIndex, 0);
model.setValueAt(email, selectedRowIndex, 1);
model.setValueAt(age, selectedRowIndex, 2);
nameField.setText("");
emailField.setText("");
ageField.setText("");
} else {
JOptionPane.showMessageDialog(null, "Please select a user to update.");
}
}
private void deleteSelectedUser() {
int selectedRowIndex = table.getSelectedRow();
if (selectedRowIndex >= 0) {
userList.remove(selectedRowIndex);
model.removeRow(selectedRowIndex);
nameField.setText("");
emailField.setText("");
ageField.setText("");
} else {
JOptionPane.showMessageDialog(null, "Please select a user to delete.");
}
}
private class User {
private String name;
private String email;
private int age;
public User(String name, String email, int age) {
this.name = name;
this.email = email;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}
```
这个示例演示了如何使用Java Swing创建一个简单的用户管理系统,包括添加、更新、删除和查看用户。用户数据存储在一个包含用户对象的ArrayList中,每个用户对象包含名称、电子邮件和年龄属性。用户界面包括文本框、按钮和表格,表格用于显示当前用户列表。当用户添加、更新或删除时,表格数据模型会相应地进行更改。
阅读全文