使用Java语言写一套swing图书管理系统
时间: 2023-07-16 12:14:20 浏览: 87
好的,下面是一个简单的Swing图书管理系统的示例代码,包括添加书籍、删除书籍、查找书籍和显示所有书籍等功能。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
public class BookManagementSystem extends JFrame implements ActionListener {
private JPanel panel;
private JLabel titleLabel, authorLabel, yearLabel;
private JTextField titleText, authorText, yearText;
private JButton addButton, deleteButton, searchButton, showAllButton;
private ArrayList<Book> bookList;
public BookManagementSystem() {
super("图书管理系统");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
// 初始化图书列表
bookList = new ArrayList<Book>();
// 添加图书表单
panel = new JPanel(new GridLayout(4, 2));
titleLabel = new JLabel("书名:");
titleText = new JTextField(20);
authorLabel = new JLabel("作者:");
authorText = new JTextField(20);
yearLabel = new JLabel("出版年份:");
yearText = new JTextField(20);
addButton = new JButton("添加");
addButton.addActionListener(this);
deleteButton = new JButton("删除");
deleteButton.addActionListener(this);
searchButton = new JButton("查找");
searchButton.addActionListener(this);
showAllButton = new JButton("显示所有");
showAllButton.addActionListener(this);
panel.add(titleLabel);
panel.add(titleText);
panel.add(authorLabel);
panel.add(authorText);
panel.add(yearLabel);
panel.add(yearText);
panel.add(addButton);
panel.add(deleteButton);
panel.add(searchButton);
panel.add(showAllButton);
add(panel);
setVisible(true);
}
// 处理按钮点击事件
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
// 添加图书
String title = titleText.getText();
String author = authorText.getText();
int year = Integer.parseInt(yearText.getText());
Book book = new Book(title, author, year);
bookList.add(book);
JOptionPane.showMessageDialog(null, "添加成功!");
} else if (e.getSource() == deleteButton) {
// 删除图书
String title = JOptionPane.showInputDialog("请输入要删除的书名:");
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getTitle().equals(title)) {
bookList.remove(i);
JOptionPane.showMessageDialog(null, "删除成功!");
return;
}
}
JOptionPane.showMessageDialog(null, "未找到该书籍!");
} else if (e.getSource() == searchButton) {
// 查找图书
String title = JOptionPane.showInputDialog("请输入要查找的书名:");
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getTitle().equals(title)) {
JOptionPane.showMessageDialog(null, bookList.get(i).toString());
return;
}
}
JOptionPane.showMessageDialog(null, "未找到该书籍!");
} else if (e.getSource() == showAllButton) {
// 显示所有图书
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bookList.size(); i++) {
sb.append(bookList.get(i).toString() + "\n");
}
JOptionPane.showMessageDialog(null, sb.toString());
}
}
public static void main(String[] args) {
new BookManagementSystem();
}
}
class Book {
private String title;
private String author;
private int year;
public Book(String title, String author, int year) {
this.title = title;
this.author = author;
this.year = year;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getYear() {
return year;
}
public String toString() {
return "书名:" + title + " 作者:" + author + " 出版年份:" + year;
}
}
```
这是一个简单的Swing图书管理系统,它使用JFrame和JPanel创建GUI,使用JButton等组件添加各种操作。同时,该系统还创建了一个Book类来存储每本书的信息。
阅读全文