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(); } }给这段代码排版好看一点,美化一下
时间: 2024-01-27 14:05:50 浏览: 67
好的,下面是美化过的代码:
```
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();
}
}
```
这是一个简单的登录页面,包括用户名、密码输入框、登录和退出按钮。当用户输入正确的用户名和密码时,点击登录按钮会弹出提示框,提示用户登录成功,并且打开一个六子棋的窗口;否则弹出提示框,提示用户名或密码错误。
阅读全文