📄 loginsystem.java
字号:
//LoginSystem.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class LoginSystem extends JFrame implements ActionListener {
private JLabel label_u;
private JLabel label_p;
private JButton button_ok;
private JButton button_cancel;
private JPasswordField passwordField;
private JTextField textField;
private Container con;
// 产生登录界面
public LoginSystem() {
super("登录界面");
setSize(214, 143);
// setAlwaysOnTop(true);
con = getContentPane();
con.setBackground(new Color(129, 249, 252));
con.setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label_u = new JLabel();
label_u.setFont(new Font("", Font.PLAIN, 14));
label_u.setText("用户名");
label_u.setBounds(10, 10, 60, 20);
con.add(label_u);
label_p = new JLabel();
label_p.setFont(new Font("", Font.PLAIN, 14));
label_p.setText("密码");
label_p.setBounds(10, 46, 60, 20);
con.add(label_p);
textField = new JTextField();
textField.setBounds(76, 10, 120, 20);
con.add(textField);
passwordField = new JPasswordField();
passwordField.setBounds(76, 46, 120, 20);
passwordField.setEchoChar('*');
con.add(passwordField);
button_ok = new JButton();
button_ok.setText("确定");
button_ok.setBounds(29, 77, 60, 23);
con.add(button_ok);
button_cancel = new JButton();
button_cancel.setText("取消");
button_cancel.setBounds(119, 77, 60, 23);
con.add(button_cancel);
passwordField.addActionListener(this);
button_ok.addActionListener(this);
button_cancel.addActionListener(this);
// 将窗口置于屏幕中央
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button_ok || e.getSource() == passwordField) {
// 取得用户输入的用户名
String tempUser = textField.getText();
// 取得用户输入的密码
char temp[] = passwordField.getPassword();
String tempPass = new String(temp);
CheckUser checkUser = new CheckUser();
if (checkUser.isValidUser(tempUser, tempPass) == false) {
passwordField.setText("");
// 弹出警告对话框
JOptionPane.showMessageDialog(null, "用户名或密码不正确", "错误",
JOptionPane.ERROR_MESSAGE);
} else {
// 初始化主界面
setVisible(false);
int flag = checkUser.getUserType(tempUser);
new MainFrame(tempUser, flag);
}
} else if (e.getSource() == button_cancel) {
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -