📄 logindialog.java~33~
字号:
package mvcdemo;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
public class LoginDialog extends JDialog {
JPanel panel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField txtName = new JTextField();
JPasswordField txtPwd = new JPasswordField();
JButton btnCancel = new JButton();
JButton btnLogin = new JButton();
public LoginDialog(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public LoginDialog() {
this(new Frame(), "LoginDialog", false);
}
private void jbInit() throws Exception {
this.setSize(260, 160);
panel1.setLayout(null);
jLabel1.setText("密 码");
jLabel1.setBounds(new Rectangle(30, 60, 40, 16));
jLabel2.setText("用户名");
jLabel2.setBounds(new Rectangle(31, 21, 40, 16));
txtName.setText("");
txtName.setBounds(new Rectangle(82, 17, 122, 23));
txtPwd.setText("");
txtPwd.setBounds(new Rectangle(80, 56, 124, 24));
btnCancel.setBounds(new Rectangle(125, 98, 67, 24));
btnCancel.setText("取消");
btnCancel.addActionListener(new LoginDialog_btnCancel_actionAdapter(this));
btnLogin.setBounds(new Rectangle(40, 99, 67, 24));
btnLogin.setText("登陆");
btnLogin.addActionListener(new LoginDialog_btnLogin_actionAdapter(this));
this.addWindowListener(new LoginDialog_this_windowAdapter(this));
getContentPane().add(panel1);
panel1.add(jLabel2);
panel1.add(jLabel1, null);
panel1.add(txtName);
panel1.add(txtPwd);
panel1.add(btnLogin);
panel1.add(btnCancel);
}
public void btnLogin_actionPerformed(ActionEvent e) {
//取用户名和密码
String name = txtName.getText();
String password = new String(txtPwd.getPassword());
//数据验证
if (name.equals("") || name == null) {
JOptionPane.showMessageDialog(this, "用户名不能为空");
return;
}
if (password.equals("") || password == null) {
JOptionPane.showMessageDialog(this, "密码不能为空");
return;
}
//封装成User对象
User user = new User();
user.setName(name);
user.setPassword(password);
//调用dao的select方法
if (UserDAO.select(user)) {
//显示主窗体
TableFrame frame = new TableFrame();
// Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
//关闭登陆对话框
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "该用户不存在");
}
}
public void this_windowClosing(WindowEvent e) {
System.exit(0);
}
public void btnCancel_actionPerformed(ActionEvent e) {
int cho = JOptionPane.showConfirmDialog(this,"确定退出么","温馨提示",JOptionPane.OK_CANCEL_OPTION)
if (cho == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
}
class LoginDialog_this_windowAdapter extends WindowAdapter {
private LoginDialog adaptee;
LoginDialog_this_windowAdapter(LoginDialog adaptee) {
this.adaptee = adaptee;
}
public void windowClosing(WindowEvent e) {
adaptee.this_windowClosing(e);
}
}
class LoginDialog_btnCancel_actionAdapter implements ActionListener {
private LoginDialog adaptee;
LoginDialog_btnCancel_actionAdapter(LoginDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnCancel_actionPerformed(e);
}
}
class LoginDialog_btnLogin_actionAdapter implements ActionListener {
private LoginDialog adaptee;
LoginDialog_btnLogin_actionAdapter(LoginDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnLogin_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -