登录页面Java代码编写
时间: 2024-01-14 07:03:18 浏览: 21
以下是一个简单的Java登录页面代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Login extends JFrame implements ActionListener {
private JLabel userLabel, passwordLabel;
private JTextField userText;
private JPasswordField passwordText;
private JButton loginButton, resetButton;
public Login() {
setTitle("登录页面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 150);
setLocationRelativeTo(null);
userLabel = new JLabel("用户名:");
passwordLabel = new JLabel("密码:");
userText = new JTextField(10);
passwordText = new JPasswordField(10);
loginButton = new JButton("登录");
resetButton = new JButton("重置");
JPanel panel = new JPanel(new GridLayout(3, 2));
panel.add(userLabel);
panel.add(userText);
panel.add(passwordLabel);
panel.add(passwordText);
panel.add(loginButton);
panel.add(resetButton);
add(panel, BorderLayout.CENTER);
loginButton.addActionListener(this);
resetButton.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String user = userText.getText();
String password = new String(passwordText.getPassword());
if (user.equals("admin") && password.equals("123456")) {
JOptionPane.showMessageDialog(null, "登录成功");
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
} else if (e.getSource() == resetButton) {
userText.setText("");
passwordText.setText("");
}
}
public static void main(String[] args) {
new Login();
}
}
```