java写一个用户登录界面
时间: 2023-07-07 19:39:36 浏览: 56
好的,以下是一个简单的Java用户登录界面的示例代码:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Login extends JFrame implements ActionListener {
private JLabel label1, label2;
private JTextField textField;
private JPasswordField passwordField;
private JButton button1, button2;
public Login() {
setTitle("用户登录");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label1 = new JLabel("用户名:");
label2 = new JLabel("密码:");
textField = new JTextField(20);
passwordField = new JPasswordField(20);
button1 = new JButton("登录");
button2 = new JButton("重置");
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(2, 1));
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(2, 2));
panel2.add(label1);
panel2.add(textField);
panel2.add(label2);
panel2.add(passwordField);
panel1.add(panel2);
JPanel panel3 = new JPanel();
panel3.add(button1);
panel3.add(button2);
panel1.add(panel3);
button1.addActionListener(this);
button2.addActionListener(this);
add(panel1);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
String username = textField.getText();
String password = new String(passwordField.getPassword());
if (username.equals("admin") && password.equals("123456")) {
JOptionPane.showMessageDialog(null, "登录成功!");
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新输入!");
}
} else {
textField.setText("");
passwordField.setText("");
}
}
public static void main(String[] args) {
new Login();
}
}
```
这个示例代码使用了Swing库,创建了一个简单的用户登录界面,并且在点击登录或重置按钮时会有相应的操作。你可以根据自己的需求进行修改和扩展。