📄 loginframe.java
字号:
/**
*
*/
package view.frame;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import view.common.GBC;
import vo.OperatorVo;
import dao.operator.OperatorDao;
import dao.operator.impl.OperatorDaoImpl;
/**
* @author lulicheng
* @version 1.0
*
*/
public class LoginFrame extends JFrame {
/**
* 主方法,初始化对象
*
* @param args
*/
public static void main(String[] args) {
// 加载外观
try {
UIManager
.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel");
} catch (Exception e) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
}
new LoginFrame("用户登录");
}
private JTextField idField = null; // 用户编号文本框
private JTextField nameField = null; // 用户名文本框
private JPasswordField pswField = null; // 用户密码文本框
private JComboBox purviewCombo;
private JButton exitButton = null; // 退出按钮
private JButton loginButton = null; // 退出按钮
// 保存用户名的静态变量
public static String username;
// 设置边框
Border etched = BorderFactory.createEtchedBorder();
/**
* 构造方法,初始化窗体
*
* @param title
*/
public LoginFrame(String title) {
// 设置窗体标题
this.setTitle(title);
// 启动连接数据库的对话框
// new ConnectDialog().getConnect();
// 初始化窗体
initialFrame();
// 设置窗体大小
this.setSize(500, 300);
// 动态获得屏幕大小
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
// 设置窗体启动的位置
this.setLocation((int) (size.getWidth() - this.getWidth()) / 2,
(int) (size.getHeight() - this.getHeight()) / 2);
this.idField.setText("1");
this.nameField.setText("刘涛");
this.pswField.setText("123");
// 设置当前窗体大小为不可改变状态
this.setResizable(false);
this.setVisible(true);
}
/**
* 初始化窗体方法
*/
public void initialFrame() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder(etched, "登录"));
panel.setLayout(new BorderLayout());
JPanel lbPanel = new JPanel();
JLabel titleLabel = new JLabel("干洗店管理系统");
lbPanel.add(titleLabel);
titleLabel.setFont(new Font(" ", Font.PLAIN, 32));
panel.add(lbPanel, BorderLayout.NORTH);
panel.add(getInfo());
this.getContentPane().add(panel, BorderLayout.CENTER);
this.getContentPane().add(getButtonPanel(), BorderLayout.SOUTH);
}
/**
* 界面布局方法
*
* @return 返回面板对象
*/
public JPanel getInfo() {
JPanel panel = new JPanel();
this.idField = new JTextField(20);
this.nameField = buildNmFld();
this.pswField = new JPasswordField(20);
panel.setLayout(new GridBagLayout());
panel.add(new JLabel("用户编号:"), new GBC(0, 0, 1, 1).
setFill(GBC.BOTH).setInset(5).setWeight(0, 0));
panel.add(idField, new GBC(1, 0, 1, 1).setFill(
GBC.BOTH).setInset(5).setWeight(0, 0));
panel.add(new JLabel("用户名:"), new GBC(0, 1, 1, 1)
.setFill(GBC.BOTH).setInset(5).setWeight(0, 0));
panel.add(nameField, new GBC(1, 1, 1, 1).setFill(
GBC.BOTH).setInset(5).setWeight(0, 0));
panel.add(new JLabel("密码:"), new GBC(0, 2, 1, 1).setFill(GBC.BOTH).setInset(5).
setWeight(0, 0));
panel.add(pswField, new GBC(1, 2, 1, 1).setFill(
GBC.BOTH).setInset(5).setWeight(0, 0));
panel.add(new JLabel("操作员权限"), new GBC(0, 3, 1, 1).setFill(
GBC.BOTH).setInset(5).setWeight(0, 0));
String[] purview = { "店长", "收银员", "系统管理员"};
purviewCombo = new JComboBox(purview);
panel.add(purviewCombo, new GBC(1, 3, 1, 1).setFill(
GBC.BOTH).setInset(5).setWeight(0, 0));
return panel;
}
/**
* 获得铵钮方法面板
*
* @return 返回面板对象
*/
public JPanel getButtonPanel() {
JPanel panel = new JPanel();
panel.add(buildLoginButton());
panel.add(buildExitButton());
return panel;
}
public JTextField buildNmFld(){
if(nameField == null){
nameField = new JTextField(20);
}
return nameField;
}
public JButton buildLoginButton() {
if (loginButton == null) {
loginButton = new JButton("登录");
loginButton.addActionListener(new LoginAction(this));
}
return loginButton;
}
public JButton buildExitButton() {
if (exitButton == null) {
exitButton = new JButton("取消");
exitButton.addActionListener(new LoginAction(this));
}
return exitButton;
}
/**
* 获得当前用户名
*
* @return 当前用户名
*/
public String getUsername() {
return this.nameField.getText().trim();
}
/**
* 构造登陆用户信息,封装在UserVo对象中
*
* @return
*/
public OperatorVo getInputValue() {
int userId = Integer.parseInt(idField.getText().trim());
String userName = nameField.getText();
String password = new String(pswField.getPassword());
return new OperatorVo(userId, userName, password);
}
}
class LoginAction implements ActionListener {
private LoginFrame loginFrame = null;
public LoginAction(LoginFrame loginFrame) {
this.loginFrame = loginFrame;
}
public void actionPerformed(ActionEvent e) {
String name = e.getActionCommand();// 得到触发事件的组件对象的名称
if (name.equals("登录")) {
OperatorVo value = loginFrame.getInputValue();
OperatorDao dao = new OperatorDaoImpl();
if (dao.findOperator(value)) {
loginFrame.dispose();
MainFrame mf = new MainFrame();
MainFrame.setOperatorName(loginFrame.buildNmFld().getText());
mf.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "输入用户名或者密码出错!");
}
}else if(name.equals("取消")){
loginFrame.dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -