java登录注册页面gui
时间: 2023-07-01 21:08:32 浏览: 51
以下是一个使用Java Swing GUI库实现的基本登录注册页面示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginGUI extends JFrame implements ActionListener {
private JLabel userLabel, passLabel;
private JTextField userText;
private JPasswordField passText;
private JButton loginButton, registerButton;
public LoginGUI() {
setTitle("Login/Register");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create components
userLabel = new JLabel("Username: ");
passLabel = new JLabel("Password: ");
userText = new JTextField(20);
passText = new JPasswordField(20);
loginButton = new JButton("Login");
registerButton = new JButton("Register");
// Add action listeners
loginButton.addActionListener(this);
registerButton.addActionListener(this);
// Create panel to hold components
JPanel panel = new JPanel(new GridLayout(3, 2));
panel.add(userLabel);
panel.add(userText);
panel.add(passLabel);
panel.add(passText);
panel.add(loginButton);
panel.add(registerButton);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
// Handle login button click
String username = userText.getText();
String password = new String(passText.getPassword());
// TODO: Check if username and password are correct
JOptionPane.showMessageDialog(this, "Logged in successfully!");
} else if (e.getSource() == registerButton) {
// Handle register button click
// TODO: Implement registration functionality
JOptionPane.showMessageDialog(this, "Registration not yet implemented.");
}
}
public static void main(String[] args) {
new LoginGUI();
}
}
```
在这个示例中,我们使用了JLabel、JTextField、JPasswordField和JButton组件来创建一个简单的登录注册界面。我们还实现了ActionListener接口来处理按钮点击事件。当用户点击“登录”按钮时,我们获取用户名和密码,并检查它们是否正确。如果用户名和密码正确,我们显示一个包含“已成功登录”的消息框。当用户点击“注册”按钮时,我们显示一个包含“尚未实现注册”消息的消息框。