addButton = new JButton("增加"); deleteButton = new JButton("删除"); updateButton = new JButton("修改"); queryButton = new JButton("查询");
时间: 2024-04-02 08:32:07 浏览: 40
这是一段Java代码,其中定义了四个JButton对象:addButton、deleteButton、updateButton和queryButton,它们分别对应着停车场信息管理系统中的增加、删除、修改和查询操作。在代码中,通过调用JButton类的构造方法,并传递相应的字符串参数,创建了这四个按钮对象。这段代码的作用是创建四个按钮对象,并对它们进行初始化。
相关问题
优化这段代码package com.daiinfo.catering.pane; import com.daiinfo.catering.util.JDBConnection; import javax.swing.*; import javax.swing.border.Border; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.EventListener; public class DishManagePane2 extends JFrame { private JTable table; private JButton addButton,updateButton,deleteButton; public DishManagePane2(){ setTitle("菜品管理"); setSize(800,600); String [] columnNames = {"菜品名","价格","菜品类别"}; Object[][] data = {{"糖醋排骨",29.0,"西餐"},{"宫保鸡丁",28.0,"中餐"}}; JTable table = new JTable(data,columnNames); addButton = new JButton("增加"); updateButton = new JButton("修改"); deleteButton = new JButton("删除"); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { } }); JPanel panel = new JPanel(new BorderLayout()); panel.add(new JScrollPane(table),BorderLayout.CENTER); JPanel buttonPanel = new JPanel(new FlowLayout()); buttonPanel.add(addButton); buttonPanel.add(updateButton); buttonPanel.add(deleteButton); panel.add(buttonPanel,BorderLayout.SOUTH); add(panel); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new JDBConnection(); new DishManagePane2(); } }
以下是对代码的优化:
1. 将 JTable、JButton 声明为类成员变量,方便后面的方法访问。
2. 将 JTable 中的数据从构造函数中移除,改为从数据库中读取,使用 JDBConnection 类进行数据库连接和查询。
3. 将按钮的 ActionListener 单独提取出来,作为类内部的一个私有方法。
4. 将 JFrame 窗口的设置移到单独的方法中,方便对窗口属性的修改和扩展。
优化后的代码如下:
```java
package com.daiinfo.catering.pane;
import com.daiinfo.catering.util.JDBConnection;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
public class DishManagePane2 extends JFrame {
private JTable table;
private JButton addButton, updateButton, deleteButton;
public DishManagePane2() {
setTitle("菜品管理");
setSize(800, 600);
setWindow();
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JScrollPane(table), BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(addButton);
buttonPanel.add(updateButton);
buttonPanel.add(deleteButton);
panel.add(buttonPanel, BorderLayout.SOUTH);
add(panel);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setWindow() {
addButton = new JButton("增加");
updateButton = new JButton("修改");
deleteButton = new JButton("删除");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 添加菜品的逻辑
}
});
// 从数据库中读取数据并绑定到 JTable 上
try {
Connection conn = JDBConnection.getConnection();
Statement stmt = conn.createStatement();
String sql = "select * from dishes";
ResultSet rs = stmt.executeQuery(sql);
Vector<Vector<Object>> data = new Vector<>();
while (rs.next()) {
Vector<Object> row = new Vector<>();
row.add(rs.getString("name"));
row.add(rs.getDouble("price"));
row.add(rs.getString("category"));
data.add(row);
}
Vector<String> columnNames = new Vector<>();
columnNames.add("菜品名");
columnNames.add("价格");
columnNames.add("菜品类别");
table = new JTable(data, columnNames);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new JDBConnection();
new DishManagePane2();
}
}
```
这样,代码就更加清晰、易于维护和扩展了。
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 中,您可以实现相应的删除和修改操作。
阅读全文