📄 loginfrm.java
字号:
package com.cdaccp.view.frame;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.cdaccp.dao.OperatorDAO;
import com.util.Config;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusAdapter;
import javax.swing.border.TitledBorder;
/**
* 登录界面
*/
public class LoginFrm
extends JFrame {
public LoginFrm() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
jpfPassword.setBackground(SystemColor.window);
jpfPassword.setBorder(titledBorder2);
jpfPassword.setBounds(new Rectangle(111, 81, 127, 24));
jpfPassword.addKeyListener(new LoginFrm_jpfPassword_keyAdapter(this));
jLabel3.setText("编号");
jLabel3.setBounds(new Rectangle(60, 40, 38, 30));
jLabel2.setText("密码");
jLabel2.setBounds(new Rectangle(60, 79, 38, 29));
btnOK.setBounds(new Rectangle(173, 126, 91, 31));
btnOK.setToolTipText("登录");
btnOK.setIcon(ok);
btnOK.addKeyListener(new LoginFrm_btnOK_keyAdapter(this));
btnOK.addActionListener(new LoginFrm_btnOK_actionAdapter(this));
btnCancel.setBounds(new Rectangle(43, 126, 91, 31));
btnCancel.setToolTipText("取消");
btnCancel.setIcon(no);
btnCancel.addActionListener(new LoginFrm_btnCancel_actionAdapter(this));
this.setResizable(false);
this.setTitle("用户登录");
this.getContentPane().setBackground(new Color(221, 231, 246));
this.setForeground(SystemColor.control);
this.setIconImage(new ImageIcon("logo.gif").getImage());
jtfUserName.setBackground(SystemColor.window);
jtfUserName.setBorder(titledBorder1);
jtfUserName.setToolTipText("");
jtfUserName.addKeyListener(new LoginFrm_jtfUserName_keyAdapter(this));
jtfUserName.addFocusListener(new LoginFrm_jtfUserName_focusAdapter(this));
jLabel1.setForeground(Color.red);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setBounds(new Rectangle(99, 21, 155, 19));
jPanel1.setBounds(new Rectangle(0, 0, 300, 200));
jPanel1.setLayout(null);
this.getContentPane().add(jPanel1);
jPanel1.add(jLabel1);
jPanel1.add(jLabel3);
jPanel1.add(jtfUserName);
jPanel1.add(jLabel2);
jPanel1.add(jpfPassword);
jPanel1.add(btnOK);
jPanel1.add(btnCancel);
jtfUserName.setBounds(new Rectangle(111, 43, 127, 24));
this.setSize(350, 220);
this.setUndecorated(true);
}
public static void main(String[] args) {
LoginFrm loginfrm = new LoginFrm();
}
JTextField jtfUserName = new JTextField();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JPasswordField jpfPassword = new JPasswordField();
JButton btnOK = new JButton();
JButton btnCancel = new JButton();
JLabel jLabel1 = new JLabel();
TitledBorder titledBorder1 = new TitledBorder("");
TitledBorder titledBorder2 = new TitledBorder("");
ImageIcon image = new ImageIcon("image/log.png");
ImageIcon ok =new ImageIcon("image/ok1.png");
ImageIcon no = new ImageIcon("image/no1.png");
Panel1 jPanel1 = new Panel1(image);
// JPanel jPanel1 =new JPanel();
public void btnOK_actionPerformed(ActionEvent actionEvent) {
yangzheng();
}
/**
* 用来在Jpanel上添加背景
*/
class Panel1 extends JPanel {
BorderLayout borderLayout1 = new BorderLayout();
private ImageIcon image = null;
public Panel1(ImageIcon image) {
try {
this.image = image;
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setLayout(borderLayout1);//设置布局管理
}
protected void paintComponent(Graphics g) {
setOpaque(true);
super.paintComponent(g);
Dimension d = getSize();
for (int x = 0; x < d.width; x += image.getIconWidth())
for (int y = 0; y < d.height; y += image.getIconHeight())
g.drawImage(image.getImage(), x, y, null, null);
}
}
void yangzheng() {
if (jtfUserName.getText() == null || jtfUserName.getText().equals("")) {
JOptionPane.showMessageDialog(this, "编号不能为空");
jtfUserName.grabFocus();
return;
}
int userId = 0;
try {
userId = Integer.parseInt(jtfUserName.getText().trim());
}
catch (NumberFormatException ex) {
jLabel1.setText("编号只能是数字");
return;
}
jLabel1.setText("");
String pwd = new String(jpfPassword.getPassword());
if (OperatorDAO.verifyUser(userId, pwd)) { //如果验证通过,则显示主界面
Config.currOperator = OperatorDAO.loadById(userId); //将登录者放入Config类的静态属性中
MainFrm mainFrm = new MainFrm(); //新建主窗口
mainFrm.setExtendedState(Frame.MAXIMIZED_BOTH); //将主窗口最大化
mainFrm.setVisible(true); //设置可见
this.setVisible(false); //将自己(登录窗口)隐藏
}
else {
JOptionPane.showMessageDialog(this, "用户名或密码错误", "登录",
JOptionPane.ERROR_MESSAGE);
jtfUserName.grabFocus();
}
}
public void btnCancel_actionPerformed(ActionEvent e) {
System.exit(0);
}
public void jpfPassword_keyTyped(KeyEvent e) {
if (e.getKeyChar() == '\n') {
yangzheng();
}
}
public void jtfUserName_focusGained(FocusEvent e) {
jLabel1.setText("");
}
public void btnOK_keyTyped(KeyEvent e) {
if (e.getKeyChar() == '\n') {
yangzheng();
}
}
public void jtfUserName_keyTyped(KeyEvent e) {
if (e.getKeyChar() == '\n') {
jpfPassword.grabFocus();
}
}
}
class LoginFrm_btnOK_keyAdapter
extends KeyAdapter {
private LoginFrm adaptee;
LoginFrm_btnOK_keyAdapter(LoginFrm adaptee) {
this.adaptee = adaptee;
}
public void keyTyped(KeyEvent e) {
adaptee.btnOK_keyTyped(e);
}
}
class LoginFrm_jtfUserName_focusAdapter
extends FocusAdapter {
private LoginFrm adaptee;
LoginFrm_jtfUserName_focusAdapter(LoginFrm adaptee) {
this.adaptee = adaptee;
}
public void focusGained(FocusEvent e) {
adaptee.jtfUserName_focusGained(e);
}
}
class LoginFrm_jtfUserName_keyAdapter
extends KeyAdapter {
private LoginFrm adaptee;
LoginFrm_jtfUserName_keyAdapter(LoginFrm adaptee) {
this.adaptee = adaptee;
}
public void keyTyped(KeyEvent e) {
adaptee.jtfUserName_keyTyped(e);
}
}
class LoginFrm_jpfPassword_keyAdapter
extends KeyAdapter {
private LoginFrm adaptee;
LoginFrm_jpfPassword_keyAdapter(LoginFrm adaptee) {
this.adaptee = adaptee;
}
public void keyTyped(KeyEvent e) {
adaptee.jpfPassword_keyTyped(e);
}
}
class LoginFrm_btnCancel_actionAdapter
implements ActionListener {
private LoginFrm adaptee;
LoginFrm_btnCancel_actionAdapter(LoginFrm adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnCancel_actionPerformed(e);
}
}
class LoginFrm_btnOK_actionAdapter
implements ActionListener {
private LoginFrm adaptee;
LoginFrm_btnOK_actionAdapter(LoginFrm adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent actionEvent) {
adaptee.btnOK_actionPerformed(actionEvent);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -