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