gpt4 book ai didi

java - 如何使用 arrayList 存储 JFrame 中的信息并将其显示在 JOptionPane 中?

转载 作者:行者123 更新时间:2023-12-01 18:45:13 25 4
gpt4 key购买 nike

我的作业要求我使用 arrayList 来获取每个订单的用户选择并将它们显示在订单摘要 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;
}
}

最佳答案

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

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 而不是选项 Pane ,它更舒服。

关于java - 如何使用 arrayList 存储 JFrame 中的信息并将其显示在 JOptionPane 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18048820/

25 4 0