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 18:41:31 浏览: 144
详谈PHP中public,private,protected,abstract等关键字的用法
您可以在 JTable 中添加一个新的列,用来显示删除和修改的按钮。在 actionPerformed() 方法中,您可以为每一行添加对应的按钮,并添加相应的 ActionListener。以下是您可以参考的修改代码:
```
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(800, 600);
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("更改时间");
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();
JButton deleteButton = new JButton("删除");
deleteButton.addActionListener(new DeleteActionListener(id));
JButton updateButton = new JButton("修改");
updateButton.addActionListener(new UpdateActionListener(id, titleName, status, createTime, updateTime));
ArrayList<String> strings = new ArrayList<>();
strings.add(id);
strings.add(titleName);
strings.add(status);
strings.add(createTime);
strings.add(updateTime);
strings.add("");
strings.add("");
String[] data = strings.toString().replace("[","").replace("]","").split(",");
tableModel.addRow(data);
int row = tableModel.getRowCount() - 1;
resultTable.setValueAt(deleteButton, row, 5);
resultTable.setValueAt(updateButton, row, 6);
}
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
class DeleteActionListener implements ActionListener {
private String id;
public DeleteActionListener(String id) {
this.id = id;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO: 实现删除操作
}
}
class UpdateActionListener implements ActionListener {
private String id;
private String titleName;
private String status;
private String createTime;
private String updateTime;
public UpdateActionListener(String id, String titleName, String status, String createTime, String updateTime) {
this.id = id;
this.titleName = titleName;
this.status = status;
this.createTime = createTime;
this.updateTime = updateTime;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO: 实现修改操作
}
}
}
```
在 DeleteActionListener 和 UpdateActionListener 中,您可以实现相应的删除和修改操作。
阅读全文