java - 使用 ActionListener 按钮和 GUI 时如何将项目添加到数组列表?
问题描述
我有一个学校作业,我必须创建一个 java GUI 并将比萨饼添加到数组列表中。数组列表在一个单独的Order
类中。这是我第一次在 Java 中使用 GUI,所以我仍在努力解决问题。
问题是,如果我在按钮单击事件期间初始化订单类,每次按下“将披萨添加到订单”按钮时,它都会重新创建订单类和数组列表并重新开始。如果我在按钮单击事件之外初始化订单类,则在单击事件方法中无法识别它。
我还尝试在 main 方法中初始化类,但没有成功。我将在下面发布主要方法类。请记住,还有一个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
方法中,将披萨添加到此订单中
order.addPizza( new Pizza( a, b, c ) );
推荐阅读
- react-native - 为计步器反应原生 Google Fit api
- jenkins - Docker for Jenkins 从节点中的 Docker 不工作
- sql - 查询具有多个父节点的闭包表中所有节点的所有路径
- asp.net-core - 找不到 ASP.Net Core 3.1 + OData V4 元数据
- powershell - 是否有任何 powershell 命令或参数可以绕过 UAC
- tensorflow - “ImportError: libcuda.so.1: cannot open shared object file: No such file or directory” when “Using TensorFlow backend;”
- office365 - AADSTS500089:SAML 2.0 断言验证失败:SAML 令牌无效。办公室 365
- c# - C# 中第 94 个斐波那契数的错误
- react-native - 导航容器中的圆角边框
- slack - 将成员添加到 slack 中的频道列表