具体修改代码应该是什么
时间: 2023-10-01 19:03:22 浏览: 46
你可以将原始代码更新如下:
```
public UpdateProductDialog(Frame owner, int productId) {
super(owner, "更新商品", true);
setPreferredSize(new Dimension(400, 300));
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
ProductDAO productDAO = new ProductDAOImpl();
product = productDAO.getProductById(productId);
if (product == null) {
JOptionPane.showMessageDialog(this, "该商品不存在!");
dispose();
return;
}
JLabel nameLabel = new JLabel("名称");
JLabel priceLabel = new JLabel("价格");
JLabel descriptionLabel = new JLabel("描述");
nameField = new JTextField(product.getName());
priceField = new JTextField(Double.toString(product.getPrice()));
descriptionArea = new JTextArea(product.getDescription(), 5, 20);
JButton okButton = new JButton("确定");
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
double price = Double.parseDouble(priceField.getText());
String description = descriptionArea.getText();
product.setName(name);
product.setPrice(price);
product.setDescription(description);
isOK = true;
dispose();
}
});
JButton cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
JPanel panel = new JPanel(new GridLayout(4, 2));
panel.add(nameLabel);
panel.add(nameField);
panel.add(priceLabel);
panel.add(priceField);
panel.add(descriptionLabel);
panel.add(new JScrollPane(descriptionArea));
panel.add(okButton);
panel.add(cancelButton);
setContentPane(panel);
pack();
setLocationRelativeTo(owner);
}
```
在上述代码中,你首先实例化`ProductDAOImpl`,然后通过`getProductById`方法获取商品对象,并对`product`进行了非空检查。如果`product`为空,你将会弹出一个对话框提示用户该商品不存在,并关闭对话框。如果`product`非空,你将继续执行接下来的代码。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)