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 17:38:22 浏览: 39
详谈PHP中public,private,protected,abstract等关键字的用法
好的,您想要在界面中添加一个新增数据的功能按钮,点击按钮后可以输入对应的数据。我可以为您提供一些参考代码:
```
private JButton addButton;
public InterfaceCallExample() {
//...
addButton = new JButton("新增数据");
addButton.addActionListener(this);
panel.add(addButton);
//...
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == callButton) {
//...
} else if (e.getSource() == addButton) {
String id = JOptionPane.showInputDialog(this, "请输入id:");
String titleName = JOptionPane.showInputDialog(this, "请输入材料类型:");
String status = JOptionPane.showInputDialog(this, "请输入状态:");
String createTime = JOptionPane.showInputDialog(this, "请输入创建时间:");
String updateTime = JOptionPane.showInputDialog(this, "请输入更改时间:");
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);
}
}
```
在代码中,我们添加了一个 `addButton` 按钮,并在 `actionPerformed` 方法中处理了按钮的点击事件。当用户点击 `addButton` 按钮时,会弹出对话框让用户输入对应的数据,并将输入的数据添加到表格中。
阅读全文