[英]How can I add String JList element from another JPanel/JFrame/etc?
[英]How can I add element into a JList? inventory products program
我正在創建一個庫存程序,允許用戶添加,修改,銷售或刪除產品。 我創建了一個類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;
}
}
if (productNameText.getText().equals("") && productStockLevelText.getText().equals("") && productPriceText.getText().equals(""))
{
修復。 你想確保字段不是空的嗎? 如果是這樣的話
if (!productNameText.getText().equals("") && !productStockLevelText.getText().equals("") && !productPriceText.getText().equals(""))
{
除此之外,您的課程設置,arraylist,模型列表和產品類都很好並且有效。 在你的arraylist類中,不要忘記添加一個getSize()方法並跟蹤它獲得了多少。 這對於稍后循環非常重要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.