public class InterfaceCallExample extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JButton callButton; private JTable resultTable; private DefaultTableModel tableModel; public InterfaceCallExample() { super("Interface Call Example"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); callButton = new JButton("查看数据"); callButton.addActionListener(this); panel.add(callButton); add(panel, BorderLayout.NORTH); tableModel = new DefaultTableModel(); tableModel.addColumn("id"); tableModel.addColumn("材料类型"); tableModel.addColumn("状态"); tableModel.addColumn("创建时间"); tableModel.addColumn("更改时间"); resultTable = new JTable(tableModel); add(new JScrollPane(resultTable), BorderLayout.CENTER); } public static void main(String[] args) { InterfaceCallExample example = new InterfaceCallExample(); example.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == callButton) { try { URL url = new URL("http://101.34.120.182:8081/wnapp/productType/getTypeId?category=1&status=0&type="); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String s = reader.readLine(); JSONObject jsonObject = new JSONObject(); JSONObject object = JSONObject.parseObject(s); Object o = object.get("data"); JSONObject object1 = JSONObject.parseObject(o.toString()); ArrayList<Map<String,Object>> list = (ArrayList<Map<String,Object>>) object1.get("list"); for (Map<String,Object> map : list){ String id = map.get("id").toString(); String titleName = map.get("titleName").toString(); String status = map.get("status").toString(); String createTime = map.get("createTime").toString(); String updateTime = map.get("updateTime").toString(); ArrayList<String> strings = new ArrayList<>(); strings.add(id); strings.add(titleName); strings.add(status); strings.add(createTime); strings.add(updateTime); String[] data = strings.toString().replace("[","").replace("]","").split(","); tableModel.addRow(data); } reader.close(); } catch (Exception ex) { ex.printStackTrace(); } } } } 将以上代码获取的每条数据后面添加删除和修改的功能按钮

时间: 2024-03-20 15:43:41 浏览: 18
要添加删除和修改的功能按钮,你需要做以下几步: 1. 在 InterfaceCallExample 类中定义两个 JButton 对象,即 deleteButton 和 editButton。 2. 在构造函数中,创建一个新的 JPanel 对象,将 deleteButton 和 editButton 添加到该面板上,并将面板添加到 JFrame 上。 3. 在 actionPerformed 方法中,为 deleteButton 和 editButton 添加 ActionListener,以响应用户的点击事件。 4. 在 actionPerformed 方法中,根据用户点击的按钮执行相应的操作。例如,如果用户点击了 deleteButton,你需要删除选中的行,如果用户点击了 editButton,你需要打开一个新的窗口,以允许用户编辑所选行的数据。 下面是一个可能的实现示例: ``` public class InterfaceCallExample extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JButton callButton; private JButton deleteButton; private JButton editButton; private JTable resultTable; private DefaultTableModel tableModel; public InterfaceCallExample() { super("Interface Call Example"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); callButton = new JButton("查看数据"); callButton.addActionListener(this); panel.add(callButton); deleteButton = new JButton("删除"); deleteButton.addActionListener(this); panel.add(deleteButton); editButton = new JButton("修改"); editButton.addActionListener(this); panel.add(editButton); add(panel, BorderLayout.NORTH); tableModel = new DefaultTableModel(); tableModel.addColumn("id"); tableModel.addColumn("材料类型"); tableModel.addColumn("状态"); tableModel.addColumn("创建时间"); tableModel.addColumn("更改时间"); resultTable = new JTable(tableModel); add(new JScrollPane(resultTable), BorderLayout.CENTER); } public static void main(String[] args) { InterfaceCallExample example = new InterfaceCallExample(); example.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == callButton) { // 调用接口获取数据 // ...省略代码... } else if (e.getSource() == deleteButton) { // 删除选中的行 int[] selectedRows = resultTable.getSelectedRows(); for (int i = selectedRows.length - 1; i >= 0; i--) { tableModel.removeRow(selectedRows[i]); } } else if (e.getSource() == editButton) { // 打开编辑窗口,允许用户编辑所选行的数据 int selectedRow = resultTable.getSelectedRow(); if (selectedRow >= 0) { String id = (String) tableModel.getValueAt(selectedRow, 0); String titleName = (String) tableModel.getValueAt(selectedRow, 1); String status = (String) tableModel.getValueAt(selectedRow, 2); String createTime = (String) tableModel.getValueAt(selectedRow, 3); String updateTime = (String) tableModel.getValueAt(selectedRow, 4); // ...省略打开编辑窗口的代码... } } } } ```

相关推荐

最新推荐

recommend-type

基于Yolov5的旋转检测

旋转检测 要求 torch==1.6 shapely==1.7.1 opencv==4.2.0.34
recommend-type

MATLAB 代码解决 Timothy Sauer 的教科书“数值分析”第三版中的两组计算机问题.zip

1.版本:matlab2014/2019a/2021a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
recommend-type

基于SpringBoot+SpringCloud微服务的商城项目.zip

基于springboot的java毕业&课程设计
recommend-type

智慧藏文化博物馆建设方案PPT(79页).pptx

智慧藏文化博物馆建设方案PPT(79页)
recommend-type

基于SpringBoot+SpringSecurity等的第三方登录(微信QQ)和安全认证框架.zip

基于springboot的java毕业&课程设计
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

SQL怎么实现 数据透视表

SQL可以通过使用聚合函数和GROUP BY子句来实现数据透视表。 例如,假设有一个销售记录表,其中包含产品名称、销售日期、销售数量和销售额等信息。要创建一个按照产品名称、销售日期和销售额进行汇总的数据透视表,可以使用以下SQL语句: ``` SELECT ProductName, SaleDate, SUM(SaleQuantity) AS TotalQuantity, SUM(SaleAmount) AS TotalAmount FROM Sales GROUP BY ProductName, SaleDate; ``` 该语句将Sales表按照ProductName和SaleDat
recommend-type

JSBSim Reference Manual

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