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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在使用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 ) );

以上是关于如何在使用ActionListener按钮和GUI时向数组列表中添加项目?的主要内容,如果未能解决你的问题,请参考以下文章

如何在另一个类中的 JPanel 类中实现 ActionListener?

ActionListener的三种实现方法

ActionListener的三种实现方法

[Java GUI]学习笔记1

如何在 GUI 中使用按钮打开单独的、可编辑的窗口?

JAVA Swing 多个按钮组