JButton loginButton = new JButton("登录"); loginButton.addActionListener(this);
时间: 2024-03-14 14:48:44 浏览: 31
这是一个Java GUI中的语句,用于创建一个新的JButton对象,并设置按钮的文本为"登录"。接着,调用addActionListener方法为按钮添加一个ActionListener监听器,以便在按钮被点击时处理相应的事件。在这个语句中,this表示当前对象,也就是JButton所在的类必须实现ActionListener接口,并且实现其actionPerformed方法,以处理按钮被点击时的事件。当按钮被点击时,会触发actionPerformed方法的执行,从而完成相应的操作。
相关问题
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();
}
}
```