swing制作一个注册登录界面
时间: 2023-12-22 14:05:05 浏览: 28
抱歉,我作为AI语言模型不支持图形界面的制作。但是,我可以提供一些基本的代码框架供您参考,您可以在此基础上进行进一步的开发和设计。
1. 注册界面代码框架
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class RegisterFrame extends JFrame implements ActionListener {
private JPanel panel;
private JLabel titleLabel;
private JLabel usernameLabel;
private JTextField usernameField;
private JLabel passwordLabel;
private JPasswordField passwordField;
private JButton registerButton;
private JButton cancelButton;
public RegisterFrame() {
initComponents();
}
private void initComponents() {
panel = new JPanel();
panel.setLayout(new GridBagLayout());
titleLabel = new JLabel("用户注册");
titleLabel.setFont(new Font("宋体", Font.BOLD, 20));
usernameLabel = new JLabel("用户名:");
passwordLabel = new JLabel("密码:");
usernameField = new JTextField(20);
passwordField = new JPasswordField(20);
registerButton = new JButton("注册");
cancelButton = new JButton("取消");
registerButton.addActionListener(this);
cancelButton.addActionListener(this);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.insets = new Insets(10, 10, 10, 10);
panel.add(titleLabel, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
panel.add(usernameLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
panel.add(usernameField, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
panel.add(passwordLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
panel.add(passwordField, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.insets = new Insets(20, 10, 10, 10);
panel.add(registerButton, gbc);
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridwidth = 2;
gbc.insets = new Insets(10, 10, 10, 10);
panel.add(cancelButton, gbc);
this.add(panel);
this.setTitle("用户注册");
this.setSize(400, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == registerButton) {
// 处理注册逻辑
} else if (e.getSource() == cancelButton) {
this.dispose();
}
}
}
```
2. 登录界面代码框架
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame extends JFrame implements ActionListener {
private JPanel panel;
private JLabel titleLabel;
private JLabel usernameLabel;
private JTextField usernameField;
private JLabel passwordLabel;
private JPasswordField passwordField;
private JButton loginButton;
private JButton cancelButton;
public LoginFrame() {
initComponents();
}
private void initComponents() {
panel = new JPanel();
panel.setLayout(new GridBagLayout());
titleLabel = new JLabel("用户登录");
titleLabel.setFont(new Font("宋体", Font.BOLD, 20));
usernameLabel = new JLabel("用户名:");
passwordLabel = new JLabel("密码:");
usernameField = new JTextField(20);
passwordField = new JPasswordField(20);
loginButton = new JButton("登录");
cancelButton = new JButton("取消");
loginButton.addActionListener(this);
cancelButton.addActionListener(this);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.insets = new Insets(10, 10, 10, 10);
panel.add(titleLabel, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
panel.add(usernameLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
panel.add(usernameField, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
panel.add(passwordLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
panel.add(passwordField, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.insets = new Insets(20, 10, 10, 10);
panel.add(loginButton, gbc);
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridwidth = 2;
gbc.insets = new Insets(10, 10, 10, 10);
panel.add(cancelButton, gbc);
this.add(panel);
this.setTitle("用户登录");
this.setSize(400, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
// 处理登录逻辑
} else if (e.getSource() == cancelButton) {
this.dispose();
}
}
}
```