编写Java应用程序,实现使用空布局完成登陆界面设计。当用户点击界面中的“登录”按钮时,获取用户输入的用户名和密码并进行校验,如果账号密码正确,弹出另外一个窗口。如果账号密码错误弹出对话框。单击重置按钮实现账号密码清空。
时间: 2024-03-19 10:43:54 浏览: 13
好的,我来帮您解答这个问题。
首先,我们需要创建一个 JFrame 窗口,并使用空布局。然后,在窗口中添加用户名和密码的 JLabel 和 JTextField,以及登录和重置的 JButton。代码如下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame extends JFrame implements ActionListener {
private JLabel userLabel, pwdLabel;
private JTextField userText, pwdText;
private JButton loginButton, resetButton;
public LoginFrame() {
setTitle("登录界面");
setSize(300, 200);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
userLabel = new JLabel("用户名:");
userLabel.setBounds(50, 30, 60, 25);
add(userLabel);
userText = new JTextField(20);
userText.setBounds(110, 30, 120, 25);
add(userText);
pwdLabel = new JLabel("密 码:");
pwdLabel.setBounds(50, 70, 60, 25);
add(pwdLabel);
pwdText = new JPasswordField(20);
pwdText.setBounds(110, 70, 120, 25);
add(pwdText);
loginButton = new JButton("登录");
loginButton.setBounds(50, 120, 80, 25);
loginButton.addActionListener(this);
add(loginButton);
resetButton = new JButton("重置");
resetButton.setBounds(150, 120, 80, 25);
resetButton.addActionListener(this);
add(resetButton);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String username = userText.getText();
String password = pwdText.getText();
if (username.equals("admin") && password.equals("123456")) {
JOptionPane.showMessageDialog(null, "登录成功!");
new MainFrame();
setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
}
} else if (e.getSource() == resetButton) {
userText.setText("");
pwdText.setText("");
}
}
}
```
在 actionPerformed 方法中,我们通过判断用户点击的是“登录”按钮还是“重置”按钮,来执行不同的操作。当用户点击“登录”按钮时,获取用户输入的用户名和密码,并进行校验。如果校验成功,弹出另外一个窗口,并关闭当前窗口。如果校验失败,弹出对话框提示错误信息。当用户点击“重置”按钮时,清空用户名和密码的文本框。
接下来,我们需要创建一个 MainFrame 窗口,作为登录成功后弹出的窗口。代码如下:
```java
import javax.swing.*;
public class MainFrame extends JFrame {
public MainFrame() {
setTitle("欢迎使用");
setSize(300, 200);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("欢迎使用本系统!");
label.setHorizontalAlignment(SwingConstants.CENTER);
add(label);
setVisible(true);
}
}
```
最后,在主函数中创建 LoginFrame 窗口即可。
```java
public class Main {
public static void main(String[] args) {
new LoginFrame();
}
}
```
好了,以上就是完整的代码。您可以将代码复制到一个 Java 项目中,编译并运行,查看效果。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)