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-23 12:38:20 浏览: 20
好的,您可以在构造函数中添加一个新的按钮,并为其添加一个监听器,如下所示:
```
public class InterfaceCallExample extends JFrame implements ActionListener {
// 省略原有的代码
private JButton callButton;
private JButton addButton; // 新增按钮
// 省略原有的代码
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);
addButton = new JButton("新增数据"); // 新增按钮
addButton.addActionListener(this); // 新增按钮监听器
panel.add(addButton); // 新增按钮
add(panel, BorderLayout.NORTH);
// 省略原有的代码
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == callButton) {
// 省略原有的代码
} else if (e.getSource() == addButton) { // 处理新增按钮点击事件
String id = "xxx"; // 新增数据的id
String titleName = "xxx"; // 新增数据的材料类型
String status = "xxx"; // 新增数据的状态
String createTime = "xxx"; // 新增数据的创建时间
String updateTime = "xxx"; // 新增数据的更改时间
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); // 添加新增数据到表格中
}
}
}
```
这样就实现了一个新增数据的功能按钮。当用户点击该按钮时,程序会添加一条新的数据到表格中。