添加部分注释

This commit is contained in:
2025-02-06 20:14:11 +08:00
parent 0465908846
commit 82cde2df25
16 changed files with 215 additions and 5 deletions
+1 -1
View File
@@ -11,7 +11,6 @@
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
@@ -26,5 +25,6 @@ hs_err_pid*
src/com/msksbr/SQL/PassWord.java
# JetBrains IDEs
.idea/
*/.idea/
*.iml
Binary file not shown.
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

+83 -2
View File
@@ -13,100 +13,182 @@ import com.msksbr.MainFrm.MainFrm;
import com.msksbr.SQL.Connector;
import com.msksbr.images.EasterEgg;
/**
* LoginFrm 类表示登录窗口,用于用户登录到图书管理系统。
*/
public class LoginFrm extends javax.swing.JFrame {
// 数据库连接器
private final Connector connector;
// 登录消息异常处理对象
private final LoginMessangeExpection loginMessangeExpection;
// 主面板
private JPanel panel1;
// 标题面板
private JPanel titlePanel;
// 标题标签
private JLabel titleLabel;
// 主体面板
private JPanel bodyPanel;
// 底部面板
private JPanel bottomPanel;
// 提交按钮
private JButton commitButton;
// 标签面板
private JPanel labelPanel;
// 字段面板
private JPanel fieldPanel;
// 用户名标签面板
private JPanel uLabelPanel;
// 密码标签面板
private JPanel pLabelPanel;
// 用户名标签
private JLabel uLabel;
// 密码标签
private JLabel pLabel;
// 用户名文本框
private JTextField uField;
// 密码文本框
private JPasswordField pField;
// 用户名
private String username;
// 密码
private String password;
/**
* 构造函数,初始化登录窗口。
*/
public LoginFrm() {
// 初始化登录消息异常处理对象
loginMessangeExpection = new LoginMessangeExpection();
// 设置窗口图标
setIconImage(new ImageIcon("src/com/msksbr/images/mainicon.png").getImage());
try {
// 创建数据库连接器
connector = new Connector();
} catch (ClassNotFoundException | SQLException e) {
// 抛出运行时异常
throw new RuntimeException(e);
}
// 设置窗口标题
setTitle("登录到图书管理系统");
// 设置内容面板
setContentPane(panel1);
// 设置默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 调整窗口大小以适应其内容
pack();
// 设置窗口可见
setVisible(true);
// 设置窗口大小
setSize(400, 250);
// 设置窗口不可调整大小
setResizable(false);
// 设置文本框的首选大小
Dimension fieldDimension = new Dimension(250, 30);
this.uField.setPreferredSize(fieldDimension);
this.pField.setPreferredSize(fieldDimension);
// 为密码文本框添加按键监听器
this.pField.addKeyListener(new KeyAdapter() {
/**
* 当按键被按下时调用的方法。
*
* @param e 按键事件
*/
@Override
public void keyPressed(KeyEvent e) {
// 如果按下的是回车键
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
// 调用登录消息输入方法
loginMessangeEntered();
}
}
});
// 设置提交按钮的光标为手型光标
this.commitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
// 为提交按钮添加动作监听器
this.commitButton.addActionListener(new ActionListener() {
/**
* 当按钮被点击时调用的方法。
*
* @param e 动作事件
*/
public void actionPerformed(ActionEvent e) {
// 调用登录消息输入方法
loginMessangeEntered();
}
});
// 设置窗口位置
setLocation();
// 设置对话框位置
setDialogLocation();
}
/**
* 设置窗口位置为屏幕中心。
*/
private void setLocation() {
// 创建屏幕尺寸对象
ScreenSize screenSize = new ScreenSize();
// 设置窗口位置为屏幕中心
setLocation(screenSize.width / 2 - getWidth() / 2, screenSize.height / 2 - getHeight() / 2);
}
/**
* 设置对话框位置。
*/
private void setDialogLocation() {
// 设置对话框位置
loginMessangeExpection.setLocation(getX() + getWidth() / 4, getY() + getHeight() / 4);
}
/**
* 处理登录消息输入。
*/
private void loginMessangeEntered() {
// 获取用户名
username = uField.getText();
// 获取密码
password = new String(pField.getPassword());
// 检查登录消息是否正确
Boolean ISMessageTrue = isLoginMessageTrue();
if (ISMessageTrue) {
// 关闭当前窗口
dispose();
try {
// 创建主窗口
new MainFrm();
} catch (SQLException e) {
// 抛出运行时异常
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
// 抛出运行时异常
throw new RuntimeException(e);
}
} else {
// 显示登录消息异常对话框
loginMessangeExpection.setVisible(true);
}
}
/**
* 检查登录消息是否正确。
*
* @return 如果登录消息正确返回 true,否则返回 false
*/
private Boolean isLoginMessageTrue() {
// 如果用户名和密码是特定的彩蛋值
if (username.equals("java") && password.equals("beans")) {
// 显示彩蛋对话框
new EasterEgg();
return false;
} else {
try {
// 执行查询以检查用户名和密码是否匹配
ResultSet resultSet = connector.executeQuery("SELECT * FROM users WHERE username = '" + username + "'");
if (resultSet != null && resultSet.next()) { // 检查结果集是否不为空
if (resultSet.getString("password").equals(password)) {
@@ -115,10 +197,9 @@ public class LoginFrm extends javax.swing.JFrame {
}
return false;
} catch (SQLException e) {
// 抛出运行时异常
throw new RuntimeException(e);
}
}
}
}
@@ -3,22 +3,44 @@ package com.msksbr.LoginFrm;
import javax.swing.*;
import java.awt.event.*;
/**
* LoginMessangeExpection 类表示一个登录失败的对话框,用于显示错误信息。
*/
public class LoginMessangeExpection extends JDialog {
// 对话框的内容面板
private JPanel contentPane;
// 确认按钮
private JButton buttonOK;
/**
* 构造函数,初始化 LoginMessangeExpection 对话框。
*/
public LoginMessangeExpection() {
// 设置对话框的内容面板
setContentPane(contentPane);
// 设置对话框为模态对话框
setModal(true);
// 设置默认按钮为 buttonOK
getRootPane().setDefaultButton(buttonOK);
// 设置对话框不可调整大小
setResizable(false);
// 设置对话框的大小
setSize(200, 150);
// 设置对话框的标题
setTitle("登录失败");
// 为 buttonOK 添加动作监听器
buttonOK.addActionListener(new ActionListener() {
/**
* 当按钮被点击时调用的方法。
*
* @param e 动作事件
*/
public void actionPerformed(ActionEvent e) {
// 调用 onOK 方法
onOK();
// 调用 onCancel 方法
onCancel();
}
});
@@ -26,26 +48,46 @@ public class LoginMessangeExpection extends JDialog {
// 点击 X 时调用 onCancel()
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
/**
* 当窗口关闭事件发生时调用的方法。
*
* @param e 窗口事件
*/
public void windowClosing(WindowEvent e) {
// 调用 onCancel 方法
onCancel();
}
});
// 遇到 ESCAPE 时调用 onCancel()
contentPane.registerKeyboardAction(new ActionListener() {
/**
* 当按键事件发生时调用的方法。
*
* @param e 动作事件
*/
public void actionPerformed(ActionEvent e) {
// 调用 onCancel 方法
onCancel();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
}
/**
* 当用户点击确认按钮时调用的方法。
*/
private void onOK() {
// 在此处添加您的代码
// 关闭对话框
dispose();
}
/**
* 当用户取消操作时调用的方法。
*/
private void onCancel() {
// 必要时在此处添加您的代码
// 关闭对话框
dispose();
}
}
+12
View File
@@ -2,14 +2,26 @@ package com.msksbr.LoginFrm;
import java.awt.*;
/**
* ScreenSize 类用于获取屏幕的尺寸信息。
*/
public class ScreenSize {
// 屏幕的宽度
public int width;
// 屏幕的高度
public int height;
// 屏幕的尺寸对象
public Dimension screenSize;
/**
* 构造函数,初始化 ScreenSize 对象并获取屏幕尺寸。
*/
public ScreenSize() {
// 获取默认工具包的屏幕尺寸
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// 设置屏幕宽度
width = screenSize.width;
// 设置屏幕高度
height = screenSize.height;
}
}
+10 -1
View File
@@ -4,10 +4,19 @@ import com.formdev.flatlaf.FlatDarculaLaf;
import com.msksbr.LoginFrm.LoginFrm;
/**
* 主类,程序入口
*/
public class Main {
/**
* 主方法,程序入口点
*
* @param args 命令行参数
*/
public static void main(String[] args) {
// 安装FlatDarcula外观
FlatDarculaLaf.install();
// 创建并显示登录窗口
new LoginFrm();
}
}
+34 -1
View File
@@ -2,23 +2,56 @@ package com.msksbr.SQL;
import java.sql.*;
/**
* 数据库连接类,用于连接到MySQL数据库并执行SQL查询和更新操作。
*/
public class Connector {
// 数据库连接对象
private Connection conn;
// SQL语句执行对象
private Statement stmt;
private String url = "jdbc:mysql://localhost:3306/book";
// 数据库连接URL
private String url = "jdbc:mysql://192.168.154.139:3306/book";
// 数据库用户名
private String user = "root";
/**
* 构造方法,初始化数据库连接。
*
* @throws ClassNotFoundException 如果找不到MySQL驱动类。
* @throws SQLException 如果数据库连接失败。
*/
public Connector() throws ClassNotFoundException, SQLException {
// 加载MySQL驱动类
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection(url, user, new PassWord().passWord);
// 创建SQL语句执行对象
stmt = conn.createStatement();
}
/**
* 执行SQL查询操作。
*
* @param query 要执行的SQL查询语句。
* @return 查询结果集。
* @throws SQLException 如果查询执行失败。
*/
public ResultSet executeQuery(String query) throws SQLException {
// 执行SQL查询并返回结果集
return stmt.executeQuery(query);
}
/**
* 执行SQL更新操作。
*
* @param query 要执行的SQL更新语句。
* @return 受影响的行数。
* @throws SQLException 如果更新执行失败。
*/
public int executeUpdate(String query) throws SQLException {
// 执行SQL更新并返回受影响的行数
return stmt.executeUpdate(query);
}
}
+33
View File
@@ -6,36 +6,69 @@ import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* EasterEgg 类表示一个彩蛋对话框,用于显示特定的图像。
*/
public class EasterEgg extends JDialog {
// 对话框的内容面板
private JPanel contentPane;
// 确认按钮
private JButton buttonOK;
// 用于显示图像的标签
private JLabel imageLable;
/**
* 构造函数,初始化 EasterEgg 对话框。
*/
public EasterEgg() {
// 设置对话框的标题
setTitle("Easter Egg!");
// 设置对话框的内容面板
setContentPane(contentPane);
// 设置对话框为模态对话框
setModal(true);
// 设置默认按钮为 buttonOK
getRootPane().setDefaultButton(buttonOK);
// 设置 imageLable 的图标为指定路径的图像
imageLable.setIcon(new ImageIcon("src/com/msksbr/images/BuzzBeans.png"));
// 为 buttonOK 添加动作监听器
buttonOK.addActionListener(new ActionListener() {
/**
* 当按钮被点击时调用的方法。
*
* @param e 动作事件
*/
public void actionPerformed(ActionEvent e) {
// 调用 onOK 方法
onOK();
}
});
// 调整对话框的大小以适应其内容
pack();
// 设置对话框不可调整大小
setResizable(false);
// 设置对话框的位置
setLocation();
// 设置对话框可见
setVisible(true);
}
/**
* 当用户点击确认按钮时调用的方法。
*/
private void onOK() {
// 在此处添加您的代码
// 关闭对话框
dispose();
}
/**
* 设置对话框的位置为屏幕中心。
*/
public void setLocation() {
// 设置对话框的位置为屏幕中心
setLocation(new ScreenSize().width / 2 - getWidth() / 2, new ScreenSize().height / 2 - getHeight() / 2);
}
}