简体   繁体   English

如何将元素添加到JList中? 库存产品计划

[英]How can I add element into a JList? inventory products program

I'm creating an inventory program which will allow user to add,modify, sell or remove product. 我正在创建一个库存程序,允许用户添加,修改,销售或删除产品。 I created a class Product, a class for the GUI and one for the ArrayList. 我创建了一个类Product,一个GUI类和一个ArrayList类。 I'm having problems adding the products to the JList and to the ArrayList, I want that once the user will press the button (addButton1) the product will be added either in the JList and in the ArrayList. 我在将产品添加到JList和ArrayList时遇到问题,我希望一旦用户按下按钮(addButton1),产品将被添加到JList和ArrayList中。 could anyone help me? 谁能帮助我? It will be really appreciated, thanks in advance. 非常感谢,提前感谢。

This is my Product class 这是我的产品类

//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}

This is the GUI class 这是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);
                        }
                    } 

                }}

This is the ArrayList class 这是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;
    }

}

在此输入图像描述 Well I ran your application, and removing the 好吧,我运行你的应用程序,并删除

if (productNameText.getText().equals("") && productStockLevelText.getText().equals("") && productPriceText.getText().equals(""))
                {

fixed it. 修复。 Are you trying to make sure the fields aren't empty? 你想确保字段不是空的吗? If so it would be 如果是这样的话

if (!productNameText.getText().equals("") && !productStockLevelText.getText().equals("") && !productPriceText.getText().equals(""))
                {

Besides for that, your class setup, arraylist, model list, and product class are all fine and work. 除此之外,您的课程设置,arraylist,模型列表和产品类都很好并且有效。 In your arraylist class, do not forget to add a getSize() method and keep track of how much it is getting. 在你的arraylist类中,不要忘记添加一个getSize()方法并跟踪它获得了多少。 This will be important for looping later. 这对于稍后循环非常重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM