简体   繁体   English

(Java)JPanel无法打印出来

[英](Java) JPanel Not Printing Out

I'm doing a JPanel assignment in my college course and I'm having trouble with the final print out. 我在大学课程中正在做一个JPanel作业,但在最终打印时遇到了麻烦。 The panel itself looks great and everything, the only thing I can't get it to do is display the line that is supposed to be printed out. 面板本身看起来很棒,并且所有事情,我唯一无法做的就是显示应该打印的行。

It's designed to take the input of a grocery item, with the name, price, category and quantity all being input by the user. 它旨在接收杂货项目的输入,而名称,价格,类别和数量均由用户输入。 When the "Add to Cart" button is clicked, it should display all the information previously typed in. But as of right now it does not. 单击“添加到购物车”按钮时,它应该显示以前输入的所有信息。但是,到目前为止,它还没有。

Here's the two classes I'm using: 这是我正在使用的两个类:

MyCart.java MyCart.java

package assignment2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class MyCart extends JFrame {

    private JPanel display;
    private JLabel itemLabel;
    private JTextField item;
    private JLabel catLabel;
    private JComboBox category;
    private JLabel quanLabel;
    private JTextField quantity;
    private JLabel priceLabel;
    private JTextField price;

    private JPanel btns;
    private JButton addBtn;
    private JButton exitBtn;

    private JPanel list;
    private JList myList;

    private ArrayList<Item> itemList;

    public MyCart() {

        itemList = new ArrayList();
        myList = new JList(itemList.toArray());

        this.setLayout(new GridLayout(2,1,5,5));

        display = new JPanel();
        display.setLayout(new GridLayout(5,2,5,5));

        itemLabel = new JLabel("  Item: ");
        item = new JTextField(10);
        catLabel = new JLabel("  Category: ");
        category = new JComboBox(new String[]{"Meat", "Fruit/Vegetable", "Dairy", "Grains", "Sweets", "Other"});
        quanLabel = new JLabel("  Quantity: ");
        quantity = new JTextField(10);
        priceLabel = new JLabel("  Price: ");
        price = new JTextField(10);

        display.add(itemLabel);
        display.add(item);
        display.add(catLabel);
        display.add(category);
        display.add(quanLabel);
        display.add(quantity);
        display.add(priceLabel);
        display.add(price);

        this.add(display);

        btns = new JPanel();
        btns.setLayout(new GridLayout(1,3,5,5));

        addBtn = new JButton("Add to Cart");
        exitBtn = new JButton("Exit");

        btns.add(addBtn);
        btns.add(exitBtn);

        this.add(btns); 

        list = new JPanel();
        list.setLayout(new FlowLayout());
        list.add(new JLabel("My Cart:"));
        JScrollPane myScrollPane = new JScrollPane(myList);
        list.add(myScrollPane);

        setLayout(new BorderLayout());
        this.add(display,BorderLayout.NORTH);
        this.add(btns,BorderLayout.CENTER);
        this.add(list,BorderLayout.SOUTH);

        //event listener object created
        ButtonListeners buttonListener = new ButtonListeners();
        addBtn.addActionListener(buttonListener);
        exitBtn.addActionListener(buttonListener);

    }

    public static void main(String[] args) {
        MyCart cart = new MyCart();
        cart.setTitle("Cart <Adam>");
        cart.setSize(400,350);
        cart.setLocationRelativeTo(null);
        cart.setVisible(true);
        cart.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

    //event listener class for adding
    class ButtonListeners implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            switch(e.getActionCommand()) {

                case "Add": String n = item.getText();
                            String c = category.getSelectedItem().toString();
                            double q = Double.valueOf(quantity.getText());
                            double p = Double.valueOf(price.getText());
                            Item myItem = new Item();
                            myItem.setName(n);
                            myItem.setCategory(c);
                            myItem.setQuantity(q);
                            myItem.setPrice(p);

                            itemList.add(myItem);
                            myList.setListData(itemList.toArray());
                            break;

                case "Exit": System.exit(0);

            }
        }
    }
    //exit listener
    class ExitListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
           System.exit(0);
        }
    }
}

Item.java Item.java

package assignment2;

public class Item {

    private String name;
    private String category;
    private double price;
    private double quantity;

    public Item(String name, String category, double price, double quantity) {

    }

    public Item() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getQuantity() {
        return quantity;
    }

    public void setQuantity(double quantity) {
        this.quantity = quantity;
    }

    public double calcAmount() {
        return quantity * price;
    }

    public String toString(){
        return(name + ", " + category + ", " + quantity + ", " + price + ", " + calcAmount()); 
    }
}

You forgot to add the ActionCommand for the JButton that checks the case statement in your ActionListener 您忘记为检查ActionListener的case语句的JButton添加ActionCommand

Everytime you call this switch(e.getActionCommand()) the Listener is grabbing the ActionCommand of the button that was added, if it is not added then the name of the button is the ActionCommand . 每次调用此switch(e.getActionCommand()) ,侦听器都会抓住已添加按钮的ActionCommand ,如果未添加,则按钮的名称为ActionCommand

solution: 解:

addBtn = new JButton("Add to Cart");
addBtn.setActionCommand("Add"); //will call the Add case actionListener

Rod has given you the solution to your actual problem, however you are not using the JList and the ListModel properly. Rod为您提供了解决实际问题的解决方案,但是您没有正确使用JListListModel

Changes to the data in the model should be done via the ListModel . 对模型中数据的更改应通过ListModel So there is not need for your ArrayList to keep track of the Items. 因此,您的ArrayList不需要跟踪项目。 The ListModel will do this for you. ListModel将为您执行此操作。

So your basic code should be: 因此,您的基本代码应为:

DefaultListModel<Item> model = new DefaultListModel<Item>();
JList list = new JList( model );

Then then you want to add items to the JList you actually update the ListModel: 然后,您想将项目添加到JList中,实际更新ListModel:

model.addElement( myItem );

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

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