用Java设计一个宿舍管理登入界面
时间: 2023-12-22 16:04:31 浏览: 76
以下是一个简单的Java Swing宿舍管理登录界面设计的例子:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DormitoryManagementLogin extends JFrame implements ActionListener {
private JLabel titleLabel, usernameLabel, passwordLabel;
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton, resetButton;
public DormitoryManagementLogin() {
super("Dormitory Management Login");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
titleLabel = new JLabel("Dormitory Management Login");
titleLabel.setFont(new Font("Arial", Font.BOLD, 20));
titleLabel.setForeground(Color.BLUE);
titleLabel.setHorizontalAlignment(JLabel.CENTER);
usernameLabel = new JLabel("Username:");
passwordLabel = new JLabel("Password:");
usernameField = new JTextField();
passwordField = new JPasswordField();
loginButton = new JButton("Login");
resetButton = new JButton("Reset");
loginButton.addActionListener(this);
resetButton.addActionListener(this);
JPanel panel = new JPanel(new GridLayout(3, 2));
panel.add(usernameLabel);
panel.add(usernameField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(loginButton);
panel.add(resetButton);
add(titleLabel, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String username = usernameField.getText();
String password = String.valueOf(passwordField.getPassword());
// 在这里添加登录验证逻辑
JOptionPane.showMessageDialog(this, "登录成功!");
// 登录成功后跳转到宿舍管理主界面
new DormitoryManagementMain();
dispose();
} else if (e.getSource() == resetButton) {
usernameField.setText("");
passwordField.setText("");
}
}
public static void main(String[] args) {
new DormitoryManagementLogin();
}
}
```
运行该程序,将会显示一个带有用户名、密码、登录和重置按钮的窗口。用户输入用户名和密码后,点击登录按钮会进行登录验证,并且在登录成功后跳转到宿舍管理主界面。
阅读全文