java简单的登录界面
时间: 2024-05-30 20:06:46 浏览: 18
Java可以使用Swing来创建图形界面,下面是一个简单的登录界面的示例代码:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Login extends JFrame implements ActionListener {
// 定义组件
JLabel label1, label2;
JTextField txtUsername;
JPasswordField txtPassword;
JButton btnLogin, btnReset;
public Login() {
super("登录");
// 初始化组件
label1 = new JLabel("用户名:");
label2 = new JLabel("密码:");
txtUsername = new JTextField();
txtPassword = new JPasswordField();
btnLogin = new JButton("登录");
btnReset = new JButton("重置");
// 设置布局
setLayout(new GridLayout(3, 2));
// 添加组件
add(label1);
add(txtUsername);
add(label2);
add(txtPassword);
add(btnLogin);
add(btnReset);
// 设置事件监听器
btnLogin.addActionListener(this);
btnReset.addActionListener(this);
// 设置窗口大小、位置和可见性
setSize(300, 150);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnLogin) {
String username = txtUsername.getText();
String password = new String(txtPassword.getPassword());
if (username.equals("admin") && password.equals("admin")) {
JOptionPane.showMessageDialog(this, "登录成功!");
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
}
} else if (e.getSource() == btnReset) {
txtUsername.setText("");
txtPassword.setText("");
}
}
public static void main(String[] args) {
new Login();
}
}
```
以上代码使用GridLayout布局,包含一个用户名输入框、一个密码输入框、一个登录按钮和一个重置按钮。当用户点击登录按钮时,程序会检查输入的用户名和密码是否正确,如果正确则弹出提示框显示“登录成功”,否则弹出提示框显示“用户名或密码错误”。当用户点击重置按钮时,程序会清空用户名和密码输入框中的内容。
相关推荐
![](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)