网上订餐java用户登录界面的代码
时间: 2023-08-11 20:07:47 浏览: 37
以下是一个简单的Java用户登录界面的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UserLogin extends JFrame implements ActionListener {
JLabel userLabel, passwordLabel;
JTextField userTextField;
JPasswordField passwordField;
JButton loginButton, resetButton;
public UserLogin() {
setTitle("用户登录");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
userLabel = new JLabel("用户名:");
passwordLabel = new JLabel("密码:");
userTextField = new JTextField(10);
passwordField = new JPasswordField(10);
loginButton = new JButton("登录");
resetButton = new JButton("重置");
JPanel panel = new JPanel(new GridLayout(3, 1));
panel.add(userLabel);
panel.add(userTextField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(loginButton);
panel.add(resetButton);
add(panel, BorderLayout.CENTER);
loginButton.addActionListener(this);
resetButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String user = userTextField.getText();
String password = new String(passwordField.getPassword());
if (user.equals("admin") && password.equals("admin123")) {
JOptionPane.showMessageDialog(this, "登录成功!");
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
}
} else if (e.getSource() == resetButton) {
userTextField.setText("");
passwordField.setText("");
}
}
public static void main(String[] args) {
UserLogin frame = new UserLogin();
frame.setVisible(true);
}
}
```
在这个示例中,我们使用了JFrame和JPanel来创建用户登录界面。该界面包括一个用户名标签和一个密码标签,以及对应的文本框和密码框,并且有一个登录按钮和一个重置按钮。当用户点击登录按钮时,程序会检查用户名和密码是否正确,并弹出相应的消息框。当用户点击重置按钮时,程序会清空用户名和密码框。