帮我用Java写一个登陆界面
时间: 2023-03-27 19:04:33 浏览: 106
好的,我可以为您提供一个基本的 Java 登录界面代码示例:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoginFrame extends JFrame implements ActionListener {
private JLabel userLabel, passwordLabel;
private JTextField userText;
private JPasswordField passwordText;
private JButton loginButton, resetButton;
public LoginFrame() {
setTitle("登录");
setSize(300, 200);
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);
resetButton = new JButton("重置");
resetButton.addActionListener(this);
panel.add(resetButton);
add(panel, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String user = userText.getText();
String password = new String(passwordText.getPassword());
if (user.equals("admin") && password.equals("123456")) {
JOptionPane.showMessageDialog(this, "登录成功!");
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
}
} else if (e.getSource() == resetButton) {
userText.setText("");
passwordText.setText("");
}
}
public static void main(String[] args) {
new LoginFrame();
}
}
```
这是一个简单的登录界面,包含用户名、密码输入框和登录、重置按钮。当用户点击登录按钮时,程序会判断用户名和密码是否正确,如果正确则弹出登录成功的提示框,否则弹出用户名或密码错误的提示框。当用户点击重置按钮时,程序会清空用户名和密码输入框。
相关推荐
![](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)