【发布时间】:2026-02-10 21:30:01
【问题描述】:
我正在尝试为我的程序编写 GUI。我有一个 Product 类,我在其中将产品的价格和名称存储在 arraylist 中。我还有一个订单数组列表,其中包含给每个服务员的订单。我将所有产品放在一个 JComboBox 中,并为每个产品添加了一个动作侦听器,以通过更新 JLable 的文本在单击时显示每个产品的价格。然后有一个JSpinner来获取所选产品的数量。最后还有一个“添加”按钮,我想用它来更新 Jtable 中的产品名称、数量和总价,同时将该产品添加到订单数组列表中。我不知道如何填充 JTable,也无法从其他答案中了解多少,因为他们使用的是 netbeans。我想只使用一个简单的 JLabe,但在选择并添加每个产品后,我不明白如何更新文本并在标签中添加新行。你能解释一下我怎么能做到这一点吗?我的部分代码看起来像这样
box1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Product prod = (Product) box1.getSelectedItem();
price.setText(String.valueOf(prod.getSellingPrice()));
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int numbs = (Integer) spinner.getValue();
for (int i = 0; i <= numbs; i++) {
order.addProduct(prod);
}
JLabel label = new JLabel();
lists.add(label);
label.setText(prod.getName() + " " + numbs + " " + numbs * prod.getSellingPrice());
}
});
}
});
【问题讨论】: