优化这段代码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(); } }
时间: 2024-03-26 07:34:36 浏览: 141
以下是对代码的优化:
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();
}
}
```
这样,代码就更加清晰、易于维护和扩展了。
阅读全文