如何在使用ActionListener按钮和GUI时向数组列表中添加项目?

我有一个学校作业,我必须创建一个java GUI,并将比萨饼添加到数组列表中。数组列表是在一个单独的 Order 类。这是我第一次在Java中使用GUI,所以我还在努力弄清楚事情。

问题是,如果我在按钮点击事件中初始化订单类,每次我按 “add pizza to order “按钮时,它都会重新创建订单类和数组列表并重新开始。如果我在按钮点击事件之外初始化订单类,它在点击事件方法里面就不被识别。

我也试过在主方法中初始化该类,但没有成功。我将在下面发布主方法类。请记住,还有一个 Order 阶级和 Pizza 类。

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// Creating the FirstGUI class to hold the main method and GUI code
public class FirstGUI extends JFrame implements ActionListener {

    // Labels to give directions to the customer
    public JLabel labelSelectPizzaSize = new JLabel("Please Select a Size for Your Pizza: [S]mall, [M]edium, or [L]arge");
    public JLabel labelSelectPizzaCrust = new JLabel("Please Select a Crust Type: [Thick] or [Thin]");
    public JLabel labelHasPepperoni = new JLabel("Do You Want Pepperoni? [Y]es or [N]o");
    public JLabel labelTotalBakeTime = new JLabel("Total Bake Time: ");
    public JLabel labelOrderTotal = new JLabel("Order Total: ");

    // Text boxes for customer input
    public JTextField textEnterPizzaSize = new JTextField(5);
    public JTextField textEnterPizzaCrust = new JTextField(5);
    public JTextField textHasPepperoni = new JTextField(5);

    // Button to add pizza to order
    public JButton buttonAddPizzaToOrder = new JButton("Add Pizza to Order");

    // Main method
    public static void main(String[] args) {

        // Creating a new class instance
        new FirstGUI();
    }

    FirstGUI() {

        // Setting visibility to true
        this.setVisible(true);

        // Setting the default close operation
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Creating panels
        JPanel panelMain = new JPanel();
        JPanel panelPizzaSize = new JPanel();
        JPanel panelPizzaCrust = new JPanel();
        JPanel panelPizzaTopping = new JPanel();
        JPanel panelButtons = new JPanel();
        JPanel panelOrderInfo = new JPanel();

        // Setting the layout for the ain panel
        panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));

        // Adding ActionListner to the "Add Pizza To Order" button.
        buttonAddPizzaToOrder.addActionListener(this);

        // Adding labels, text fields, and buttons to the panels and then adding panels to the main panel.
        panelPizzaSize.add(labelSelectPizzaSize);
        panelPizzaSize.add(textEnterPizzaSize);
        panelPizzaCrust.add(labelSelectPizzaCrust);
        panelPizzaCrust.add(textEnterPizzaCrust);
        panelPizzaTopping.add(labelHasPepperoni);
        panelPizzaTopping.add(textHasPepperoni);
        panelButtons.add(buttonAddPizzaToOrder);
        panelMain.add(panelPizzaSize);
        panelMain.add(panelPizzaCrust);
        panelMain.add(panelPizzaTopping);
        panelMain.add(panelButtons);
        panelOrderInfo.add(labelTotalBakeTime);
        panelOrderInfo.add(labelOrderTotal);
        panelMain.add(panelOrderInfo);

        // I'm not sure what this does.
        this.getContentPane().add(panelMain);

        // Setting size of the main panel.
        this.setSize(475, 250);
    }

    // Method to create a pizza and add it to the array list when the "Add Pizza To Order" button is pressed.
    public void actionPerformed(ActionEvent event1) {
        try {
            String a;
            String b;
            String c;

            a = textEnterPizzaSize.getText().toUpperCase();
            b = textEnterPizzaCrust.getText().toUpperCase();
            c = textHasPepperoni.getText().toUpperCase();

            Order O = new Order();
            O.addPizza(new Pizza(a, b, c));

            labelTotalBakeTime.setText("Total Bake Time: " + Double.toString(O.calculateBakeTime()));
            labelOrderTotal.setText("-  Order Total: " + Double.toString(O.calculateCost()));

            // Conformation message when a pizza is added to the order.
            JOptionPane.showMessageDialog(null, "Size: " + a + ", Crust: " + b + ", Pepperoni: " + c,
                    "Pizza Successfully Added To Order", JOptionPane.INFORMATION_MESSAGE);
        }
        // Exception and ERROR message handling
        catch (MyException me) {
            JOptionPane.showMessageDialog(null, me, "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }
}

解决方案:

试试这个方法。

添加一个类变量来保存您的订单

private Order order;

那么

public static void main( String[] args )
{

    // Creating a new class instance
    Order order = new Order(); // Create order here
    new FirstGUI( order );  // Pass the order to the constructor
}

在构造函数中初始化顺序

FirstGUI( Order order )
{
    this.order = order;
    // other remaining code
}

actionPerformed 方法,将pizze添加到这个命令

order.addPizza( new Pizza( a, b, c ) );

给TA打赏
共{{data.count}}人
人已打赏
解决方案

在MySQL 8.0中CONTAINS关键字是用来做什么的?

2022-4-22 18:08:52

解决方案

错误 "IndexError: 如何在Keras中使用训练好的模型预测输入图像?

2022-4-22 18:08:54

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索