用JAVA实现简单用户登录页面
时间: 2023-09-24 09:14:26 浏览: 45
以下是一个简单的用户登录页面的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 userTextField;
private JPasswordField passTextField;
private JButton loginButton;
public UserLogin() {
setTitle("用户登录");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
userLabel = new JLabel("用户名:");
passLabel = new JLabel("密码:");
userTextField = new JTextField(20);
passTextField = new JPasswordField(20);
loginButton = new JButton("登录");
JPanel panel = new JPanel(new GridLayout(3, 1));
panel.add(userLabel);
panel.add(userTextField);
panel.add(passLabel);
panel.add(passTextField);
panel.add(loginButton);
loginButton.addActionListener(this);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String username = userTextField.getText();
String password = new String(passTextField.getPassword());
if (username.equals("admin") && password.equals("admin")) {
JOptionPane.showMessageDialog(this, "登录成功!");
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
}
}
public static void main(String[] args) {
new UserLogin();
}
}
```
运行代码后,会弹出一个包含用户名、密码输入框和登录按钮的窗口,用户输入正确的用户名和密码,点击登录按钮即可登录成功。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)