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:43:47 浏览: 139
详谈PHP中public,private,protected,abstract等关键字的用法
好的,我可以帮您添加删除和修改的功能按钮。首先,在 `InterfaceCallExample()` 方法中,我们需要创建删除和修改按钮,并将它们添加到 `JPanel` 中。代码如下:
```
JButton deleteButton = new JButton("删除数据");
deleteButton.addActionListener(this);
panel.add(deleteButton);
JButton updateButton = new JButton("修改数据");
updateButton.addActionListener(this);
panel.add(updateButton);
```
接下来,在 `actionPerformed()` 方法中,我们可以检查事件源是否为删除或修改按钮,并且获取当前选中的行。代码如下:
```
int selectedRow = resultTable.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this, "请先选中要操作的行!");
return;
}
if (e.getSource() == deleteButton) {
tableModel.removeRow(selectedRow);
} else if (e.getSource() == updateButton) {
// 获取当前行的数据,并在新的窗口中显示数据的详细信息
String id = tableModel.getValueAt(selectedRow, 0).toString();
String titleName = tableModel.getValueAt(selectedRow, 1).toString();
String status = tableModel.getValueAt(selectedRow, 2).toString();
String createTime = tableModel.getValueAt(selectedRow, 3).toString();
String updateTime = tableModel.getValueAt(selectedRow, 4).toString();
JOptionPane.showMessageDialog(this, "详细信息:\n" + "id:" + id + "\n材料类型:" + titleName + "\n状态:" + status + "\n创建时间:" + createTime + "\n更改时间:" + updateTime);
}
```
完整代码如下所示:
```
public class InterfaceCallExample extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton callButton;
private JButton deleteButton;
private JButton updateButton;
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);
updateButton = new JButton("修改数据");
updateButton.addActionListener(this);
panel.add(updateButton);
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();
}
} else if (e.getSource() == deleteButton || e.getSource() == updateButton) {
int selectedRow = resultTable.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this, "请先选中要操作的行!");
return;
}
if (e.getSource() == deleteButton) {
tableModel.removeRow(selectedRow);
} else if (e.getSource() == updateButton) {
// 获取当前行的数据,并在新的窗口中显示数据的详细信息
String id = tableModel.getValueAt(selectedRow, 0).toString();
String titleName = tableModel.getValueAt(selectedRow, 1).toString();
String status = tableModel.getValueAt(selectedRow, 2).toString();
String createTime = tableModel.getValueAt(selectedRow, 3).toString();
String updateTime = tableModel.getValueAt(selectedRow, 4).toString();
JOptionPane.showMessageDialog(this, "详细信息:\n" + "id:" + id + "\n材料类型:" + titleName + "\n状态:" + status + "\n创建时间:" + createTime + "\n更改时间:" + updateTime);
}
}
}
}
```
阅读全文