使用swing编写一个登录页面,登录成功运行public class Test { public static void main(String[] args) { FiveChessFrame f=new FiveChessFrame(); } },用户名和密码都是admin,标题是六子棋
时间: 2024-02-25 07:58:31 浏览: 62
好的,下面是使用swing编写的登录页面的代码示例:
```java
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();
}
}
```
当用户输入正确的用户名和密码后,程序会弹出一个提示框,提示用户登录成功,并且创建一个六子棋的窗口。如果用户名或密码错误,则会弹出一个提示框,提示用户错误信息。
阅读全文