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

📄 logindialog.java

📁 此文档针对开发人员和测试人员。第二章对软件进行了全面的描述。第三章对接口进行了分析。第四章对软件实现的功能进行概述。第五章对软件后续开发实现提出的要求。第六章提出其他一些在软件开发过程中需要注意的问题
💻 JAVA
字号:
package com.ciash.bms.gui.impl;


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import com.ciash.common.gui.tool.ToolKit;
import java.util.Vector;

import com.ciash.bms.gui.uiInter.LoginUI;


/**
 * 用户登陆对话框,内部设置检查事件,将检查用户名与密码是否为空
 */
public class LoginDialog extends JDialog implements LoginUI{
    
    private JLabel jLabel1 = new JLabel();
    private JLabel jLabel2 = new JLabel();
    private JButton confirmButton = new JButton();
    private JButton cancelButton = new JButton();
    private CheckListener listener = new CheckListener();
    private Vector confirmListeners = new Vector();
    private GridBagLayout gridBagLayout1 = new GridBagLayout();
    
    private JPasswordField jPasswordField1 = new JPasswordField();
    private JTextField textField = new JTextField();
    
    public int OK_OPTION = 1;
    public int CANCEL_OPTION = 0;
    private int option = -1;
    
    public LoginDialog() {
        this(null, "登陆", true);
        jbInit();
        textField.setName("用户名");
        jPasswordField1.setName("密码");
        addWindowListener(listener);
        confirmButton.addActionListener(listener);
        confirmButton.setActionCommand(CheckListener.CONFIRM);
        cancelButton.addActionListener(listener);
        cancelButton.setActionCommand(CheckListener.CANCEL);
    }
    
    public int showDialog(){
        init();
        setVisible(true);
        return option;
    }
    
    public int getOption(){
        return option;
    }
    
    public String getId(){
        return textField.getText();
    }
    
    public String getPassword(){
        return new String(jPasswordField1.getPassword());
    }
    
    public LoginDialog(Frame frame, String string, boolean b) {
        super(frame, string, b);
    }
    
    private boolean check(String id, String s) {
        if (ToolKit.isNull(id)) {
            JOptionPane.showMessageDialog(null, s + "不能为空", "错误"
                    , JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
    
    // ---------------------------------------------------------------------------------------------
    // 初始化窗口
    private void init() {
        textField.setText("");
        textField.grabFocus();
        jPasswordField1.setText("");
        setSize(250,200);
        Point centerLocation = ToolKit.getScreenCenterLocation(getSize());
        setLocation(centerLocation);
    }
    
    // ---------------------------------------------------------------------------------------------
    // 继续实现事件驱动内部封装
    private class CheckListener
            extends WindowAdapter
            implements ActionListener {
        
        public final static String CONFIRM = "确定";
        public final static String CANCEL = "取消";
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();
            if (command.equals(CONFIRM)) {
                if(check(getId(), "用户名")){
                    if(check(getPassword(), "密码")){
                        option = OK_OPTION;
                        setVisible(false);
                    }
                }
            } else if(command.equals(CANCEL)){
                option = CANCEL_OPTION;
                setVisible(false);
            }
        }
        
        public void windowClosing(WindowEvent e) {
            option = CANCEL_OPTION;
            setVisible(false);
        }
    }
    
    // ---------------------------------------------------------------------------------------------
    //JB9 自带控件设计方法
    private void jbInit() {
        jLabel2.setText("密码");
        jPasswordField1.setToolTipText("在这里输入你的密码");
        jPasswordField1.setText("");
        textField.setText("");
        textField.setToolTipText("在这里输入你的用户名");
        jLabel1.setText("用户名");
        confirmButton.setText("确定");
        cancelButton.setText("取消");
        getContentPane().setLayout(gridBagLayout1);
        getContentPane().add(textField, new GridBagConstraints(1, 0, 2, 1, 1.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL
                , new Insets(23, 19, 0, 23), 128, 6));
        getContentPane().add(jPasswordField1, new GridBagConstraints(1, 1, 2, 1, 1.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL
                , new Insets(15, 19, 0, 23), 128, 5));
        getContentPane().add(jLabel2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE
                , new Insets(9, 27, 0, 0), 16, 17));
        getContentPane().add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE
                , new Insets(23, 27, 0, 0), 7, 15));
        getContentPane().add(confirmButton, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0
                , GridBagConstraints.CENTER, GridBagConstraints.NONE
                , new Insets(14, 34, 20, 0), 22, 0));
        getContentPane().add(cancelButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
                , GridBagConstraints.CENTER, GridBagConstraints.NONE
                , new Insets(14, 20, 20, 23), 25, 0));
    }
    // ---------------------------------------------------------------------------------------------
}

⌨️ 快捷键说明

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