javagui登录界面设计
时间: 2023-10-09 18:04:13 浏览: 65
以下是一个简单的Java GUI登录界面设计,包含用户名、密码和登录按钮:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoginGUI extends JFrame implements ActionListener {
private JLabel userLabel, passLabel;
private JTextField userText;
private JPasswordField passText;
private JButton loginButton;
public LoginGUI() {
setTitle("登录");
setSize(300, 150);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
userLabel = new JLabel("用户名:");
userText = new JTextField();
passLabel = new JLabel("密 码:");
passText = new JPasswordField();
loginButton = new JButton("登录");
loginButton.addActionListener(this);
JPanel panel = new JPanel(new GridLayout(3, 2));
panel.add(userLabel);
panel.add(userText);
panel.add(passLabel);
panel.add(passText);
panel.add(new JLabel(""));
panel.add(loginButton);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String user = userText.getText();
String pass = new String(passText.getPassword());
if (user.equals("admin") && pass.equals("password")) {
JOptionPane.showMessageDialog(this, "登录成功!");
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
}
}
public static void main(String[] args) {
new LoginGUI();
}
}
```
运行程序后,将会显示如下的登录界面:
![login_gui](https://user-images.githubusercontent.com/12789449/129474752-5e0d8bc6-0af1-4c38-9a4a-6e7c34c081dc.png)
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)