📄 loginui.java
字号:
package ui;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import bean.User;
import dao.IUserDAO;
import dao.IUserDAOImpl;
public class LogInUI {
private static final long serialVersionUID = -6646173435421766907L;
private JTextField username;
private JPasswordField passwordField;
private JLabel labName, labPassword;
private JButton butConfirm, butCancel;
private MainUI mainUI = new MainUI();
private JFrame frame;
public LogInUI() {
frame = new JFrame("请登录...");
frame.setSize(300, 120);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((scrSize.width - frame.getSize().width) / 2,
(scrSize.height - frame.getSize().height) / 2);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(3, 2));
labName = new JLabel("请输入用户名:");
contentPane.add(labName);
username = new JTextField(10);
username.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
loginAction();
}
});
contentPane.add(username);
labPassword = new JLabel("请输用户入密码:");
contentPane.add(labPassword);
passwordField = new JPasswordField();
passwordField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
loginAction();
}
});
contentPane.add(passwordField);
butConfirm = new JButton("确定");
butConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
loginAction();
}
});
contentPane.add(butConfirm);
butCancel = new JButton("取消");
butCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
username.setText("");
passwordField.setText("");
}
});
contentPane.add(butCancel);
frame.setVisible(true);
}
private boolean validate(String username, String password) {
IUserDAO userDAO = new IUserDAOImpl();
List<User> list = userDAO.queryUserByName(username);
for (User o : list) {
if (username.equals(o.getUsername().trim())
&& password.equals(o.getPassword().trim()))
return true;
}
return false;
};
private void loginAction() {
String password = new String(passwordField.getPassword());
if (username.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "用户名不能为空! 请重试!");
return;
}
if (password.equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空! 请重试!");
return;
}
if (validate(username.getText(), String.valueOf(passwordField
.getPassword()))) {
JOptionPane.showMessageDialog(null, "祝贺你:" + username.getText()
+ ",陆成功了哦!");
mainUI.setVisible(true);
frame.dispose();
return;
} else {
JOptionPane.showMessageDialog(null, "密码不正确! 请重试!");
passwordField.setText("");
}
}
public static void main(String args[]) {
new LogInUI();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -