第13章 基于Java Swing的图书管理系统
由于原代码较长,我将提供一个简化版的图书管理系统框架示例,仅包含必要的组件和逻辑。这将帮助理解图书管理系统的基本结构,并且可以根据需要扩展功能。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
public class BookManagementSystem {
private JFrame mainFrame;
private JTextField bookTitleField;
private JTextField bookAuthorField;
private JTextArea bookListArea;
private List<Book> bookList;
public BookManagementSystem() {
prepareGUI();
}
private void prepareGUI() {
mainFrame = new JFrame("图书管理系统");
mainFrame.setSize(400, 400);
mainFrame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.add(new JLabel("书名:"));
bookTitleField = new JTextField(20);
panel.add(bookTitleField);
panel.add(new JLabel("作者:"));
bookAuthorField = new JTextField(20);
panel.add(bookAuthorField);
JButton addButton = new JButton("添加");
addButton.addActionListener(new AddBookListener());
panel.add(addButton);
mainFrame.add(panel, BorderLayout.NORTH);
bookListArea = new JTextArea();
mainFrame.add(new JScrollPane(bookListArea), BorderLayout.CENTER);
bookList = new ArrayList<>();
mainFrame.setVisible(true);
}
class AddBookListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String title = bookTitleField.getText();
String author = bookAuthorField.getText();
Book book = new Book(title, author);
bookList.add(book);
bookListArea.append(book.toString() + "\n");
bookTitleField.setText("");
bookAuthorField.setText("");
}
}
public static void main(String[] args) {
new BookManagementSystem();
}
static class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public String toString() {
retu
评论已关闭