📄 loginframe.java
字号:
package view.frame;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import view.com.CenterDialog;
import view.com.GBC;
import view.com.factory.MainFrameFactory;
import view.com.factory.PosFrameFactory;
import view.com.getcomponent.button.GetButton;
import dao.UserPwdDao;
/**
* @version 2008年04月 超市管理系统
* @author linfeng,tanwenjie
*/
public class LoginFrame extends JDialog implements ActionListener {
/**
* dialog 对话框 gbl GridBagLayout布局器 user 用户名称s pwd 用户密码 user_type 用户类型 welcome
* 一个提示welcome标签 hint 一个提示用户信息标签 user_typeBox 下拉列表 user_tf 用户文本框 pwd_tf 密码文本框
*
*/
public static JDialog dialog;
private GridBagLayout gbl;
private JLabel user, pwd, user_type, welcome, hint;
private JComboBox user_typeBox;
public static JTextField user_tf;
private JPasswordField pwd_tf;
private JButton ok, cancel;
private UserPwdDao upd = new UserPwdDao();
public LoginFrame() {
// 获得规格的对话框
dialog = new CenterDialog().getCenterDialog(dialog, 3, true);
// 设对话框标题
dialog.setTitle("用户登录");
// 设置布局
gbl = new GridBagLayout();
dialog.setLayout(gbl);
// 构造两个按钮,并添加相应事件
ok = new GetButton().getButton("登录", "icon/登录.gif", "登录");
ok.addActionListener(this);
cancel = new GetButton().getButton("退出", "icon/退出.gif", "退出");
cancel.addActionListener(this);
// 初始化各种组件
user = new JLabel("用户名称");
user.setFont(new Font("Serif", Font.BOLD, 12));
user.setForeground(Color.WHITE);
pwd = new JLabel("用户密码");
pwd.setFont(new Font("Serif", Font.BOLD, 12));
pwd.setForeground(Color.WHITE);
user_type = new JLabel("用户类型");
user_type.setFont(new Font("Serif", Font.BOLD, 12));
user_type.setForeground(Color.WHITE);
welcome = new JLabel("超市管理系统", new ImageIcon("icon/login.png"),
SwingConstants.CENTER);
welcome.setFont(new Font("Serif", Font.PLAIN, 20));
welcome.setForeground(Color.ORANGE);
hint = new JLabel("用户名默认为admin,密码为123456");
hint.setFont(new Font("Serif", Font.BOLD, 14));
hint.setForeground(Color.PINK);
user_tf = new JTextField("admin", 14);
pwd_tf = new JPasswordField("123456", 14);
// 设置GridBagLayout布局
dialog.add(welcome, new GBC(0, 0, 2, 1).setAnchor(GBC.CENTER));
dialog.add(user, new GBC(0, 1).setAnchor(GBC.EAST));
dialog.add(user_tf, new GBC(1, 1).setFill(GBC.WEST).setInset(1));
dialog.add(pwd, new GBC(0, 2).setAnchor(GBC.EAST));
dialog.add(pwd_tf, new GBC(1, 2).setFill(GBC.WEST).setInset(1));
dialog.add(user_type, new GBC(0, 3).setAnchor(GBC.EAST));
dialog.add(this.initalComboBox(), new GBC(1, 3).setFill(GBC.WEST).setInset(
1));
dialog.add(ok, new GBC(0, 4).setAnchor(GBC.EAST));
dialog.add(cancel, new GBC(1, 4).setAnchor(GBC.EAST));
dialog.add(hint, new GBC(0, 6, 2, 1).setAnchor(GBC.CENTER));
dialog.getContentPane().setBackground(Color.BLACK);
dialog.setVisible(true);
}
/**
* 初始化用户类型的下拉列表框
*
* @return user_typeBox 返回一个用户类型下拉列表框
*/
public JComboBox initalComboBox() {
user_typeBox = new JComboBox();
user_typeBox.setEditable(false);
user_typeBox.addItem("系统管理员");
user_typeBox.addItem("POS端用户");
return user_typeBox;
}
/**
* 事件处理:对用户输入的用户名称,用户密码,用户类型与数据库中进行核对
*/
public void actionPerformed(ActionEvent e) {
String name = e.getActionCommand();
if (e.getActionCommand().equals("登录")) {
if (user_tf.getText().equals("")) {
JOptionPane.showMessageDialog(null, "用户名不能为空");
return;
}
if (new String(pwd_tf.getPassword()).equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空");
return;
}
if (upd.findUser(user_tf.getText(), String.valueOf(pwd_tf.getPassword()),
user_typeBox.getSelectedItem().toString())) {
if (user_typeBox.getSelectedItem().toString().equals("系统管理员")) {
MainFrameFactory.getInstance(name).getContainer();
dialog.setVisible(false);
dialog.dispose();
} else if (user_typeBox.getSelectedItem().toString().equals("POS端用户")) {
PosFrameFactory.getInstance(name).getContainer();
dialog.setVisible(false);
dialog.dispose();
}
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误", "提示",
JOptionPane.ERROR_MESSAGE);
}
} else if (e.getActionCommand().equals("退出")) {
int ch = JOptionPane.showConfirmDialog(null, "确实退出系统?", "友情提示",
JOptionPane.YES_NO_OPTION);
if (ch == JOptionPane.YES_OPTION) {
dialog.setVisible(false);
dialog.dispose();
}
}
}
/**
* @param args
* 从命令行输入的字符串
*/
public static void main(String[] args) {
try {
UIManager
.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e) {
}
new LoginFrame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -