0

我的任务要求我使用 arrayList 来获取每个订单的用户选择并将它们显示在 Order Summary JOptionPane 中。我必须为arrayList 创建一个新类吗?我们在不使用 GUI 的情况下学习它们,所以我不确定如何让它显示在每个订单的订单摘要中。例如,如果有人选择了一个子类型,我将如何获取类型和价格,将其存储在 arrayList 中,并将其显示在订单摘要中。如果有人向我解释了如何做一个,那么我可以弄清楚其余的。

已编辑

我试图以与其他选项相同的方式获得“浇头”,但它不起作用。你如何让它与 JCheckBox 一起工作。另外,我如何用数字来获得价格?您可以在我的代码中看到我正在尝试使用案例/开关的方法。我究竟做错了什么?

谢谢

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class Subway extends JFrame {

    private String[] breadNames = {"9-grain Wheat", "Italian", "Honey Oat",
        "Herbs and Cheese", "Flatbread"};
    private String[] subTypes = {"Oven Roasted Chicken - $3.50",
        "Meatball Marinara - $4.50", "Cold Cut Combo - $4.00",
        "BLT - $3.75", "Blackforest Ham - $4.00", "Veggie Delight - $2.50"};
    private String[] cheesetypes = {
        "Cheddar", "American", "Provolone", "Pepperjack"};
    private String[] size = {"6 inch", "Footlong"};
    private String[] toasted = {"Yes", "No"};
    private JTextField jtfname = new JTextField(10);
    private JComboBox<String> jcbBread = new JComboBox<String>(breadNames);
    private JComboBox<String> jcbtype = new JComboBox<String>(subTypes);
    private JComboBox<String> jcbcheese = new JComboBox<String>(cheesetypes);
    private JButton jbtExit = new JButton("EXIT");
    private JButton jbtAnother = new JButton("Next Order");
    private JButton jbtSubmit = new JButton("SUBMIT");
    private JComboBox<String> jcbSize = new JComboBox<String>(size);
    private JComboBox<String> jcbToasted = new JComboBox<String>(toasted);
    private JCheckBox jcbLettuce = new JCheckBox("Lettuce");
    private JCheckBox jcbSpinach = new JCheckBox("Spinach");
    private JCheckBox jcbOnion = new JCheckBox("Onion");
    private JCheckBox jcbPickles = new JCheckBox("Pickles");
    private JCheckBox jcbTomatoes = new JCheckBox("Tomatoes");
    private JCheckBox jcbPeppers = new JCheckBox("Peppers");
    private JCheckBox jcbMayo = new JCheckBox("Mayo");
    private JCheckBox jcbMustard = new JCheckBox("Mustard");
    private JCheckBox jcbDressing = new JCheckBox("Italian Dressing");

    public Subway() {
        //name
        JPanel p1 = new JPanel(new GridLayout(24, 1));
        p1.add(new JLabel("Enter Name"));
        p1.add(jtfname);
        //size
        p1.add(new JLabel("Select a size"));
        p1.add(jcbSize);
        //bread
        p1.add(new JLabel("Select a Bread"));
        p1.add(jcbBread);
        //type
        p1.add(new JLabel("What type of sub would you like?"));
        p1.add(jcbtype);
        //cheese
        p1.add(new JLabel("Select a cheese"));
        p1.add(jcbcheese);
        //toasted
        p1.add(new JLabel("Would you like it toasted?"));
        p1.add(jcbToasted);
        //toppings
        p1.add(new JLabel("Select your toppings"));
        p1.add(jcbLettuce);
        p1.add(jcbSpinach);
        p1.add(jcbPickles);
        p1.add(jcbOnion);
        p1.add(jcbTomatoes);
        p1.add(jcbPeppers);
        p1.add(jcbMayo);
        p1.add(jcbMustard);
        p1.add(jcbDressing);
        // BUTTON PANEL
        JPanel p5 = new JPanel();
        p5.setLayout(new BoxLayout(p5, BoxLayout.LINE_AXIS));
        p5.add(Box.createHorizontalGlue());// KEEPS THEM HORIZONTAL
        p5.add(jbtExit);
        p5.add(jbtAnother);
        p5.add(jbtSubmit);
        // ADDING PANELS AND WHERE THEY GO
        add(p1, BorderLayout.NORTH);// TOP
        add(p5, BorderLayout.SOUTH);// BOTTOM
        // SETTING INVISIBLE BORDERS AROUND PANELS TO SPACE THEM OUT
        p1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        p5.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        // EXIT BUTTON LISTENER
        jbtExit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {

                System.exit(0);

            }
        });
        // Another order BUTTON LISTENER
        jbtAnother.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                makeASandwich();


            }
        });
        // SUBMIT BUTTON LISTENER
        jbtSubmit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                makeASandwich();
                for (Sandwich s : Sandwich.order) {
                    String Order = " ";
                    Order = jtfname.getText() + "\n"
                            + "\nSize: " + s.getSize()
                            + "\nType of Sub: " + s.getName()
                            + "\nBread: " + s.getBread()
                            + "\nCheese: " + s.getCheese()
                            + "\nToasted? " + s.getToasted()
                            + "\n\n---Next Order---";
                    JOptionPane.showMessageDialog(null, Order);
                }
            }
        });
    }

    private void makeASandwich() {
        Sandwich sandwich = new Sandwich(jcbtype.getItemAt(jcbtype.getSelectedIndex()));
        sandwich.setBread(jcbBread.getItemAt(jcbBread.getSelectedIndex()));
        sandwich.setCheese(jcbcheese.getItemAt(jcbcheese.getSelectedIndex()));
        sandwich.setToasted(jcbToasted.getItemAt(jcbToasted.getSelectedIndex()));
        sandwich.setSize(jcbSize.getItemAt(jcbSize.getSelectedIndex()));
        // sandwich.setPrice(jcbtype.getItemAt(jcbtype.getSelectedIndex()));
        Sandwich.order.add(sandwich);
    }

    public static void main(String[] args) {
        Subway frame = new Subway();
        frame.pack();
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("SUBWAY");
        frame.setVisible(true);
        frame.setSize(600, 750);
    }// ENDOFMAIN
}// ENDOFCLASS

