java表格实现管理员对书籍的操作
时间: 2024-01-23 08:02:29 浏览: 142
好的,我可以帮你完成这个任务。假设你已经使用Java中的GUI库(如Swing或JavaFX)创建了一个表格用于存储书籍信息,你需要创建一个管理员界面,允许管理员对书籍进行操作。
下面是一个简单的示例代码,演示如何使用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 AdminGUI extends JFrame implements ActionListener {
private JTable table;
private DefaultTableModel model;
private ArrayList<Book> books;
public AdminGUI() {
setTitle("Admin Panel");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setLayout(new BorderLayout());
// Create table model
String[] columns = {"ID", "Title", "Author", "Publication Date"};
model = new DefaultTableModel(columns, 0);
// Create table and set model
table = new JTable(model);
add(new JScrollPane(table), BorderLayout.CENTER);
// Create buttons for adding, editing, and deleting books
JButton addButton = new JButton("Add Book");
addButton.addActionListener(this);
JButton editButton = new JButton("Edit Book");
editButton.addActionListener(this);
JButton deleteButton = new JButton("Delete Book");
deleteButton.addActionListener(this);
// Add buttons to panel
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(addButton);
buttonPanel.add(editButton);
buttonPanel.add(deleteButton);
// Add panel to frame
add(buttonPanel, BorderLayout.SOUTH);
// Initialize book list
books = new ArrayList<>();
setVisible(true);
}
public static void main(String[] args) {
new AdminGUI();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Add Book")) {
// Create dialog for entering book information
JTextField idField = new JTextField();
JTextField titleField = new JTextField();
JTextField authorField = new JTextField();
JTextField dateField = new JTextField();
Object[] message = {
"ID:", idField,
"Title:", titleField,
"Author:", authorField,
"Publication Date:", dateField
};
int option = JOptionPane.showConfirmDialog(this, message, "Add Book", JOptionPane.OK_CANCEL_OPTION);
// Add book to table if OK is clicked
if (option == JOptionPane.OK_OPTION) {
int id = Integer.parseInt(idField.getText());
String title = titleField.getText();
String author = authorField.getText();
String date = dateField.getText();
Book book = new Book(id, title, author, date);
books.add(book);
String[] row = {Integer.toString(id), title, author, date};
model.addRow(row);
}
} else if (e.getActionCommand().equals("Edit Book")) {
// Edit selected book in table
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) {
int id = Integer.parseInt(model.getValueAt(selectedRow, 0).toString());
String title = model.getValueAt(selectedRow, 1).toString();
String author = model.getValueAt(selectedRow, 2).toString();
String date = model.getValueAt(selectedRow, 3).toString();
JTextField idField = new JTextField(Integer.toString(id));
JTextField titleField = new JTextField(title);
JTextField authorField = new JTextField(author);
JTextField dateField = new JTextField(date);
Object[] message = {
"ID:", idField,
"Title:", titleField,
"Author:", authorField,
"Publication Date:", dateField
};
int option = JOptionPane.showConfirmDialog(this, message, "Edit Book", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
int newId = Integer.parseInt(idField.getText());
String newTitle = titleField.getText();
String newAuthor = authorField.getText();
String newDate = dateField.getText();
Book newBook = new Book(newId, newTitle, newAuthor, newDate);
books.set(selectedRow, newBook);
model.setValueAt(newId, selectedRow, 0);
model.setValueAt(newTitle, selectedRow, 1);
model.setValueAt(newAuthor, selectedRow, 2);
model.setValueAt(newDate, selectedRow, 3);
}
}
} else if (e.getActionCommand().equals("Delete Book")) {
// Delete selected book from table
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) {
books.remove(selectedRow);
model.removeRow(selectedRow);
}
}
}
private class Book {
private int id;
private String title;
private String author;
private String date;
public Book(int id, String title, String author, String date) {
this.id = id;
this.title = title;
this.author = author;
this.date = date;
}
}
}
```
这段代码创建了一个管理员界面,允许管理员添加、编辑和删除书籍。当管理员点击“Add Book”按钮时,程序会弹出一个对话框,让管理员输入书籍信息,并将这些信息添加到表格中。当管理员选择一行并点击“Edit Book”按钮时,程序会弹出一个对话框,允许管理员编辑该书籍的信息。当管理员选择一行并点击“Delete Book”按钮时,程序会从表格中删除该书籍的信息。你可以根据自己的需求来修改这段代码。
阅读全文