gpt4 book ai didi

java - 由于某些原因它没有添加到 arrayList

转载 作者:行者123 更新时间:2023-12-01 06:38:34 24 4
gpt4 key购买 nike

我遇到了这个奇怪的事情,我试图添加到 ArrayList 但它没有添加,它获取了值和所有内容。请检查代码并告诉我。

我要添加的类(class):

public class ManualProductGUI extends JDialog {

private final JPanel contentPanel = new JPanel();
private JTextField barcodeField;
private JTextField idField;
private JTextField nameField;
private JTextField priceField;
private JTextField quantityField;
private JTextField infoField;

BasketContainer bc = new BasketContainer();

private static final long serialVersionUID = 1L;

/**
* Launch the application.
*/
public static void main(String[] args) {
try {
ManualProductGUI dialog = new ManualProductGUI();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Create the dialog.
*/
public ManualProductGUI() {
setTitle("Product search");
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);


barcodeField = new JTextField();
barcodeField.setText("Enter barcode");
barcodeField.setBounds(10, 11, 157, 20);
contentPanel.add(barcodeField);
barcodeField.setColumns(10);
barcodeField.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
barcodeField.setText("");
}
});

barcodeField.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int barcode = Integer.parseInt(barcodeField.getText());
ProductCtr prodCtr = new ProductCtr();

Product prod = prodCtr.searchProductByBarcode(barcode);
buildFields(prod);
}
});

infoField = new JTextField();
infoField.setEditable(false);
infoField.setBounds(177, 133, 86, 20);
contentPanel.add(infoField);
infoField.setColumns(10);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("Add");
okButton.setActionCommand("Add");
buttonPane.add(okButton);
okButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
addToBasket();
setVisible(false);
dispose();
}
});
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
dispose();
}
});
buttonPane.add(cancelButton);
}
}
}

private void addToBasket()
{
int barcode = Integer.parseInt(barcodeField.getText());
ProductCtr prodCtr = new ProductCtr();

Product prod = prodCtr.searchProductByBarcode(barcode);

bc.addProduct(prod);
}

}

这是 Container 类的一部分:

private ArrayList<Product> listOfItems;
public BasketContainer()
{
listOfItems = new ArrayList<>();
}

public void addProduct(Product prod)
{
listOfItems.add(prod);
}
public ArrayList<Product> getProducts()
{
return listOfItems;
}

它打印出屏幕上的信息,但不添加。我错过了什么吗?

谢谢。

最佳答案

每次都会创建一个新的BasketContainer实例,这样新实例就会有一个新的清晰的ArrayList。

要修复此问题,您应该在某处(可能在 init block 中?)创建 BasketContainer 实例并保存引用,然后使用它来添加元素。

关于java - 由于某些原因它没有添加到 arrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23698711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com