java弹窗表格把文件的书存入表格实现管理员对书籍的添加和删除的代码,不用数据库

时间: 2024-03-16 18:46:29 浏览: 15
以下是一个使用Java Swing实现弹窗表格的示例代码,可以实现管理员对书籍的添加和删除,数据存储在内存中的数据结构中,而不是使用数据库。 ```java import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class BookManagerDialog extends JDialog { private static final long serialVersionUID = 1L; private List<Book> bookList = new ArrayList<>(); private JTable table; private DefaultTableModel model; public BookManagerDialog(JFrame parent) { super(parent, true); setTitle("图书管理"); setSize(400, 300); setLocationRelativeTo(null); // 表格模型 model = new DefaultTableModel(); model.addColumn("书名"); model.addColumn("作者"); model.addColumn("出版社"); table = new JTable(model); // 添加滚动条 JScrollPane scrollPane = new JScrollPane(table); // 添加按钮 JButton addButton = new JButton("添加"); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { addBook(); } }); JButton deleteButton = new JButton("删除"); deleteButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { deleteBook(); } }); JPanel buttonPanel = new JPanel(); buttonPanel.add(addButton); buttonPanel.add(deleteButton); // 添加到对话框中 getContentPane().add(scrollPane, BorderLayout.CENTER); getContentPane().add(buttonPanel, BorderLayout.SOUTH); } public void addBook() { BookFormDialog formDialog = new BookFormDialog(this); formDialog.setVisible(true); if (formDialog.isSubmit()) { Book book = new Book(formDialog.getBookName(), formDialog.getAuthorName(), formDialog.getPublisherName()); bookList.add(book); model.addRow(new Object[] {book.getName(), book.getAuthor(), book.getPublisher()}); } } public void deleteBook() { int selectedRow = table.getSelectedRow(); if (selectedRow >= 0) { bookList.remove(selectedRow); model.removeRow(selectedRow); } } public static void main(String[] args) { JFrame frame = new JFrame("测试"); frame.setSize(800, 600); frame.setLocationRelativeTo(null); BookManagerDialog dialog = new BookManagerDialog(frame); dialog.setVisible(true); } } class BookFormDialog extends JDialog { private static final long serialVersionUID = 1L; private boolean submit; private String bookName; private String authorName; private String publisherName; public BookFormDialog(JDialog parent) { super(parent, true); setTitle("添加图书"); setSize(400, 300); setLocationRelativeTo(null); JPanel panel = new JPanel(); getContentPane().add(panel, BorderLayout.CENTER); panel.setLayout(null); // 添加表单元素 JLabel bookNameLabel = new JLabel("书名:"); bookNameLabel.setBounds(10, 20, 60, 25); panel.add(bookNameLabel); JTextField bookNameField = new JTextField(); bookNameField.setBounds(80, 20, 200, 25); panel.add(bookNameField); JLabel authorNameLabel = new JLabel("作者:"); authorNameLabel.setBounds(10, 50, 60, 25); panel.add(authorNameLabel); JTextField authorNameField = new JTextField(); authorNameField.setBounds(80, 50, 200, 25); panel.add(authorNameField); JLabel publisherNameLabel = new JLabel("出版社:"); publisherNameLabel.setBounds(10, 80, 60, 25); panel.add(publisherNameLabel); JTextField publisherNameField = new JTextField(); publisherNameField.setBounds(80, 80, 200, 25); panel.add(publisherNameField); // 添加按钮 JButton okButton = new JButton("确定"); okButton.setBounds(50, 150, 80, 25); okButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { bookName = bookNameField.getText(); authorName = authorNameField.getText(); publisherName = publisherNameField.getText(); submit = true; setVisible(false); } }); panel.add(okButton); JButton cancelButton = new JButton("取消"); cancelButton.setBounds(150, 150, 80, 25); cancelButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { submit = false; setVisible(false); } }); panel.add(cancelButton); } public boolean isSubmit() { return submit; } public String getBookName() { return bookName; } public String getAuthorName() { return authorName; } public String getPublisherName() { return publisherName; } } class Book { private String name; private String author; private String publisher; public Book(String name, String author, String publisher) { this.name = name; this.author = author; this.publisher = publisher; } public String getName() { return name; } public String getAuthor() { return author; } public String getPublisher() { return publisher; } } ``` 说明: 1. BookManagerDialog类是弹窗表格的主界面,使用JTable实现表格,使用DefaultTableModel作为表格模型,将数据存储在bookList中,通过添加和删除按钮修改表格数据。 2. BookFormDialog类是添加图书的表单弹窗,使用JTextField作为表单元素,通过getXXX()方法获取用户输入的数据,通过isSubmit()方法判断用户是否点击了“确定”按钮。 3. Book类是一个简单的数据模型类,用于存储图书信息。

相关推荐

最新推荐

recommend-type

用Jena将本体文件存入MySQL数据库的实现方法

以下的文章主要介绍的是使用Jena,将本体文件存入MySQL数据库的实际操作步骤
recommend-type

Java+mysql本地图片上传数据库及下载示例

本篇文章主要介绍了Java+mysql本地图片上传数据库及下载示例,具有一定的参加价值,有兴趣的可以了解一下。
recommend-type

java+vue实现添加单选题、多选题到题库功能

主要为大家详细介绍了java+vue实现添加单选题、多选题到题库功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Java实现TCP/IP协议的收发数据(服务端)代码实例

主要介绍了Java实现TCP/IP协议的收发数据(服务端)代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

Python实现读取TXT文件数据并存进内置数据库SQLite3的方法

主要介绍了Python实现读取TXT文件数据并存进内置数据库SQLite3的方法,涉及Python针对txt文件的读取及sqlite3数据库的创建、插入、查询等相关操作技巧,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。