逐句分析public class QueryJPanel extends JPanel implements ActionListener { Object[] columnName = {"学号","姓名","年龄", "专业"}; Object[][] data; private JButton chaxun=new JButton("查询"); private JTable table=new JTable(); public QueryJPanel() { this.add(chaxun); JScrollPane sp=new JScrollPane(table); this.add(sp); chaxun.addActionListener(this); }
时间: 2024-03-15 17:42:54 浏览: 18
这是一段Java代码,根据语法可以分析出以下内容:
- 定义了一个名为QueryJPanel的类,继承自JPanel类,并实现了ActionListener接口。
- 定义了一个名为columnName的Object数组,数组中包含四个字符串元素:"学号"、"姓名"、"年龄"、"专业"。
- 定义了一个名为data的Object二维数组,用于存储查询结果数据。
- 定义了一个私有的JButton类型的变量chaxun,用于触发查询操作。
- 定义了一个私有的JTable类型的变量table,用于展示查询结果。
- 定义了一个构造函数,用于初始化界面和组件。
- 在构造函数中,将chaxun按钮和table表格添加到界面中,并为chaxun按钮添加了一个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(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(); } } } } 将以上代码获取的每条数据后面添加删除和修改的按钮
您可以在 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 中,您可以实现相应的删除和修改操作。
import java.awt.event.*; import java.awt.*; import javax.swing.*; import javax.swing.table.DefaultTableModel; public class MyTableWin extends JFrame implements ActionListener { Object rows[][] = { { "001", "zhang", "03/04/2002", "Computer" }, { "002", "Li", "05/10/2002", "Chinese" }, { "003", "Wang", "09/10/2002", "English" } }; String cols[] = { "number", "name", "birthday", "department" }; JTable table; JLabel lbl_show; JTextField txt_show; JButton btn_update; JPanel pSouth, pNorth; JScrollPane jp; public MyTableWin() { table = new JTable(rows, cols); // 表格里点谁就显示谁 table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { int row = table.getSelectedRow(); int column = table.getSelectedColumn(); Object selectedvalue = table.getModel().getValueAt(row, column); txt_show.setText((String) selectedvalue); btn_update.setEnabled(true); } }); lbl_show = new JLabel("Info."); txt_show = new JTextField(15); btn_update = new JButton("Update"); btn_update.setEnabled(false); btn_update.addActionListener(this); pSouth = new JPanel(); pNorth = new JPanel(); jp = new JScrollPane(table); pNorth.add(jp); pSouth.add(lbl_show); pSouth.add(txt_show); pSouth.add(btn_update); add(pNorth, BorderLayout.CENTER); add(pSouth, BorderLayout.SOUTH); setVisible(true); setSize(800, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { int row = table.getSelectedRow(); int column = table.getSelectedColumn(); rows[row][column] = txt_show.getText(); int option = JOptionPane.showConfirmDialog(this, "do you confirm to update the table content?", "warning", JOptionPane.OK_CANCEL_OPTION); if (option == JOptionPane.OK_OPTION) { table.setModel(new DefaultTableModel(rows, cols)); } btn_update.setEnabled(false); } public static void main(String[] args) { MyTableWin win = new MyTableWin(); } }修改该程序,使得点击表格时,在控件台显示所在行一行的数据。
可以在鼠标点击事件中获取所在行的数据,然后显示在控件台上。修改代码如下:
```
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int row = table.getSelectedRow();
Object[] rowData = new Object[cols.length];
for (int i = 0; i < cols.length; i++) {
rowData[i] = table.getModel().getValueAt(row, i);
}
String rowInfo = "Row " + (row + 1) + ": " + Arrays.toString(rowData);
txt_show.setText(rowInfo);
btn_update.setEnabled(true);
}
});
```
这样修改后,点击表格时就可以在控件台中显示所在行一行的数据了。