用JAVA实现简单的登录页面
时间: 2024-05-05 09:21:10 浏览: 10
以下是一个简单的Java登录页面的示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Login extends JFrame implements ActionListener {
JLabel userLabel, passwordLabel;
JTextField userText;
JPasswordField passwordText;
JButton loginButton, resetButton;
Login() {
setTitle("Login");
setSize(300, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
userLabel = new JLabel("Username:");
passwordLabel = new JLabel("Password:");
userText = new JTextField(10);
passwordText = new JPasswordField(10);
loginButton = new JButton("Login");
resetButton = new JButton("Reset");
loginButton.addActionListener(this);
resetButton.addActionListener(this);
JPanel panel = new JPanel(new GridLayout(3, 2));
panel.add(userLabel);
panel.add(userText);
panel.add(passwordLabel);
panel.add(passwordText);
panel.add(loginButton);
panel.add(resetButton);
add(panel, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String user = userText.getText();
String password = new String(passwordText.getPassword());
if (user.equals("admin") && password.equals("admin123")) {
JOptionPane.showMessageDialog(this, "Login successful!");
} else {
JOptionPane.showMessageDialog(this, "Invalid username or password!");
}
} else if (e.getSource() == resetButton) {
userText.setText("");
passwordText.setText("");
}
}
public static void main(String[] args) {
new Login();
}
}
```
这个登录页面包含了一个用户名和密码输入框,以及一个“登录”按钮和一个“重置”按钮。当用户点击“登录”按钮时,程序会检查用户名和密码是否正确,如果正确则显示一个登录成功的消息框,否则显示一个错误消息框。当用户点击“重置”按钮时,程序会清空用户名和密码输入框中的内容。
相关推荐
![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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.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_column_c1.png)