📄 loginframe.java
字号:
package loginInterface;
import java.awt.*;
import javax.swing.*;
import data.*;
import java.awt.event.*;
import java.awt.Rectangle;
public class LoginFrame extends JFrame implements ActionListener {
//初始化部件
JPanel pnlMain;
JLabel lblLogin, lblUserName, lblPwd;
JTextField txtUserName;
JPasswordField pwdPassword;
JButton btnLogin, btnCancel;
//构造函数
public LoginFrame() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
public void jbInit() throws Exception {
//取得窗口面板
pnlMain = (JPanel)this.getContentPane();
pnlMain.setLayout(null);
//定义窗口的大小和标题
this.setSize(new Dimension(350, 300));
this.setTitle("登录模块");
//定义标签的标题
lblLogin = new JLabel("人事管理系统");
lblLogin.setFont(new Font("黑体", Font.BOLD, 24));
lblUserName = new JLabel("用户名:");
txtUserName = new JTextField("");
lblPwd = new JLabel("密 码:");
pwdPassword = new JPasswordField("");
btnLogin = new JButton("登录");
btnCancel = new JButton("取消");
Font font = new Font("宋体", Font.PLAIN, 14);
lblUserName.setFont(font);
lblUserName.setForeground(Color.blue);
lblPwd.setFont(font);
lblPwd.setForeground(Color.blue);
btnLogin.setFont(font);
btnLogin.setForeground(Color.blue);
btnCancel.setFont(font);
btnCancel.setForeground(Color.blue);
lblLogin.setForeground(Color.green);
//设置组件位置
lblLogin.setBounds(new Rectangle(92, 30, 150, 40));
lblUserName.setBounds(new Rectangle(67, 103, 81, 16));
txtUserName.setCaretColor(Color.green);
txtUserName.setBounds(new Rectangle(137, 102, 132, 22));
lblPwd.setBounds(new Rectangle(68, 159, 79, 16));
pwdPassword.setCaretColor(Color.green);
pwdPassword.setBounds(new Rectangle(138, 156, 132, 22));
btnLogin.setBackground(Color.cyan);
btnLogin.setBounds(new Rectangle(29, 213, 109, 25));
btnCancel.setBackground(Color.cyan);
btnCancel.setBounds(new Rectangle(201, 214, 109, 25));
pnlMain.add(lblLogin);
pnlMain.add(btnCancel);
pnlMain.add(btnLogin);
pnlMain.add(txtUserName);
pnlMain.add(lblUserName);
pnlMain.add(pwdPassword);
pnlMain.add(lblPwd);
btnLogin.addActionListener(this);
btnCancel.addActionListener(this);
}
//按钮执行事件
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnLogin) {
String name = txtUserName.getText().trim();
String pwd = new String(pwdPassword.getPassword());
LoginData login = new LoginData();
boolean check = login.checkUser(name, pwd);
//显示提示信息
if (check) {
JOptionPane.showMessageDialog(null, "登录成功");
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
}
}
if (ae.getSource().equals(btnCancel)) {
//清空内存
System.exit(0);
}
}
//添加窗口关闭事件
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -