写一个实现GUI界面操作的图书管理系统代码JAVA
时间: 2023-12-14 09:40:13 浏览: 37
以下是一个简单的图书管理系统的GUI界面代码示例,包括添加、修改、删除和搜索图书等功能。
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BookManagementSystem extends JFrame implements ActionListener {
private JTextField bookNameField, authorField, publisherField, priceField, searchField;
private JButton addButton, modifyButton, deleteButton, searchButton;
private JLabel bookNameLabel, authorLabel, publisherLabel, priceLabel, searchLabel;
private JTable bookTable;
private JScrollPane scrollPane;
public BookManagementSystem() {
super("图书管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
bookNameLabel = new JLabel("书名:");
bookNameField = new JTextField(10);
add(bookNameLabel);
add(bookNameField);
authorLabel = new JLabel("作者:");
authorField = new JTextField(10);
add(authorLabel);
add(authorField);
publisherLabel = new JLabel("出版社:");
publisherField = new JTextField(10);
add(publisherLabel);
add(publisherField);
priceLabel = new JLabel("价格:");
priceField = new JTextField(10);
add(priceLabel);
add(priceField);
addButton = new JButton("添加");
addButton.addActionListener(this);
add(addButton);
modifyButton = new JButton("修改");
modifyButton.addActionListener(this);
add(modifyButton);
deleteButton = new JButton("删除");
deleteButton.addActionListener(this);
add(deleteButton);
searchLabel = new JLabel("搜索:");
searchField = new JTextField(10);
add(searchLabel);
add(searchField);
searchButton = new JButton("搜索");
searchButton.addActionListener(this);
add(searchButton);
String[] columnNames = {"书名", "作者", "出版社", "价格"};
Object[][] data = {
{"Java编程思想", "Bruce Eckel", "机械工业出版社", 108.0},
{"Head First Java", "Kathy Sierra", "中国电力出版社", 89.0},
{"Thinking in Java", "Bruce Eckel", "电子工业出版社", 98.0},
{"设计模式", "Erich Gamma", "Addison-Wesley", 68.0},
{"计算机网络", "谢希仁", "机械工业出版社", 89.0}
};
bookTable = new JTable(data, columnNames);
scrollPane = new JScrollPane(bookTable);
add(scrollPane);
pack();
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String[] newRow = {
bookNameField.getText(),
authorField.getText(),
publisherField.getText(),
priceField.getText()
};
((DefaultTableModel) bookTable.getModel()).addRow(newRow);
} else if (e.getSource() == modifyButton) {
int row = bookTable.getSelectedRow();
if (row != -1) {
bookTable.setValueAt(bookNameField.getText(), row, 0);
bookTable.setValueAt(authorField.getText(), row, 1);
bookTable.setValueAt(publisherField.getText(), row, 2);
bookTable.setValueAt(priceField.getText(), row, 3);
}
} else if (e.getSource() == deleteButton) {
int row = bookTable.getSelectedRow();
if (row != -1) {
((DefaultTableModel) bookTable.getModel()).removeRow(row);
}
} else if (e.getSource() == searchButton) {
String keyword = searchField.getText();
if (keyword != null && !keyword.isEmpty()) {
for (int i = 0; i < bookTable.getRowCount(); i++) {
for (int j = 0; j < bookTable.getColumnCount(); j++) {
String value = bookTable.getValueAt(i, j).toString();
if (value.contains(keyword)) {
bookTable.setRowSelectionInterval(i, i);
bookTable.setColumnSelectionInterval(j, j);
return;
}
}
}
JOptionPane.showMessageDialog(this, "未找到匹配的记录!");
}
}
}
public static void main(String[] args) {
new BookManagementSystem();
}
}
```