📄 logindialog.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 + -