帮我用Java和gui写一个用户管理系统界面
时间: 2023-11-11 07:07:42 浏览: 56
好的,下面是一个简单的用户管理系统界面的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UserManagementSystemGUI extends JFrame implements ActionListener {
private JTextField nameField;
private JTextField ageField;
private JTextField emailField;
private JButton addButton;
private JButton clearButton;
private JTextArea userArea;
public UserManagementSystemGUI() {
setTitle("用户管理系统");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel nameLabel = new JLabel("姓名:");
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.WEST;
panel.add(nameLabel, c);
nameField = new JTextField(20);
c.gridx = 1;
c.gridy = 0;
c.anchor = GridBagConstraints.EAST;
panel.add(nameField, c);
JLabel ageLabel = new JLabel("年龄:");
c.gridx = 0;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
panel.add(ageLabel, c);
ageField = new JTextField(20);
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.EAST;
panel.add(ageField, c);
JLabel emailLabel = new JLabel("邮箱:");
c.gridx = 0;
c.gridy = 2;
c.anchor = GridBagConstraints.WEST;
panel.add(emailLabel, c);
emailField = new JTextField(20);
c.gridx = 1;
c.gridy = 2;
c.anchor = GridBagConstraints.EAST;
panel.add(emailField, c);
addButton = new JButton("添加用户");
addButton.addActionListener(this);
c.gridx = 0;
c.gridy = 3;
c.anchor = GridBagConstraints.CENTER;
panel.add(addButton, c);
clearButton = new JButton("清空");
clearButton.addActionListener(this);
c.gridx = 1;
c.gridy = 3;
c.anchor = GridBagConstraints.CENTER;
panel.add(clearButton, c);
userArea = new JTextArea(15, 40);
JScrollPane scrollPane = new JScrollPane(userArea);
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 2;
c.anchor = GridBagConstraints.CENTER;
panel.add(scrollPane, c);
add(panel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String name = nameField.getText();
String age = ageField.getText();
String email = emailField.getText();
String user = "姓名:" + name + ",年龄:" + age + ",邮箱:" + email + "\n";
userArea.append(user);
nameField.setText("");
ageField.setText("");
emailField.setText("");
} else if (e.getSource() == clearButton) {
userArea.setText("");
nameField.setText("");
ageField.setText("");
emailField.setText("");
}
}
public static void main(String[] args) {
new UserManagementSystemGUI();
}
}
```
这个界面包含三个文本框和两个按钮。你可以在文本框中输入姓名、年龄和邮箱,然后点击“添加用户”按钮将用户信息添加到文本区域中。如果你想要清空所有输入和输出,可以点击“清空”按钮。
阅读全文
相关推荐















