一、数据库连接(DBUtil.java)
后续的其他功能模块可以直接调用这个代码对查看或改变数据库中数据
package util;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class DBUtil {
public static String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=MobileBase;encrypt=true;trustServerCertificate=true";
public static String userName="yourname";//在yourname那里填写登录数据库所用的用户名,可以是直接设置过的sa,或者其他
public static String userPwd="yourpassword";//在yourpassword那里填写登录数据库所用用户名相应的密码
static {
try {
Class.forName(DRIVER);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
public static Connection getConn(){
try {
return DriverManager.getConnection(dbURL,userName,userPwd);
}catch (SQLException e){
e.printStackTrace();
}
return null;
}
public static void closeConn(Connection connection){
if(connection!=null){
try {
connection.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
public static void closePs(PreparedStatement ps){
if(ps!=null){
try {
ps.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
public static void closeRs(ResultSet rs){
if(rs!=null){
try {
rs.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
}
}
二、界面布局代码(DimensionUtil.java)
package util;
import javax.swing.*;
import java.awt.*;
public class DimensionUtil {
public static Rectangle getBounds(){
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Insets screenInsets=Toolkit.getDefaultToolkit().getScreenInsets(new JFrame().getGraphicsConfiguration());
Rectangle rectangle=new Rectangle(screenInsets.left,screenInsets.top,screenSize.width-screenInsets.left-screenInsets.right,
screenSize.height-screenInsets.top-screenInsets.bottom);
return rectangle;
}
}
三、界面代码(LoginView.java)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
public class LoginView extends JFrame{
JLabel nameLabel=new JLabel("翼动Java营业厅登录",JLabel.CENTER);
SpringLayout springLayout=new SpringLayout();
JPanel centerPanel=new JPanel(springLayout);
JLabel userNameLabel=new JLabel("用户名:");
JTextField userText=new JTextField();
JLabel pwdLabel=new JLabel("密码:");
JPasswordField pwdField=new JPasswordField();
JButton loginBtn=new JButton("登录");
JButton resetBtn=new JButton("重置");
JButton registerBtn=new JButton("注册");
SystemTray systemTray;
TrayIcon trayIcon;
LoginHandler loginHandler;
public LoginView(){
super("翼动Java营业厅");
loginHandler=new LoginHandler(this);
Container contentPane=getContentPane();
nameLabel.setFont(new Font("华文行楷",Font.PLAIN,40));
nameLabel.setPreferredSize(new Dimension(0,80));
Font centerFont=new Font("楷体",Font.PLAIN,20);
userNameLabel.setFont(centerFont);
pwdLabel.setFont(centerFont);
loginBtn.setFont(centerFont);
registerBtn.setFont(centerFont);
resetBtn.setFont(centerFont);
userText.setPreferredSize(new Dimension(200,30));
pwdField.setPreferredSize(new Dimension(200,30));
//把主键加入面板
centerPanel.add(userNameLabel);
centerPanel.add(userText);
centerPanel.add(pwdLabel);
centerPanel.add(pwdField);
centerPanel.add(loginBtn);
centerPanel.add(registerBtn);
centerPanel.add(resetBtn);
contentPane.add(nameLabel,BorderLayout.NORTH);
contentPane.add(centerPanel,BorderLayout.CENTER);
loginBtn.addActionListener(loginHandler);
loginBtn.addKeyListener(loginHandler);
resetBtn.addActionListener(loginHandler);
registerBtn.addActionListener(loginHandler);
//布局
Spring childWidth=Spring.sum(Spring.sum(Spring.width(userNameLabel),Spring.width(userText)),Spring.constant(20));
int offseX=childWidth.getValue()/2;
springLayout.putConstraint(SpringLayout.WEST,userNameLabel,-offseX,SpringLayout.HORIZONTAL_CENTER,centerPanel);
springLayout.putConstraint(SpringLayout.NORTH,userNameLabel,20,SpringLayout.NORTH,centerPanel);
springLayout.putConstraint(SpringLayout.WEST,userText,20,SpringLayout.EAST,userNameLabel);
springLayout.putConstraint(SpringLayout.NORTH,userText,0,SpringLayout.NORTH,userNameLabel);
springLayout.putConstraint(SpringLayout.EAST,pwdLabel,0,SpringLayout.EAST,userNameLabel);
springLayout.putConstraint(SpringLayout.NORTH,pwdLabel,20,SpringLayout.SOUTH,userNameLabel);
springLayout.putConstraint(SpringLayout.WEST,pwdField,20,SpringLayout.EAST,pwdLabel);
springLayout.putConstraint(SpringLayout.NORTH,pwdField,0,SpringLayout.NORTH,pwdLabel);
springLayout.putConstraint(SpringLayout.WEST,loginBtn,0,SpringLayout.WEST,pwdLabel);
springLayout.putConstraint(SpringLayout.NORTH,loginBtn,20,SpringLayout.SOUTH,pwdLabel);
springLayout.putConstraint(SpringLayout.WEST,resetBtn,20,SpringLayout.EAST,loginBtn);
springLayout.putConstraint(SpringLayout.NORTH,resetBtn,0,SpringLayout.NORTH,loginBtn);
springLayout.putConstraint(SpringLayout.WEST,registerBtn,20,SpringLayout.EAST,resetBtn);
springLayout.putConstraint(SpringLayout.NORTH,registerBtn,0,SpringLayout.NORTH,resetBtn);
if(SystemTray.isSupported()){
systemTray=SystemTray.getSystemTray();
URL imgUrl =LoginView.class.getClassLoader().getResource("yid.png");
trayIcon=new TrayIcon(new ImageIcon(imgUrl).getImage());
//设置托盘图片大小自动缩放
trayIcon.setImageAutoSize(true);
try{
systemTray.add(trayIcon);
}catch (AWTException e){
e.printStackTrace();
}
//增加最小化时销毁资源
this.addWindowListener(new WindowAdapter() {
@Override
public void windowIconified(WindowEvent e) {
LoginView.this.dispose();
}
});
//托盘事件监听
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int clickCount=e.getClickCount();
if(clickCount==1){
LoginView.this.setExtendedState(JFrame.NORMAL);
}
LoginView.this.setVisible(true);
}
});
}
//设置loginBtn为默认按钮
getRootPane().setDefaultButton(loginBtn);
//自定义图标
URL imgUrl =LoginView.class.getClassLoader().getResource("yid.png");//改为自己在idea当中添加的图片的名称
setIconImage(new ImageIcon(imgUrl).getImage());
setSize(600,400);
setLocationRelativeTo(null);//居中
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭退出
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new LoginView();
}
public JTextField getUserText() {
return userText;
}
public JPasswordField getPwdField() {
return pwdField;
}
}
四、登录过程中使用数据库(LoginHandler.java)
import java.sql.*;
import util.DBUtil;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class LoginHandler extends KeyAdapter implements ActionListener {
private LoginView loginView;
public LoginHandler(LoginView loginView){
this.loginView=loginView;
}
@Override
public void actionPerformed(ActionEvent e){
JButton jButton =(JButton) e.getSource();
String text=jButton.getText();
if("重置".equals(text)){
loginView.getUserText().setText("");
loginView.getPwdField().setText("");
}else if("登录".equals(text)){
login();
}
else if("注册".equals(text)){
new RegisterView();
}
}
private void login() {
String user = loginView.getUserText().getText();
char[] chars = loginView.getPwdField().getPassword();
if (user == null || "".equals(user.trim()) || chars == null) {
JOptionPane.showMessageDialog(loginView, "用户名密码不能为空");
return;
}
String pwd = new String(chars);
try {
Connection conn = DBUtil.getConn();
String sql = "SELECT * FROM tb_User WHERE PhoneNumber = ? AND Pwd = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, user);
stmt.setString(2, pwd);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
new MobileHall(user);
loginView.dispose();
JOptionPane.showMessageDialog(loginView, "登录成功");
} else {
// 用户账号或密码错误
JOptionPane.showMessageDialog(loginView, "用户账号或密码错误");
}
}
}catch (SQLException ex) {
ex.printStackTrace();
}
}
@Override
public void keyPressed(KeyEvent e) {
if(KeyEvent.VK_ENTER==e.getKeyCode()){
login();
};
}
}