java实现简单的登录界面代码
时间: 2023-12-07 19:03:23 浏览: 36
好的,以下是一个简单的 Java 登录界面的示例代码:
```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 static final long serialVersionUID = 1L;
private JPanel panel;
private JLabel userLabel, passwordLabel, messageLabel;
private JTextField userNameText;
private JPasswordField passwordText;
private JButton loginButton, resetButton;
public LoginGUI() {
setTitle("Login Form");
userLabel = new JLabel("Username");
passwordLabel = new JLabel("Password");
userNameText = new JTextField(20);
passwordText = new JPasswordField(20);
loginButton = new JButton("Login");
resetButton = new JButton("Reset");
messageLabel = new JLabel();
panel = new JPanel(new GridLayout(3, 1));
panel.add(userLabel);
panel.add(userNameText);
panel.add(passwordLabel);
panel.add(passwordText);
panel.add(loginButton);
panel.add(resetButton);
panel.add(messageLabel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginButton.addActionListener(this);
resetButton.addActionListener(this);
add(panel, BorderLayout.CENTER);
pack();
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String userName = userNameText.getText();
String password = String.valueOf(passwordText.getPassword());
if (userName.trim().equals("") || password.trim().equals("")) {
messageLabel.setText("Username or password cannot be blank.");
} else if (userName.equals("admin") && password.equals("1234")) {
messageLabel.setText("Login successful. Welcome " + userName + "!");
} else {
messageLabel.setText("Invalid username or password.");
}
} else if (e.getSource() == resetButton) {
userNameText.setText("");
passwordText.setText("");
messageLabel.setText("");
}
}
public static void main(String[] args) {
new LoginGUI();
}
}
```
这段代码演示了如何使用 Java Swing 创建一个简单的登录界面。它包括两个文本框(一个用于用户名,一个用于密码)、两个按钮(一个用于登录,一个用于重置表单)和一个消息标签(用于向用户显示登录结果)。用户输入用户名和密码后,单击登录按钮,应用程序将检查它们是否正确并显示相应的消息。如果用户名或密码为空,则应用程序将提示用户,要求他们输入这些信息。如果登录成功,则应用程序将向用户显示欢迎消息;否则,它将向用户显示错误消息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)