用Java写一个弹窗式图书管理系统
时间: 2023-10-03 15:09:20 浏览: 82
好的,下面是一个简单的弹窗式图书管理系统的Java代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class BookManagementSystem extends JFrame implements ActionListener {
private JTextField bookNameField, authorField, priceField;
private JButton addButton, deleteButton, searchButton, displayButton;
private JTextArea resultArea;
private ArrayList<Book> bookList;
public BookManagementSystem() {
super("图书管理系统");
bookList = new ArrayList<Book>();
// UI界面初始化
JPanel controlPanel = new JPanel(new GridLayout(5, 2, 5, 5));
controlPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
controlPanel.add(new JLabel("书名:"));
bookNameField = new JTextField(20);
controlPanel.add(bookNameField);
controlPanel.add(new JLabel("作者:"));
authorField = new JTextField(20);
controlPanel.add(authorField);
controlPanel.add(new JLabel("价格:"));
priceField = new JTextField(20);
controlPanel.add(priceField);
addButton = new JButton("添加");
addButton.addActionListener(this);
controlPanel.add(addButton);
deleteButton = new JButton("删除");
deleteButton.addActionListener(this);
controlPanel.add(deleteButton);
searchButton = new JButton("查询");
searchButton.addActionListener(this);
controlPanel.add(searchButton);
displayButton = new JButton("显示");
displayButton.addActionListener(this);
controlPanel.add(displayButton);
resultArea = new JTextArea(20, 60);
resultArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(resultArea);
Container container = getContentPane();
container.add(controlPanel, BorderLayout.NORTH);
container.add(scrollPane, BorderLayout.CENTER);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String bookName = bookNameField.getText();
String author = authorField.getText();
double price = Double.parseDouble(priceField.getText());
Book book = new Book(bookName, author, price);
bookList.add(book);
resultArea.setText("添加成功!");
} else if (e.getSource() == deleteButton) {
String bookName = bookNameField.getText();
for (Book book : bookList) {
if (book.getBookName().equals(bookName)) {
bookList.remove(book);
resultArea.setText("删除成功!");
return;
}
}
resultArea.setText("未找到该书!");
} else if (e.getSource() == searchButton) {
String bookName = bookNameField.getText();
for (Book book : bookList) {
if (book.getBookName().equals(bookName)) {
resultArea.setText(book.toString());
return;
}
}
resultArea.setText("未找到该书!");
} else if (e.getSource() == displayButton) {
StringBuilder sb = new StringBuilder();
for (Book book : bookList) {
sb.append(book.toString()).append("\n");
}
resultArea.setText(sb.toString());
}
}
public static void main(String[] args) {
new BookManagementSystem();
}
}
class Book {
private String bookName;
private String author;
private double price;
public Book(String bookName, String author, double price) {
this.bookName = bookName;
this.author = author;
this.price = price;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return "书名:" + bookName + ",作者:" + author + ",价格:" + price + "元。";
}
}
```
这个图书管理系统可以实现添加、删除、查询和显示图书列表的功能。您可以根据您的具体需求进行修改和完善。
阅读全文