loginButton.addActionListener(e -> { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); if (validate(username, password)) { messageLabel.setText("登录成功!"); messageLabel.setForeground(Color.GREEN); new TextBookFrame(); this.setVisible(false); } else { messageLabel.setText("用户名或密码错误!"); messageLabel.setForeground(Color.RED); } });
时间: 2024-02-14 19:19:36 浏览: 34
这段代码是用来实现一个登录界面的功能,当用户点击登录按钮时,程序会获取用户名和密码,然后调用validate方法进行验证。如果验证通过,会显示一个“登录成功”的提示信息,并打开一个新的教材窗口(TextBookFrame),同时关闭当前登录窗口。如果验证不通过,会显示一个“用户名或密码错误”的提示信息。这段代码使用了Java Swing库来实现用户界面,其中包括JLabel、JTextField、JPasswordField和JButton等组件。在JButton上注册了一个ActionListener监听器,当用户点击按钮时,会触发ActionEvent事件,从而执行相应的代码逻辑。
相关问题
import javax.swing.; import java.awt.; import java.awt.event.*; public class Login extends JFrame implements ActionListener { private JLabel usernameLabel, passwordLabel; private JTextField usernameField; private JPasswordField passwordField; private JButton loginButton, exitButton; public Login() { super("登录页面"); this.setLayout(new GridLayout(3, 2)); usernameLabel = new JLabel("用户名:"); this.add(usernameLabel); usernameField = new JTextField(10); this.add(usernameField); passwordLabel = new JLabel("密码:"); this.add(passwordLabel); passwordField = new JPasswordField(10); this.add(passwordField); loginButton = new JButton("登录"); loginButton.addActionListener(this); this.add(loginButton); exitButton = new JButton("退出"); exitButton.addActionListener(this); this.add(exitButton); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if (e.getSource() == loginButton) { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); if (username.equals("admin") && password.equals("admin")) { JOptionPane.showMessageDialog(this, "登录成功!"); new FiveChessFrame(); this.dispose(); } else { JOptionPane.showMessageDialog(this, "用户名或密码错误!"); } } else if (e.getSource() == exitButton) { System.exit(0); } } public static void main(String[] args) { new Login(); } }给这段代码排版好看一点,美化一下
好的,下面是美化过的代码:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Login extends JFrame implements ActionListener {
private JLabel usernameLabel, passwordLabel;
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton, exitButton;
public Login() {
super("登录页面");
this.setLayout(new GridLayout(3, 2));
usernameLabel = new JLabel("用户名:");
this.add(usernameLabel);
usernameField = new JTextField(10);
this.add(usernameField);
passwordLabel = new JLabel("密码:");
this.add(passwordLabel);
passwordField = new JPasswordField(10);
this.add(passwordField);
loginButton = new JButton("登录");
loginButton.addActionListener(this);
this.add(loginButton);
exitButton = new JButton("退出");
exitButton.addActionListener(this);
this.add(exitButton);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (username.equals("admin") && password.equals("admin")) {
JOptionPane.showMessageDialog(this, "登录成功!");
new FiveChessFrame();
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
}
} else if (e.getSource() == exitButton) {
System.exit(0);
}
}
public static void main(String[] args) {
new Login();
}
}
```
这是一个简单的登录页面,包括用户名、密码输入框、登录和退出按钮。当用户输入正确的用户名和密码时,点击登录按钮会弹出提示框,提示用户登录成功,并且打开一个六子棋的窗口;否则弹出提示框,提示用户名或密码错误。
import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class DormitoryManagementLogin extends JFrame implements ActionListener { private JLabel titleLabel, usernameLabel, passwordLabel; private JTextField usernameField; private JPasswordField passwordField; private JButton loginButton, resetButton; public DormitoryManagementLogin() { super("Dormitory Management Login"); setSize(400, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); titleLabel = new JLabel("Dormitory Management Login"); titleLabel.setFont(new Font("Arial", Font.BOLD, 20)); titleLabel.setForeground(Color.BLUE); titleLabel.setHorizontalAlignment(JLabel.CENTER); usernameLabel = new JLabel("Username:"); passwordLabel = new JLabel("Password:"); usernameField = new JTextField(); passwordField = new JPasswordField(); loginButton = new JButton("Login"); resetButton = new JButton("Reset"); loginButton.addActionListener(this); resetButton.addActionListener(this); JPanel panel = new JPanel(new GridLayout(3, 2)); panel.add(usernameLabel); panel.add(usernameField); panel.add(passwordLabel); panel.add(passwordField); panel.add(loginButton); panel.add(resetButton); add(titleLabel, BorderLayout.NORTH); add(panel, BorderLayout.CENTER); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == loginButton) { String username = usernameField.getText(); String password = String.valueOf(passwordField.getPassword()); // 在这里添加登录验证逻辑 JOptionPane.showMessageDialog(this, "登录成功!"); // 登录成功后跳转到宿舍管理主界面 new DormitoryManagementMain(); dispose(); } else if (e.getSource() == resetButton) { usernameField.setText(""); passwordField.setText(""); } } public static void main(String[] args) { new DormitoryManagementLogin(); }}请修改代码错误
代码中没有明显的错误,但是为了使代码更加规范和易读,可以对其中一些部分进行优化。例如:
1. 在类名后面加上 `{}`,表示该类是一个独立的代码块。
2. 将 `JFrame.EXIT_ON_CLOSE` 改为 `WindowConstants.EXIT_ON_CLOSE`,使代码更加易读。
3. 将 `setDefaultCloseOperation(EXIT_ON_CLOSE)` 改为 `setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)`。
4. 将 `String password = String.valueOf(passwordField.getPassword())` 改为 `String password = new String(passwordField.getPassword())`,因为 `String.valueOf()` 方法返回的是 char 数组的字符串表示形式,而不是密码字符串本身。
修改后的代码如下:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DormitoryManagementLogin extends JFrame implements ActionListener {
private JLabel titleLabel, usernameLabel, passwordLabel;
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton, resetButton;
public DormitoryManagementLogin() {
super("Dormitory Management Login");
titleLabel = new JLabel("Dormitory Management Login");
titleLabel.setFont(new Font("Arial", Font.BOLD, 20));
titleLabel.setForeground(Color.BLUE);
titleLabel.setHorizontalAlignment(JLabel.CENTER);
usernameLabel = new JLabel("Username:");
passwordLabel = new JLabel("Password:");
usernameField = new JTextField();
passwordField = new JPasswordField();
loginButton = new JButton("Login");
resetButton = new JButton("Reset");
loginButton.addActionListener(this);
resetButton.addActionListener(this);
JPanel panel = new JPanel(new GridLayout(3, 2));
panel.add(usernameLabel);
panel.add(usernameField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(loginButton);
panel.add(resetButton);
add(titleLabel, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
setSize(400, 300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
// 在这里添加登录验证逻辑
JOptionPane.showMessageDialog(this, "登录成功!");
// 登录成功后跳转到宿舍管理主界面
new DormitoryManagementMain();
dispose();
} else if (e.getSource() == resetButton) {
usernameField.setText("");
passwordField.setText("");
}
}
public static void main(String[] args) {
new DormitoryManagementLogin();
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)