我正在创建一个库存程序,该程序将允许用户添加,修改,出售或删除产品。我创建了一个Product类,一个用于GUI的类,一个用于ArrayList的类。我在将产品添加到JList和ArrayList时遇到问题,我希望一旦用户按下按钮(addButton1),产品就会添加到JList和ArrayList中。谁能帮我吗?谢谢,谢谢。
这是我的产品类别
//attributes
{
private String name;
private int stockLevel;
private double price;
//methods
public Product(String nameIn, int stockLevelIn, double priceIn)
{
name = nameIn;
stockLevel = stockLevelIn;
price = priceIn;
}
//all the methods here}
这是GUI类
import javax.swing.*;
import java.awt.*;
public class ProductGUI extends JFrame implements ActionListener
{
private JFrame AddJFrame = new JFrame();
private JTextField productNameText = new JTextField(15);
private JTextField productPriceText = new JTextField(15);
private JTextField productStockLevelText = new JTextField(15);
private JButton addButton1 = new JButton("Add");
private JButton cancelButton = new JButton("Cancel");
//menu
private JMenuBar bar = new JMenuBar();
private JMenu productMenu = new JMenu("Product");
private JMenu infoMenu = new JMenu("Info");
private JMenuItem addProductItem = new JMenuItem("Add");
private JMenuItem modifyProductItem = new JMenuItem("Modify");
private JMenuItem removeProductItem = new JMenuItem("Remove");
private JMenuItem sellProductItem = new JMenuItem("Sell");
private JMenuItem aboutApp = new JMenuItem("About App");
//Buttons
private JButton sellButton = new JButton("Sell");
private JButton addButton = new JButton("Add");
private JButton modifyButton = new JButton("Modify");
private JButton removeButton = new JButton("Remove");
//Labels
private JLabel productName = new JLabel("Product Name: ");
private JLabel productPrice = new JLabel("Price: ");
private JLabel productStockLevel = new JLabel("Stock level: ");
//panels
private JPanel topPanel = new JPanel();
private JPanel bottomPanel = new JPanel();
private JPanel middlePanel = new JPanel();
//List
private JScrollPane scrollPane;
private JList<String> listArray;
private DefaultListModel modelList = new DefaultListModel();
//first object
private ProductCollection collection;
//constructor
public ProductGUI() {
setTitle("Product Inventory");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
//AddJFrame
AddJFrame.setTitle("Add Product");
AddJFrame.setSize(300,300);
AddJFrame.setLayout(null);
AddJFrame.add(addButton1);
addButton1.setBounds(200, 200, 70, 30);
addButton1.addActionListener(this);
AddJFrame.add(productNameText);
AddJFrame.add(productStockLevelText);
AddJFrame.add(productPriceText);
AddJFrame.add(cancelButton);
cancelButton.setBounds(70, 200, 75, 30);
AddJFrame.add(productName);
productName.setBounds(40, 13, 90, 15);
productNameText.setBounds(140, 10, 90, 20);
AddJFrame.add(productStockLevel);
productStockLevel.setBounds(40, 30, 100, 100);
productStockLevelText.setBounds(140, 70, 90, 20);
AddJFrame.add(productPrice);
productPrice.setBounds(40, 95, 100, 100);
productPriceText.setBounds(140, 135, 90, 20);
//Menu
setJMenuBar(bar);
bar.add(productMenu);
productMenu.add(addProductItem);
productMenu.add(modifyProductItem);
productMenu.add(sellProductItem);
productMenu.add(removeProductItem);
addProductItem.addActionListener(this);
modifyProductItem.addActionListener(this);
sellProductItem.addActionListener(this);
removeProductItem.addActionListener(this);
bar.add(infoMenu);
infoMenu.add(aboutApp);
//Panel Layout
add(BorderLayout.NORTH, topPanel);
topPanel.setBackground(Color.white);
topPanel.setPreferredSize(new Dimension(250, 250));
topPanel.setLayout(null);
middlePanel.setLayout(null);
//TopPanel buttons
topPanel.add(addButton);
removeButton.setBounds(350, 190, 80, 30);
topPanel.add(modifyButton);
sellButton.setBounds(250, 190, 80, 30);
topPanel.add(removeButton);
addButton.setBounds(50, 190, 80, 30);
topPanel.add(sellButton);
modifyButton.setBounds(150, 190, 80, 30);
addButton.addActionListener(this);
//collection object
collection = new ProductCollection();
//topPanel list
scrollPane = new JScrollPane();
topPanel.add(scrollPane);
scrollPane.setBounds(50, 10, 380, 150);
modelList = new DefaultListModel();
JList listArray = new JList(modelList);
scrollPane.setViewportView(listArray);
modelList.addElement("gfrew");
//set frame visible
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==addButton || e.getSource()==addProductItem)
{
AddJFrame.setVisible(true);
}
if(e.getSource()==addButton1)
{
if (productNameText.getText().equals("") && productStockLevelText.getText().equals("") && productPriceText.getText().equals(""))
{
try{
String productStockString = productStockLevelText.getText();
int productStockInt = Integer.parseInt(productStockString);
String productPriceString = productPriceText.getText();
double productPriceDouble = Double.parseDouble(productPriceString);
Product objectX = new Product(productNameText.getText(), productStockInt,productPriceDouble);
collection.addProduct(objectX);
modelList.addElement(objectX.getName());
}
catch (NumberFormatException f) {JOptionPane.showMessageDialog(getContentPane(),"Couldn't add the product, you didn't enter a number in the Stock/Price field", "Error",JOptionPane.ERROR_MESSAGE);
}
}
}}
这是ArrayList类
import java.util.*;
public class ProductCollection {
private ArrayList<Product> productsArrayList;
public ProductCollection(){
productsArrayList = new ArrayList<Product>();
}
public void addProduct(Product newProduct) {
productsArrayList.add(newProduct);
}
public void removeProduct(int removeProduct) {
productsArrayList.remove(removeProduct);
}
public ArrayList<Product> getProducts() {
return productsArrayList;
}
}
答案 0 :(得分:0)
if (productNameText.getText().equals("") && productStockLevelText.getText().equals("") && productPriceText.getText().equals(""))
{
修复了它。您是否要确保这些字段不为空?如果是这样
if (!productNameText.getText().equals("") && !productStockLevelText.getText().equals("") && !productPriceText.getText().equals(""))
{
除此之外,您的类设置,数组列表,模型列表和产品类都可以正常工作。在您的arraylist类中,请不要忘记添加getSize()方法并跟踪其获得了多少。这对于以后循环很重要。