纯java实现用户登陆界面
时间: 2023-06-21 17:09:37 浏览: 118
以下是一个简单的Java Swing实现用户登录界面的示例代码:
```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, cancelButton;
public Login() {
setTitle("用户登录");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
userLabel = new JLabel("用户名:");
panel.add(userLabel);
userText = new JTextField(20);
panel.add(userText);
passwordLabel = new JLabel("密码:");
panel.add(passwordLabel);
passwordText = new JPasswordField(20);
panel.add(passwordText);
loginButton = new JButton("登录");
loginButton.addActionListener(this);
panel.add(loginButton);
cancelButton = new JButton("取消");
cancelButton.addActionListener(this);
panel.add(cancelButton);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String user = userText.getText();
String password = new String(passwordText.getPassword());
// 进行用户登录验证,此处省略具体实现
JOptionPane.showMessageDialog(this, "登录成功!");
} else if (e.getSource() == cancelButton) {
System.exit(0);
}
}
public static void main(String[] args) {
new Login();
}
}
```
该示例代码使用了Java Swing库实现一个简单的用户登录界面,包含了用户名、密码输入框、登录按钮和取消按钮。登录按钮与取消按钮均添加了事件监听器,当用户点击登录按钮时会进行用户登录验证,验证成功后会弹出一个提示框显示“登录成功”,当用户点击取消按钮时会退出程序。
阅读全文