用JAVA写一个登入窗口
时间: 2024-03-16 15:45:57 浏览: 70
好的,以下是一个简单的Java登陆窗口代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoginWindow extends JFrame implements ActionListener {
private JTextField usernameField;
private JPasswordField passwordField;
public LoginWindow() {
super("登陆窗口");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JLabel usernameLabel = new JLabel("用户名:");
JLabel passwordLabel = new JLabel("密码:");
usernameField = new JTextField(20);
passwordField = new JPasswordField(20);
JButton loginButton = new JButton("登陆");
loginButton.addActionListener(this);
JPanel panel = new JPanel(new GridLayout(3, 2));
panel.add(usernameLabel);
panel.add(usernameField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(new JLabel(""));
panel.add(loginButton);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (username.equals("admin") && password.equals("admin")) {
JOptionPane.showMessageDialog(this, "登陆成功");
dispose();
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误,请重试");
}
}
public static void main(String[] args) {
new LoginWindow();
}
}
```
这个登陆窗口包含了一个用户名输入框、一个密码输入框和一个登陆按钮。当用户点击登陆按钮时,程序会判断输入的用户名和密码是否正确。如果正确,弹出登陆成功的提示框并关闭窗口,否则弹出错误提示框并清空密码输入框。
阅读全文