用java写一个基于java swing的图书管理系统
时间: 2023-09-14 15:14:07 浏览: 94
基于Java与Swing的图书管理系统的设计与实现
好的,以下是一个简单的基于Java Swing的图书管理系统示例:
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class LibraryManagementSystem extends JFrame implements ActionListener {
private JLabel headerLabel;
private JPanel mainPanel;
private JTextField titleField, authorField, yearField;
private JButton addButton, removeButton, updateButton;
private JTable table;
private DefaultTableModel model;
private ArrayList<Book> books;
public LibraryManagementSystem() {
super("图书管理系统");
// 初始化图书列表
books = new ArrayList<>();
books.add(new Book("《Java编程思想》", "Bruce Eckel", "2002"));
books.add(new Book("《Java核心技术》", "Cay S. Horstmann", "2016"));
books.add(new Book("《深入浅出设计模式》", "程杰", "2015"));
// 初始化界面
initUI();
setSize(600, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void initUI() {
// 创建标题
headerLabel = new JLabel("图书列表");
headerLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
headerLabel.setHorizontalAlignment(SwingConstants.CENTER);
// 创建表格
model = new DefaultTableModel();
model.addColumn("书名");
model.addColumn("作者");
model.addColumn("出版年份");
for (Book book : books) {
model.addRow(new Object[]{book.getTitle(), book.getAuthor(), book.getYear()});
}
table = new JTable(model);
// 创建添加按钮
addButton = new JButton("添加");
addButton.addActionListener(this);
// 创建删除按钮
removeButton = new JButton("删除");
removeButton.addActionListener(this);
// 创建更新按钮
updateButton = new JButton("更新");
updateButton.addActionListener(this);
// 创建输入框和标签
titleField = new JTextField();
authorField = new JTextField();
yearField = new JTextField();
JLabel titleLabel = new JLabel("书名:");
JLabel authorLabel = new JLabel("作者:");
JLabel yearLabel = new JLabel("出版年份:");
// 创建主面板
mainPanel = new JPanel(new BorderLayout());
JPanel topPanel = new JPanel(new GridLayout(2, 3, 5, 5));
topPanel.add(titleLabel);
topPanel.add(titleField);
topPanel.add(addButton);
topPanel.add(authorLabel);
topPanel.add(authorField);
topPanel.add(removeButton);
topPanel.add(yearLabel);
topPanel.add(yearField);
topPanel.add(updateButton);
mainPanel.add(headerLabel, BorderLayout.NORTH);
mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);
mainPanel.add(topPanel, BorderLayout.SOUTH);
// 添加主面板到窗口中
add(mainPanel);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
// 添加新书
String title = titleField.getText();
String author = authorField.getText();
String year = yearField.getText();
if (!title.isEmpty() && !author.isEmpty() && !year.isEmpty()) {
books.add(new Book(title, author, year));
model.addRow(new Object[]{title, author, year});
titleField.setText("");
authorField.setText("");
yearField.setText("");
}
} else if (e.getSource() == removeButton) {
// 删除选中的书
int[] selectedRows = table.getSelectedRows();
for (int i = selectedRows.length - 1; i >= 0; i--) {
books.remove(selectedRows[i]);
model.removeRow(selectedRows[i]);
}
} else if (e.getSource() == updateButton) {
// 更新选中的书
int row = table.getSelectedRow();
if (row >= 0) {
String title = titleField.getText();
String author = authorField.getText();
String year = yearField.getText();
if (!title.isEmpty() && !author.isEmpty() && !year.isEmpty()) {
Book book = books.get(row);
book.setTitle(title);
book.setAuthor(author);
book.setYear(year);
model.setValueAt(title, row, 0);
model.setValueAt(author, row, 1);
model.setValueAt(year, row, 2);
}
}
}
}
public static void main(String[] args) {
new LibraryManagementSystem();
}
}
class Book {
private String title;
private String author;
private String year;
public Book(String title, String author, String year) {
this.title = title;
this.author = author;
this.year = year;
}
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 getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
}
```
这个图书管理系统使用了Java Swing库来创建界面,可以添加、删除和更新图书。它包含一个Book类来表示每个书籍的属性,并使用一个ArrayList来存储所有的书籍。控制器是LibraryManagementSystem类,负责处理用户与图书列表的交互并更新界面。
阅读全文