⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loginpanel.java

📁 java私塾里的图书进销存项目的源代码,非常不错,J2SE基础知识复习非常有帮助!
💻 JAVA
字号:
package cn.javass.bookmgr.user.ui.panels;

import java.awt.*;
import com.borland.jbcl.layout.*;
import javax.swing.*;
import java.awt.event.*;
import cn.javass.bookmgr.user.business.factory.UserFactory;
import cn.javass.bookmgr.MainFrame;
import cn.javass.bookmgr.user.valueobject.UserModel;
/**
 * 用户模块表现层用于用户登录的Panel
 *
 * <p>Title: Java私塾第一个Java项目——图书进销存系统(单机版)</p>
 * <p>Description: 网址:<a href="http://www.javass.cn">http://www.javass.cn</a>
 * 新电话:010-86835215   新地址:北京市海淀区厂洼路5号院深博达商务楼5层</p>
 * <p>Copyright: Copyright (c) 2008</p>
 * <p>Company: Java私塾</p>
 * @author Java私塾
 * @version 1.0
 */
public class LoginPanel extends JPanel {
  //以下为本界面需要的组件定义

  XYLayout xYLayout1 = new XYLayout();
  JLabel jLabel1 = new JLabel();
  JLabel jLabel2 = new JLabel();
  JLabel jLabel3 = new JLabel();
  JTextField txt_userId = new JTextField();
  JPasswordField pwd_pwd = new JPasswordField();
  JButton btn_login = new JButton();

  /**
   * 用来保持对主窗体的引用
   */
  MainFrame mf= null;

  /**
   * 构建用户登录的Panel
   * @param mf 主窗体的引用
   */
  public LoginPanel(MainFrame mf) {
    try {
      this.mf = mf;
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }
  /**
   * 真正进行组件初始化,并构建整个界面
   * @throws Exception
   */
  void jbInit() throws Exception {
    jLabel1.setFont(new java.awt.Font("SansSerif", 1, 30));
    jLabel1.setText("欢迎使用Javass图书进销存(单机版)");
    this.setLayout(xYLayout1);
    this.setMinimumSize(new Dimension(800, 600));
    this.setPreferredSize(new Dimension(800, 600));
    xYLayout1.setWidth(800);
    xYLayout1.setHeight(600);
    jLabel2.setText("用户编号");
    jLabel3.setText("用户口令");
    txt_userId.setText("");
    pwd_pwd.setText("");
    btn_login.setText("登录");
    btn_login.addActionListener(new LoginPanel_btn_login_actionAdapter(this));
    this.add(jLabel1,    new XYConstraints(160, 40, 535, 76));
    this.add(jLabel2, new XYConstraints(275, 152, 65, 37));
    this.add(txt_userId, new XYConstraints(338, 155, 209, 34));
    this.add(pwd_pwd,     new XYConstraints(338, 218, 209, 34));
    this.add(jLabel3,  new XYConstraints(275, 215, 65, 37));
    this.add(btn_login, new XYConstraints(456, 277, 91, 36));
  }
  /**
   * 点击登录按钮的事件处理
   * @param e Action事件对象
   */
  void btn_login_actionPerformed(ActionEvent e) {
    //1:收集参数
    String id = this.txt_userId.getText();
    String pwd = new String(this.pwd_pwd.getPassword());
    //1.1检测数据
    if (id == null || id.trim().length() == 0) {
      JOptionPane.showMessageDialog(null, "用户编号不能为空");
      return;
    }
    if (pwd == null || pwd.trim().length() == 0) {
      JOptionPane.showMessageDialog(null, "用户口令不能为空");
      return;
    }
    //2:
    //3:调用逻辑层Api,并获取返回值
    boolean flag = UserFactory.getInstance().createUserEbi().login(id, pwd);
    //4:根据返回值选择新的Panel
    if (flag) {
      //使登录界面不再出现
      this.remove(this.txt_userId);
      this.remove(this.pwd_pwd);
      this.remove(this.btn_login);
      this.remove(this.jLabel2);
      this.remove(this.jLabel3);
      this.updateUI();
      //针对菜单进行简单的权限控制
      UserModel um = UserFactory.getInstance().createUserEbi().getUserModelById(
          id);
      this.mf.jMenuItem1.setEnabled(false);
      this.mf.jMenuItem17.setEnabled(true);
      if (um.getType() == um.TYPE_INT_1) {
        this.mf.jMenu1.setEnabled(true);
        this.mf.jMenu2.setEnabled(true);
        this.mf.jMenu3.setEnabled(true);
        this.mf.jMenu4.setEnabled(true);
        this.mf.jMenu5.setEnabled(true);
      }
      else if (um.getType() == um.TYPE_INT_2) {
        this.mf.jMenu3.setEnabled(true);
      }
      else if (um.getType() == um.TYPE_INT_3) {
        this.mf.jMenu2.setEnabled(true);
      }
      else if (um.getType() == um.TYPE_INT_4) {
        this.mf.jMenu1.setEnabled(true);
      }
      else if (um.getType() == um.TYPE_INT_5) {
        this.mf.jMenu4.setEnabled(true);
      }
      else if (um.getType() == um.TYPE_INT_6) {
        this.mf.jMenu5.setEnabled(true);
      }
    }else{
      JOptionPane.showMessageDialog(null, "用户编号或口令不正确");
    }
  }
}
//以下为事件处理中的Adaper类
class LoginPanel_btn_login_actionAdapter implements java.awt.event.ActionListener {
  LoginPanel adaptee;

  LoginPanel_btn_login_actionAdapter(LoginPanel adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.btn_login_actionPerformed(e);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -