java实现用户登录页面
时间: 2023-11-17 22:08:16 浏览: 32
以下是一个简单的Java用户登录页面的实现示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UserLogin extends JFrame implements ActionListener {
private JLabel userLabel, passLabel;
private JTextField userText;
private JPasswordField passText;
private JButton loginButton, cancelButton;
public UserLogin() {
setTitle("用户登录");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建组件
userLabel = new JLabel("用户名:");
passLabel = new JLabel("密码:");
userText = new JTextField(20);
passText = new JPasswordField(20);
loginButton = new JButton("登录");
cancelButton = new JButton("取消");
// 设置布局
setLayout(new GridLayout(3, 2));
// 添加组件
add(userLabel);
add(userText);
add(passLabel);
add(passText);
add(loginButton);
add(cancelButton);
// 添加按钮事件监听器
loginButton.addActionListener(this);
cancelButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
// 获取用户输入的用户名和密码
String username = userText.getText();
String password = new String(passText.getPassword());
// 进行用户验证
if (username.equals("admin") && password.equals("123456")) {
JOptionPane.showMessageDialog(this, "登录成功!");
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
}
} else if (e.getSource() == cancelButton) {
// 清空用户输入
userText.setText("");
passText.setText("");
}
}
public static void main(String[] args) {
UserLogin login = new UserLogin();
login.setVisible(true);
}
}
```
这个示例创建了一个简单的用户登录窗口,包含用户名和密码的输入框以及登录和取消按钮。当用户点击登录按钮时,程序获取用户输入的用户名和密码,然后进行验证。如果用户名和密码正确,程序弹出一个消息框显示登录成功;否则弹出一个消息框显示用户名或密码错误。如果用户点击取消按钮,程序清空用户名和密码的输入框。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)