GUI登陆界面实现代码
时间: 2024-06-12 17:09:50 浏览: 6
以下是一个简单的Java代码示例,用于实现一个GUI登录界面:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginGUI extends JFrame {
private JTextField usernameTextField;
private JPasswordField passwordField;
private JButton loginButton;
private JButton resetButton;
public LoginGUI() {
setTitle("登录界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
JLabel usernameLabel = new JLabel("用户名:");
JLabel passwordLabel = new JLabel("密码:");
usernameTextField = new JTextField();
passwordField = new JPasswordField();
loginButton = new JButton("登录");
resetButton = new JButton("重置");
panel.add(usernameLabel);
panel.add(usernameTextField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(loginButton);
panel.add(resetButton);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameTextField.getText();
String password = new String(passwordField.getPassword());
if (username.equals("admin") && password.equals("123456")) {
JOptionPane.showMessageDialog(null, "登录成功");
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
}
});
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
usernameTextField.setText("");
passwordField.setText("");
}
});
add(panel);
setVisible(true);
}
public static void main(String[] args) {
new LoginGUI();
}
}
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)