帮我写一个java swing图书管理的代码
时间: 2023-08-20 13:25:11 浏览: 52
好的,以下是一个简单的Java Swing图书管理系统的代码,供您参考:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class BookManagementSystem extends JFrame implements ActionListener {
private JLabel titleLabel, bookNameLabel, authorLabel, publisherLabel, yearLabel, idLabel, searchLabel;
private JTextField bookNameTextField, authorTextField, publisherTextField, yearTextField, idTextField, searchTextField;
private JButton addButton, deleteButton, updateButton, searchButton, resetButton;
private JTable table;
private JScrollPane scrollPane;
private ArrayList<Book> bookList = new ArrayList<Book>();
public BookManagementSystem() {
setTitle("图书管理系统");
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
titleLabel = new JLabel("图书管理系统");
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
titleLabel.setBounds(300, 20, 200, 30);
add(titleLabel);
bookNameLabel = new JLabel("书名:");
bookNameLabel.setBounds(100, 80, 50, 20);
add(bookNameLabel);
bookNameTextField = new JTextField();
bookNameTextField.setBounds(150, 80, 150, 20);
add(bookNameTextField);
authorLabel = new JLabel("作者:");
authorLabel.setBounds(100, 120, 50, 20);
add(authorLabel);
authorTextField = new JTextField();
authorTextField.setBounds(150, 120, 150, 20);
add(authorTextField);
publisherLabel = new JLabel("出版社:");
publisherLabel.setBounds(100, 160, 50, 20);
add(publisherLabel);
publisherTextField = new JTextField();
publisherTextField.setBounds(150, 160, 150, 20);
add(publisherTextField);
yearLabel = new JLabel("年份:");
yearLabel.setBounds(100, 200, 50, 20);
add(yearLabel);
yearTextField = new JTextField();
yearTextField.setBounds(150, 200, 150, 20);
add(yearTextField);
idLabel = new JLabel("编号:");
idLabel.setBounds(100, 240, 50, 20);
add(idLabel);
idTextField = new JTextField();
idTextField.setBounds(150, 240, 150, 20);
add(idTextField);
addButton = new JButton("添加");
addButton.setBounds(350, 80, 80, 30);
addButton.addActionListener(this);
add(addButton);
deleteButton = new JButton("删除");
deleteButton.setBounds(350, 120, 80, 30);
deleteButton.addActionListener(this);
add(deleteButton);
updateButton = new JButton("修改");
updateButton.setBounds(350, 160, 80, 30);
updateButton.addActionListener(this);
add(updateButton);
searchLabel = new JLabel("搜索:");
searchLabel.setBounds(450, 80, 50, 20);
add(searchLabel);
searchTextField = new JTextField();
searchTextField.setBounds(500, 80, 150, 20);
add(searchTextField);
searchButton = new JButton("查找");
searchButton.setBounds(670, 80, 80, 30);
searchButton.addActionListener(this);
add(searchButton);
resetButton = new JButton("重置");
resetButton.setBounds(670, 120, 80, 30);
resetButton.addActionListener(this);
add(resetButton);
table = new JTable();
scrollPane = new JScrollPane(table);
scrollPane.setBounds(100, 280, 600, 200);
add(scrollPane);
setVisible(true);
}
public static void main(String[] args) {
new BookManagementSystem();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
Book book = new Book();
book.setName(bookNameTextField.getText());
book.setAuthor(authorTextField.getText());
book.setPublisher(publisherTextField.getText());
book.setYear(yearTextField.getText());
book.setId(idTextField.getText());
bookList.add(book);
Object[][] data = new Object[bookList.size()][5];
for (int i = 0; i < bookList.size(); i++) {
data[i][0] = bookList.get(i).getName();
data[i][1] = bookList.get(i).getAuthor();
data[i][2] = bookList.get(i).getPublisher();
data[i][3] = bookList.get(i).getYear();
data[i][4] = bookList.get(i).getId();
}
Object[] columnNames = {"书名", "作者", "出版社", "年份", "编号"};
table.setModel(new DefaultTableModel(data, columnNames));
} else if (e.getSource() == deleteButton) {
int row = table.getSelectedRow();
if (row != -1) {
String id = (String) table.getValueAt(row, 4);
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getId().equals(id)) {
bookList.remove(i);
break;
}
}
Object[][] data = new Object[bookList.size()][5];
for (int i = 0; i < bookList.size(); i++) {
data[i][0] = bookList.get(i).getName();
data[i][1] = bookList.get(i).getAuthor();
data[i][2] = bookList.get(i).getPublisher();
data[i][3] = bookList.get(i).getYear();
data[i][4] = bookList.get(i).getId();
}
Object[] columnNames = {"书名", "作者", "出版社", "年份", "编号"};
table.setModel(new DefaultTableModel(data, columnNames));
}
} else if (e.getSource() == updateButton) {
int row = table.getSelectedRow();
if (row != -1) {
String id = (String) table.getValueAt(row, 4);
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getId().equals(id)) {
bookList.get(i).setName(bookNameTextField.getText());
bookList.get(i).setAuthor(authorTextField.getText());
bookList.get(i).setPublisher(publisherTextField.getText());
bookList.get(i).setYear(yearTextField.getText());
bookList.get(i).setId(idTextField.getText());
break;
}
}
Object[][] data = new Object[bookList.size()][5];
for (int i = 0; i < bookList.size(); i++) {
data[i][0] = bookList.get(i).getName();
data[i][1] = bookList.get(i).getAuthor();
data[i][2] = bookList.get(i).getPublisher();
data[i][3] = bookList.get(i).getYear();
data[i][4] = bookList.get(i).getId();
}
Object[] columnNames = {"书名", "作者", "出版社", "年份", "编号"};
table.setModel(new DefaultTableModel(data, columnNames));
}
} else if (e.getSource() == searchButton) {
String keyword = searchTextField.getText();
ArrayList<Book> searchResult = new ArrayList<Book>();
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getName().contains(keyword) || bookList.get(i).getAuthor().contains(keyword) || bookList.get(i).getPublisher().contains(keyword) || bookList.get(i).getYear().contains(keyword) || bookList.get(i).getId().contains(keyword)) {
searchResult.add(bookList.get(i));
}
}
Object[][] data = new Object[searchResult.size()][5];
for (int i = 0; i < searchResult.size(); i++) {
data[i][0] = searchResult.get(i).getName();
data[i][1] = searchResult.get(i).getAuthor();
data[i][2] = searchResult.get(i).getPublisher();
data[i][3] = searchResult.get(i).getYear();
data[i][4] = searchResult.get(i).getId();
}
Object[] columnNames = {"书名", "作者", "出版社", "年份", "编号"};
table.setModel(new DefaultTableModel(data, columnNames));
} else if (e.getSource() == resetButton) {
bookNameTextField.setText("");
authorTextField.setText("");
publisherTextField.setText("");
yearTextField.setText("");
idTextField.setText("");
searchTextField.setText("");
Object[][] data = new Object[0][5];
Object[] columnNames = {"书名", "作者", "出版社", "年份", "编号"};
table.setModel(new DefaultTableModel(data, columnNames));
bookList.clear();
}
}
}
class Book {
private String name;
private String author;
private String publisher;
private String year;
private String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
```
这个代码实现了添加、删除、修改、搜索和重置功能,使用了JTable显示图书列表。当您运行这个程序时,可以在窗口上看到一个简单的图书管理界面。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)