📄 logindialog.java
字号:
package com.lzw.login;
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;
import com.lzw.MainFrame;
import com.lzw.dao.Dao;
import java.lang.reflect.Method;
public class LoginDialog extends JInternalFrame {
private static final long serialVersionUID = 1L;
private LoginPanel loginPanel = null;
private JLabel jLabel = null;
private JTextField userField = null;
private JLabel jLabel1 = null;
private JPasswordField passwordField = null;
private JButton loginButton = null;
private JButton exitButton = null;
private static String userStr;
private MainFrame mainFrame=null;
/**
* @param owner
*/
public LoginDialog(MainFrame mf) {
super();
mainFrame=mf;
try {
initialize();
this.setVisible(true);
//mainFrame = new MainFrame();
//initialize();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 初始化loginPanel登录面板的方法
*
* @return com.lzw.login.LoginPanel
*/
private LoginPanel getLoginPanel() {
if (loginPanel == null) {
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(86, 71, 55, 18));
jLabel1.setText("密 码:");
jLabel = new JLabel();
jLabel.setText("用户名:");
jLabel.setBounds(new Rectangle(85, 41, 56, 18));
loginPanel = new LoginPanel();
loginPanel.setLayout(null);
loginPanel.setBackground(new Color(0xD8DDC7));
loginPanel.add(jLabel, null);
loginPanel.add(getUserField(), null);
loginPanel.add(jLabel1, null);
loginPanel.add(getPasswordField(), null);
loginPanel.add(getLoginButton(), null);
loginPanel.add(getExitButton(), null);
}
return loginPanel;
}
/**
* This method initializes userField
*
* @return javax.swing.JTextField
*/
private JTextField getUserField() {
if (userField == null) {
userField = new JTextField();
userField.setBounds(new Rectangle(142, 39, 127, 22));
}
return userField;
}
/**
* This method initializes passwordField
*
* @return javax.swing.JPasswordField
*/
private JPasswordField getPasswordField() {
if (passwordField == null) {
passwordField = new JPasswordField();
passwordField.setBounds(new Rectangle(143, 69, 125, 22));
passwordField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent e) {
if(e.getKeyChar()=='\n')
loginButton.doClick();
}
});
}
return passwordField;
}
/**
* This method initializes loginButton
*
* @return javax.swing.JButton
*/
private JButton getLoginButton() {
if (loginButton == null) {
loginButton = new JButton();
loginButton.setBounds(new Rectangle(109, 114, 48, 20));
loginButton.setIcon(new ImageIcon(getClass().getResource(
"/res/loginButton.jpg")));
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
userStr = userField.getText();
String passStr = new String(passwordField.getPassword());
if (!Dao.checkLogin(userStr, passStr)) {
JOptionPane.showMessageDialog(LoginDialog.this,
"用户名与密码无法登录", "登录失败",
JOptionPane.ERROR_MESSAGE);
return;
}
} catch (Exception e1) {
e1.printStackTrace();
}
mainFrame.getCzyStateLabel().setText(userStr);
//setVisible(true);
/*try {
method.invoke(mb, true);
} catch (InvocationTargetException ex) {
} catch (IllegalArgumentException ex) {
} catch (IllegalAccessException ex) {
}*/
mainFrame.getFrameMenuBar().initMenu(true);
dispose();
//mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//mainFrame.setVisible(true);
//mainFrame.getCzyStateLabel().setText(userStr);
//setVisible(true);
}
});
}
return loginButton;
}
/**
* This method initializes exitButton
*
* @return javax.swing.JButton
*/
private JButton getExitButton() {
if (exitButton == null) {
exitButton = new JButton();
exitButton.setBounds(new Rectangle(181, 114, 48, 20));
exitButton.setIcon(new ImageIcon(getClass().getResource(
"/res/exitButton.jpg")));
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
dispose();
//System.exit(0);
}
});
}
return exitButton;
}
/**
* 界面初始化方法
*
* @return void
*/
private void initialize() {
Dimension size = getToolkit().getScreenSize();
setLocation((size.width - 296) / 2, (size.height - 188) / 2);
setSize(296, 188);
this.setTitle("系统登录");
this.setClosable(true);
setContentPane(getLoginPanel());
}
public String getUserStr() {
return userStr;
}
} // @jve:decl-index=0:visual-constraint="10,10"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -