You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Library/LibrarySystemGUI.java

639 lines
23 KiB

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
// 图书类
class Book {
private String id;
private String title;
private String author;
private String isbn;
private String category;
private boolean available;
public Book(String id, String title, String author, String isbn, String category) {
this.id = id;
this.title = title;
this.author = author;
this.isbn = isbn;
this.category = category;
this.available = true;
}
// Getter和Setter方法
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
public String getIsbn() { return isbn; }
public void setIsbn(String isbn) { this.isbn = isbn; }
public String getCategory() { return category; }
public void setCategory(String category) { this.category = category; }
public boolean isAvailable() { return available; }
public void setAvailable(boolean available) { this.available = available; }
@Override
public String toString() {
return "ID: " + id + ", 书名: " + title + ", 作者: " + author +
", ISBN: " + isbn + ", 类别: " + category +
", 状态: " + (available ? "可借" : "已借出");
}
}
// 用户类
class User {
private String username;
private String password;
private String email;
public User(String username, String password, String email) {
this.username = username;
this.password = password;
this.email = email;
}
// Getter和Setter方法
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
}
// 图书管理系统类
class LibraryManagementSystem {
private List<Book> books;
private List<User> users;
private User currentUser;
public LibraryManagementSystem() {
books = new ArrayList<>();
users = new ArrayList<>();
// 添加一些初始图书
books.add(new Book("B001", "Java编程思想", "Bruce Eckel", "9787111213826", "编程"));
books.add(new Book("B002", "设计模式", "Erich Gamma", "9787111075647", "编程"));
books.add(new Book("B003", "算法导论", "Thomas H. Cormen", "9787111407010", "算法"));
}
// 用户注册
public boolean registerUser(String username, String password, String email) {
// 检查用户名是否已存在
for (User user : users) {
if (user.getUsername().equals(username)) {
return false;
}
}
users.add(new User(username, password, email));
return true;
}
// 用户登录
public boolean loginUser(String username, String password) {
for (User user : users) {
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
currentUser = user;
return true;
}
}
return false;
}
// 获取当前用户
public User getCurrentUser() {
return currentUser;
}
// 添加图书
public boolean addBook(Book book) {
// 检查ID是否已存在
for (Book b : books) {
if (b.getId().equals(book.getId())) {
return false;
}
}
books.add(book);
return true;
}
// 删除图书
public boolean deleteBook(String bookId) {
for (int i = 0; i < books.size(); i++) {
if (books.get(i).getId().equals(bookId)) {
books.remove(i);
return true;
}
}
return false;
}
// 更新图书
public boolean updateBook(String bookId, Book updatedBook) {
for (int i = 0; i < books.size(); i++) {
if (books.get(i).getId().equals(bookId)) {
books.set(i, updatedBook);
return true;
}
}
return false;
}
// 查找图书
public List<Book> searchBooks(String keyword) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
if (book.getTitle().contains(keyword) ||
book.getAuthor().contains(keyword) ||
book.getCategory().contains(keyword) ||
book.getId().contains(keyword)) {
result.add(book);
}
}
return result;
}
// 获取所有图书
public List<Book> getAllBooks() {
return new ArrayList<>(books);
}
// 根据ID获取图书
public Book getBookById(String bookId) {
for (Book book : books) {
if (book.getId().equals(bookId)) {
return book;
}
}
return null;
}
}
// 主界面
public class LibrarySystemGUI extends JFrame {
private LibraryManagementSystem librarySystem;
private JPanel mainPanel;
private CardLayout cardLayout;
// 登录面板组件
private JTextField loginUsernameField;
private JPasswordField loginPasswordField;
// 注册面板组件
private JTextField registerUsernameField;
private JPasswordField registerPasswordField;
private JTextField registerEmailField;
// 主功能面板组件
private JTextArea bookListArea;
private JTextField searchField;
private DefaultListModel<String> bookListModel;
private JList<String> bookList;
public LibrarySystemGUI() {
librarySystem = new LibraryManagementSystem();
initializeGUI();
}
private void initializeGUI() {
setTitle("图书管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
// 创建主面板和卡片布局
cardLayout = new CardLayout();
mainPanel = new JPanel(cardLayout);
// 添加各个面板
mainPanel.add(createLoginPanel(), "login");
mainPanel.add(createRegisterPanel(), "register");
mainPanel.add(createMainPanel(), "main");
add(mainPanel);
cardLayout.show(mainPanel, "login");
setVisible(true);
}
// 创建登录面板
private JPanel createLoginPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// 标题
JLabel titleLabel = new JLabel("图书管理系统", JLabel.CENTER);
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
panel.add(titleLabel, BorderLayout.NORTH);
// 表单面板
JPanel formPanel = new JPanel(new GridLayout(3, 2, 10, 10));
formPanel.setBorder(BorderFactory.createEmptyBorder(50, 100, 50, 100));
JLabel usernameLabel = new JLabel("用户名:");
loginUsernameField = new JTextField();
JLabel passwordLabel = new JLabel("密码:");
loginPasswordField = new JPasswordField();
formPanel.add(usernameLabel);
formPanel.add(loginUsernameField);
formPanel.add(passwordLabel);
formPanel.add(loginPasswordField);
// 按钮面板
JPanel buttonPanel = new JPanel(new FlowLayout());
JButton loginButton = new JButton("登录");
JButton registerButton = new JButton("注册");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = loginUsernameField.getText();
String password = new String(loginPasswordField.getPassword());
if (username.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(LibrarySystemGUI.this,
"用户名和密码不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
if (librarySystem.loginUser(username, password)) {
JOptionPane.showMessageDialog(LibrarySystemGUI.this,
"登录成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
refreshBookList();
cardLayout.show(mainPanel, "main");
} else {
JOptionPane.showMessageDialog(LibrarySystemGUI.this,
"用户名或密码错误!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(mainPanel, "register");
}
});
buttonPanel.add(loginButton);
buttonPanel.add(registerButton);
formPanel.add(new JLabel()); // 占位
formPanel.add(buttonPanel);
panel.add(formPanel, BorderLayout.CENTER);
return panel;
}
// 创建注册面板
private JPanel createRegisterPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// 标题
JLabel titleLabel = new JLabel("用户注册", JLabel.CENTER);
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
panel.add(titleLabel, BorderLayout.NORTH);
// 表单面板
JPanel formPanel = new JPanel(new GridLayout(4, 2, 10, 10));
formPanel.setBorder(BorderFactory.createEmptyBorder(50, 100, 50, 100));
JLabel usernameLabel = new JLabel("用户名:");
registerUsernameField = new JTextField();
JLabel passwordLabel = new JLabel("密码:");
registerPasswordField = new JPasswordField();
JLabel emailLabel = new JLabel("邮箱:");
registerEmailField = new JTextField();
formPanel.add(usernameLabel);
formPanel.add(registerUsernameField);
formPanel.add(passwordLabel);
formPanel.add(registerPasswordField);
formPanel.add(emailLabel);
formPanel.add(registerEmailField);
// 按钮面板
JPanel buttonPanel = new JPanel(new FlowLayout());
JButton registerButton = new JButton("注册");
JButton backButton = new JButton("返回登录");
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = registerUsernameField.getText();
String password = new String(registerPasswordField.getPassword());
String email = registerEmailField.getText();
if (username.isEmpty() || password.isEmpty() || email.isEmpty()) {
JOptionPane.showMessageDialog(LibrarySystemGUI.this,
"所有字段都不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
if (librarySystem.registerUser(username, password, email)) {
JOptionPane.showMessageDialog(LibrarySystemGUI.this,
"注册成功! 请登录.", "成功", JOptionPane.INFORMATION_MESSAGE);
cardLayout.show(mainPanel, "login");
} else {
JOptionPane.showMessageDialog(LibrarySystemGUI.this,
"用户名已存在!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
backButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(mainPanel, "login");
}
});
buttonPanel.add(registerButton);
buttonPanel.add(backButton);
formPanel.add(new JLabel()); // 占位
formPanel.add(buttonPanel);
panel.add(formPanel, BorderLayout.CENTER);
return panel;
}
// 创建主功能面板
private JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
// 顶部面板 - 欢迎信息和搜索
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel welcomeLabel = new JLabel("欢迎使用图书管理系统");
welcomeLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
topPanel.add(welcomeLabel, BorderLayout.WEST);
JPanel searchPanel = new JPanel(new FlowLayout());
searchField = new JTextField(20);
JButton searchButton = new JButton("搜索");
JButton logoutButton = new JButton("退出登录");
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String keyword = searchField.getText();
if (keyword.isEmpty()) {
refreshBookList();
} else {
List<Book> searchResult = librarySystem.searchBooks(keyword);
updateBookList(searchResult);
}
}
});
logoutButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(LibrarySystemGUI.this,
"确定要退出登录吗?", "确认", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
cardLayout.show(mainPanel, "login");
// 清空登录信息
loginUsernameField.setText("");
loginPasswordField.setText("");
}
}
});
searchPanel.add(new JLabel("搜索:"));
searchPanel.add(searchField);
searchPanel.add(searchButton);
searchPanel.add(logoutButton);
topPanel.add(searchPanel, BorderLayout.EAST);
panel.add(topPanel, BorderLayout.NORTH);
// 中央面板 - 图书列表
JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
bookListModel = new DefaultListModel<>();
bookList = new JList<>(bookListModel);
JScrollPane scrollPane = new JScrollPane(bookList);
centerPanel.add(new JLabel("图书列表:"), BorderLayout.NORTH);
centerPanel.add(scrollPane, BorderLayout.CENTER);
panel.add(centerPanel, BorderLayout.CENTER);
// 底部面板 - 功能按钮
JPanel bottomPanel = new JPanel(new FlowLayout());
bottomPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JButton addButton = new JButton("添加图书");
JButton editButton = new JButton("编辑图书");
JButton deleteButton = new JButton("删除图书");
JButton refreshButton = new JButton("刷新列表");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showAddBookDialog();
}
});
editButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedIndex = bookList.getSelectedIndex();
if (selectedIndex == -1) {
JOptionPane.showMessageDialog(LibrarySystemGUI.this,
"请先选择一本图书!", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
String selectedBookInfo = bookListModel.getElementAt(selectedIndex);
String bookId = selectedBookInfo.split(",")[0].split(":")[1].trim();
Book book = librarySystem.getBookById(bookId);
if (book != null) {
showEditBookDialog(book);
}
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedIndex = bookList.getSelectedIndex();
if (selectedIndex == -1) {
JOptionPane.showMessageDialog(LibrarySystemGUI.this,
"请先选择一本图书!", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
String selectedBookInfo = bookListModel.getElementAt(selectedIndex);
String bookId = selectedBookInfo.split(",")[0].split(":")[1].trim();
int result = JOptionPane.showConfirmDialog(LibrarySystemGUI.this,
"确定要删除这本图书吗?", "确认删除", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
if (librarySystem.deleteBook(bookId)) {
JOptionPane.showMessageDialog(LibrarySystemGUI.this,
"图书删除成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
refreshBookList();
} else {
JOptionPane.showMessageDialog(LibrarySystemGUI.this,
"图书删除失败!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
});
refreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
refreshBookList();
}
});
bottomPanel.add(addButton);
bottomPanel.add(editButton);
bottomPanel.add(deleteButton);
bottomPanel.add(refreshButton);
panel.add(bottomPanel, BorderLayout.SOUTH);
return panel;
}
// 显示添加图书对话框
private void showAddBookDialog() {
JTextField idField = new JTextField();
JTextField titleField = new JTextField();
JTextField authorField = new JTextField();
JTextField isbnField = new JTextField();
JTextField categoryField = new JTextField();
JPanel panel = new JPanel(new GridLayout(5, 2, 10, 10));
panel.add(new JLabel("图书ID:"));
panel.add(idField);
panel.add(new JLabel("书名:"));
panel.add(titleField);
panel.add(new JLabel("作者:"));
panel.add(authorField);
panel.add(new JLabel("ISBN:"));
panel.add(isbnField);
panel.add(new JLabel("类别:"));
panel.add(categoryField);
int result = JOptionPane.showConfirmDialog(this, panel, "添加图书",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String id = idField.getText();
String title = titleField.getText();
String author = authorField.getText();
String isbn = isbnField.getText();
String category = categoryField.getText();
if (id.isEmpty() || title.isEmpty() || author.isEmpty() ||
isbn.isEmpty() || category.isEmpty()) {
JOptionPane.showMessageDialog(this, "所有字段都不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
Book newBook = new Book(id, title, author, isbn, category);
if (librarySystem.addBook(newBook)) {
JOptionPane.showMessageDialog(this, "图书添加成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
refreshBookList();
} else {
JOptionPane.showMessageDialog(this, "图书ID已存在!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
// 显示编辑图书对话框
private void showEditBookDialog(Book book) {
JTextField idField = new JTextField(book.getId());
idField.setEditable(false); // ID不可编辑
JTextField titleField = new JTextField(book.getTitle());
JTextField authorField = new JTextField(book.getAuthor());
JTextField isbnField = new JTextField(book.getIsbn());
JTextField categoryField = new JTextField(book.getCategory());
JPanel panel = new JPanel(new GridLayout(5, 2, 10, 10));
panel.add(new JLabel("图书ID:"));
panel.add(idField);
panel.add(new JLabel("书名:"));
panel.add(titleField);
panel.add(new JLabel("作者:"));
panel.add(authorField);
panel.add(new JLabel("ISBN:"));
panel.add(isbnField);
panel.add(new JLabel("类别:"));
panel.add(categoryField);
int result = JOptionPane.showConfirmDialog(this, panel, "编辑图书",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String title = titleField.getText();
String author = authorField.getText();
String isbn = isbnField.getText();
String category = categoryField.getText();
if (title.isEmpty() || author.isEmpty() || isbn.isEmpty() || category.isEmpty()) {
JOptionPane.showMessageDialog(this, "所有字段都不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
Book updatedBook = new Book(book.getId(), title, author, isbn, category);
updatedBook.setAvailable(book.isAvailable());
if (librarySystem.updateBook(book.getId(), updatedBook)) {
JOptionPane.showMessageDialog(this, "图书更新成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
refreshBookList();
} else {
JOptionPane.showMessageDialog(this, "图书更新失败!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
// 刷新图书列表
private void refreshBookList() {
List<Book> books = librarySystem.getAllBooks();
updateBookList(books);
}
// 更新图书列表显示
private void updateBookList(List<Book> books) {
bookListModel.clear();
for (Book book : books) {
bookListModel.addElement(book.toString());
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new LibrarySystemGUI();
}
});
}
}