Java若用户名称为“www”,用户密码为“888”,单击登录时控制台显示登录成功,否则显示登录失败,单击取消退出应用程序。
时间: 2023-12-31 13:03:37 浏览: 33
以下是一个简单的Java Swing界面实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame extends JFrame implements ActionListener {
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton;
private JButton cancelButton;
public LoginFrame() {
setTitle("登录界面");
setSize(300, 150);
setLocationRelativeTo(null);
JPanel panel = new JPanel(new GridLayout(3, 2));
JLabel label1 = new JLabel("用户名:");
panel.add(label1);
usernameField = new JTextField();
panel.add(usernameField);
JLabel label2 = new JLabel("密码:");
panel.add(label2);
passwordField = new JPasswordField();
panel.add(passwordField);
loginButton = new JButton("登录");
loginButton.addActionListener(this);
panel.add(loginButton);
cancelButton = new JButton("取消");
cancelButton.addActionListener(this);
panel.add(cancelButton);
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String username = usernameField.getText();
String password = String.valueOf(passwordField.getPassword());
if (username.equals("www") && password.equals("888")) {
System.out.println("登录成功");
} else {
System.out.println("登录失败");
}
} else if (e.getSource() == cancelButton) {
System.exit(0);
}
}
public static void main(String[] args) {
new LoginFrame();
}
}
```
以上代码创建了一个简单的登录界面,当用户输入用户名和密码后,单击登录按钮会检查用户名和密码是否正确,如果正确则在控制台输出“登录成功”,否则输出“登录失败”。当用户单击取消按钮时,应用程序会关闭。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)