Java设计一个用户登陆界面,点击确定按钮,将输入信息与用户名“amy”,密码“123456”进行比较,输入正确,则直接在窗体标签上显示“输入正确,欢迎登录”,若输入错误,直接在窗体标签显示“输入错误,请重新输入”并清空文本框中内容
时间: 2023-11-26 22:06:42 浏览: 42
下面是一个Java Swing实现的简单用户登录界面:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UserLogin extends JFrame implements ActionListener {
private JLabel userLabel, passwordLabel, messageLabel;
private JTextField userText, passwordText;
private JButton loginButton, resetButton;
public UserLogin() {
setTitle("用户登录");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
userLabel = new JLabel("用户名:");
passwordLabel = new JLabel("密码:");
messageLabel = new JLabel("");
userText = new JTextField(20);
passwordText = new JPasswordField(20);
loginButton = new JButton("确定");
resetButton = new JButton("重置");
setLayout(new GridLayout(3, 2));
add(userLabel);
add(userText);
add(passwordLabel);
add(passwordText);
add(loginButton);
add(resetButton);
loginButton.addActionListener(this);
resetButton.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String userName = userText.getText();
String password = passwordText.getText();
if (userName.equals("amy") && password.equals("123456")) {
messageLabel.setText("输入正确,欢迎登录");
} else {
messageLabel.setText("输入错误,请重新输入");
userText.setText("");
passwordText.setText("");
}
} else if (e.getSource() == resetButton) {
userText.setText("");
passwordText.setText("");
messageLabel.setText("");
}
}
public static void main(String[] args) {
new UserLogin();
}
}
```
在这个界面中,有用户名、密码的标签和输入框,以及确定和重置按钮。当用户点击登录按钮时,程序会获取输入框中的用户名和密码,与预设的用户名和密码进行比较,如果输入正确,则在窗体标签上显示“输入正确,欢迎登录”,否则显示“输入错误,请重新输入”并清空文本框中内容。当用户点击重置按钮时,程序会清空输入框中的内容和窗体标签上的文字。