javafx图书管理系统完整代码
时间: 2025-06-06 22:02:36 浏览: 21
<think>我们正在寻找一个使用JavaFX实现的图书管理系统的完整代码示例。根据之前引用的内容,我们知道面向对象的解决方案会创建Book类和BookManager类。在JavaFX中,我们还需要考虑用户界面的实现。由于提供完整的代码可能会很长,而且通常一个完整的系统包含多个类,这里我将提供一个简化但完整的示例,包括以下部分:1.Book类:表示图书,包含属性如书名、作者、ISBN等。2.BookManager类:管理图书的增删改查。3.主界面:使用JavaFX构建,包括表格显示图书列表,以及添加、删除、修改图书的按钮和表单。注意:为了简化,我们不使用数据库,而是使用内存中的列表来存储图书。我们将分步骤构建:步骤1:创建Book类步骤2:创建BookManager类(这里我们将其功能合并到主程序的数据模型中,以简化)步骤3:创建JavaFX主界面考虑到代码长度,我们将在一个主类中完成,但将界面和逻辑分开。这里提供一个简化的版本,仅包括核心功能:-显示图书列表-添加图书-删除图书我们将使用TableView来显示图书列表。代码如下:</think>
### JavaFX 图书管理系统完整代码示例
以下是一个基于JavaFX实现的简化版图书管理系统完整代码示例,采用了MVC设计模式,包含核心功能和UI界面:
#### 1. 主程序入口 `Main.java`
```java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("main-view.fxml"));
primaryStage.setTitle("图书管理系统");
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
#### 2. 图书实体类 `Book.java`
```java
public class Book {
private String isbn;
private String title;
private String author;
private String category;
private boolean available;
// 构造方法
public Book(String isbn, String title, String author, String category) {
this.isbn = isbn;
this.title = title;
this.author = author;
this.category = category;
this.available = true;
}
// Getter和Setter方法
public String getIsbn() { return isbn; }
public void setIsbn(String isbn) { this.isbn = isbn; }
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 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 title + " by " + author + " [" + isbn + "]";
}
}
```
#### 3. 数据管理类 `BookManager.java`
```java
import java.util.ArrayList;
import java.util.List;
public class BookManager {
private List<Book> books = new ArrayList<>();
// 添加新书
public void addBook(Book book) {
books.add(book);
}
// 根据ISBN删除图书
public boolean deleteBook(String isbn) {
return books.removeIf(book -> book.getIsbn().equals(isbn));
}
// 搜索图书
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.getIsbn().equals(keyword)) {
result.add(book);
}
}
return result;
}
// 获取所有图书
public List<Book> getAllBooks() {
return new ArrayList<>(books);
}
}
```
#### 4. 主界面控制器 `MainController.java`
```java
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
public class MainController {
@FXML private TableView<Book> booksTable;
@FXML private TableColumn<Book, String> isbnColumn;
@FXML private TableColumn<Book, String> titleColumn;
@FXML private TableColumn<Book, String> authorColumn;
@FXML private TableColumn<Book, String> categoryColumn;
@FXML private TableColumn<Book, Boolean> statusColumn;
@FXML private TextField isbnField;
@FXML private TextField titleField;
@FXML private TextField authorField;
@FXML private TextField categoryField;
@FXML private TextField searchField;
private BookManager manager = new BookManager();
@FXML
public void initialize() {
// 配置表格列
isbnColumn.setCellValueFactory(new PropertyValueFactory<>("isbn"));
titleColumn.setCellValueFactory(new PropertyValueFactory<>("title"));
authorColumn.setCellValueFactory(new PropertyValueFactory<>("author"));
categoryColumn.setCellValueFactory(new PropertyValueFactory<>("category"));
statusColumn.setCellValueFactory(new PropertyValueFactory<>("available"));
// 初始数据加载
loadSampleData();
}
private void loadSampleData() {
manager.addBook(new Book("9787111636667", "深入理解Java虚拟机", "周志明", "计算机"));
manager.addBook(new Book("9787302517592", "Java核心技术 卷I", "Cay S. Horstmann", "编程"));
refreshTable();
}
@FXML
private void handleAddBook() {
String isbn = isbnField.getText();
String title = titleField.getText();
String author = authorField.getText();
String category = categoryField.getText();
if (!isbn.isEmpty() && !title.isEmpty()) {
manager.addBook(new Book(isbn, title, author, category));
clearFields();
refreshTable();
} else {
showAlert("输入错误", "ISBN和书名不能为空");
}
}
@FXML
private void handleDeleteBook() {
Book selected = booksTable.getSelectionModel().getSelectedItem();
if (selected != null) {
manager.deleteBook(selected.getIsbn());
refreshTable();
} else {
showAlert("删除错误", "请先在表格中选择一本书");
}
}
@FXML
private void handleSearch() {
String keyword = searchField.getText();
if (!keyword.isEmpty()) {
booksTable.setItems(FXCollections.observableArrayList(manager.searchBooks(keyword)));
} else {
refreshTable();
}
}
private void refreshTable() {
booksTable.setItems(FXCollections.observableArrayList(manager.getAllBooks()));
}
private void clearFields() {
isbnField.clear();
titleField.clear();
authorField.clear();
categoryField.clear();
}
private void showAlert(String title, String message) {
Alert alleet = new Alert(Alert.AlertType.ERROR);
alleet.setTitle(title);
alleet.setHeaderText(null);
alleet.setContentText(message);
alleet.showAndWait();
}
}
```
#### 5. FXML界面文件 `main-view.fxml`
```xml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<VBox spacing="10" xmlns="http://javafx.com/javafx/11.0.1"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="MainController">
<Text text="图书管理系统" style="-fx-font-size: 24pt; -fx-font-weight: bold;"/>
<HBox spacing="10">
<VBox spacing="5">
<Label text="ISBN:"/>
<TextField fx:id="isbnField" promptText="请输入ISBN"/>
<Label text="书名:"/>
<TextField fx:id="titleField" promptText="请输入书名"/>
<Label text="作者:"/>
<TextField fx:id="authorField" promptText="请输入作者"/>
<Label text="分类:"/>
<TextField fx:id="categoryField" promptText="请输入分类"/>
<Button text="添加图书" onAction="#handleAddBook" style="-fx-background-color: #4CAF50; -fx-text-fill: white;"/>
</VBox>
<TableView fx:id="booksTable" prefWidth="500">
<columns>
<TableColumn fx:id="isbnColumn" text="ISBN" prefWidth="120"/>
<TableColumn fx:id="titleColumn" text="书名" prefWidth="120"/>
<TableColumn fx:id="authorColumn" text="作者" prefWidth="100"/>
<TableColumn fx:id="categoryColumn" text="分类" prefWidth="80"/>
<TableColumn fx:id="statusColumn" text="状态" prefWidth="60"/>
</columns>
</TableView>
</HBox>
<HBox spacing="10">
<TextField fx:id="searchField" promptText="搜索书籍..." prefWidth="300"/>
<Button text="搜索" onAction="#handleSearch" style="-fx-background-color: #2196F3; -fx-text-fill: white;"/>
<Button text="删除选中" onAction="#handleDeleteBook" style="-fx-background-color: #f44336; -fx-text-fill: white;"/>
</HBox>
</VBox>
```
### 功能说明
1. **核心功能**:
- 添加新书(ISBN、书名、作者、分类)
- 删除图书
- 图书搜索(按标题、作者或ISBN)
- 图书状态管理(可借阅状态)
2. **技术特点**:
- 使用JavaFX的TableView展示数据
- FXML实现UI与代码分离
- 面向对象设计(Book类封装图书数据)
- MVC架构模式
3. **扩展提示**:
- 增加持久化存储(使用SQLite或MySQL)
- 添加借阅管理功能
- 实现用户登录认证
- 添加分类统计和报表功能
这个示例包含完整的系统框架,可直接编译运行(需要JavaFX库)。实际生产环境还需要添加数据持久化、输入验证和异常处理等功能。
---
阅读全文
相关推荐



