class HoldOrder {

    public static List<Sandwich> order = new ArrayList<Sandwich>();
}

class Sandwich {

    String bread = "";
    String sandwichName = "";
    String cheese = "";
    String size = "";
    String toasted = "";
    private double total = 0;
    public static List<Sandwich> order = new ArrayList<Sandwich>();

    public Sandwich(String typeOfSandwich) {
        sandwichName = typeOfSandwich;
        //switch(setPrice) { 
        //case "BLT":
        //  total += 3.00D;
        //   break;
        //  case "Turkey Bacon":
        //   total += 3.50D;
        //  break;
        //}
    }

    public String getName() {
        return sandwichName;
    }

    public void setBread(String s) {
        bread = s;
    }

    public String getBread() {
        return bread;
    }

    public void setCheese(String s) {
        cheese = s;
    }

    public String getCheese() {
        return cheese;
    }

    public void setSize(String s) {
        size = s;
    }

    public String getSize() {
        return size;
    }

    public void setToasted(String s) {
        toasted = s;
    }

    public String getToasted() {
        return toasted;
    }
}
4

2 回答 2

2

您发布了很多代码,因此,我为您创建了一个演示:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 *
 * @author Azad
 */
public class ShowListDemo extends JFrame implements ActionListener{
    private java.util.List<String> list;

    public ShowListDemo(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new JPanel();
        JButton show = new JButton("Show List Content");
        show.addActionListener(this);
        p.add(show);

        list = new ArrayList<>();
        list.add("infromation one");
        list.add("information two");
        list.add("information three");

        add(p);
        setLocationRelativeTo(null);
        pack();
    }

    public static void main(String...args){
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
              new ShowListDemo().setVisible(true);
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
       String  s = "" ;
        for(String a : list)
            s += a +"\n";
        JOptionPane.showMessageDialog(this, s);//@Mad programmer's recommended, I can't refuse :)
    }
}

还创建一个用于向列表添加元素的按钮,但是,我更喜欢JList选项窗格,它更舒服。

于 2013-08-05T00:25:47.747 回答
2

List您可以将of s格式化String为 HTML 并允许 Swing 中的 HTML 支持来呈现它,例如JOptionPane 中的 Display 数组JOptionPane 多维数组输出和JOptionPane 在 Java中显示 HTML 问题

许多人忘记的东西, a 的第二个参数JOptionPane是 a Object,它JOptionPane允许您传递 aComponent它将用作主要显示。这意味着您可以显示一个JTableJList例如。

查看JOptionPane 在 Java 中显示 HTML 问题的示例

于 2013-08-05T00:31:44.837 回答