想知道为什么这段代码不会运行。它可以编译,但由于某种原因不显示 GUI 或任何东西,请帮忙!这是一个在 1 帧中有 2 个,最终 3 个面板的程序。如果我删除了到目前为止我所做的一切,但只保留一个组合框/一个面板的东西,它就可以正常工作并完美显示/编译。
无论如何,这里是代码!
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class FastFood implements ActionListener
{
JFrame frame;
JPanel contentpane1; //this panel will include cheese burger, chicken sandwich, hot dog, pizza, and salad
JPanel contentpane2; // this pane will include side salad, french fried, or apple slice
JPanel contentpane3; // this panel will include coke, diet coke, sprite, ice tea, coffee, root beer, or water
JComboBox maindish; //combo box for maindishes
JComboBox sides; //box for sides
JComboBox drink; //box for drinks
JLabel fastfoodp1; // label for main dish
JLabel fastfoodp2; //label for sides
JLabel fastfoodp3; // label for drinks
JLabel c1; // cost for main dish
JLabel c2; //cost for drink
JLabel c3; //cost for sides
JLabel cost; //label to display the total cost
public FastFood()
{
frame = new JFrame("Order your fastfood!"); //sets up frame title
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//all of this code is for the maindish
contentpane1 = new JPanel(); //creats panel object
contentpane1.setLayout(new BoxLayout(contentpane1, BoxLayout.PAGE_AXIS)); //creates layout
contentpane1.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); //creates border
//next three lines of code set up the label to prompt user for main dish
fastfoodp1 = new JLabel("Select a main dish");
fastfoodp1.setAlignmentX(JLabel.LEFT_ALIGNMENT);
contentpane1.add(fastfoodp1);
//next code sets up the combo box options and adds it to the panel
String[] MainDishes = {"Cheese Burger", "Chicken Sandwich", "Hot Dog", "Pizza", "Salad" };
maindish = new JComboBox(MainDishes);
maindish.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
maindish.setSelectedIndex(0);
maindish.addActionListener(this);
contentpane1.add(maindish);
//sets up price label for selected item
c1 = new JLabel("Cost of Main Dish");
c1.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));
contentpane1.add(c1);
//next code is for sides
contentpane2 = new JPanel();
contentpane2.setLayout(new BoxLayout(contentpane1, BoxLayout.PAGE_AXIS));
contentpane2.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
//next codes set up label to prompt user for sides
fastfoodp2 = new JLabel("Select a side");
fastfoodp2.setAlignmentX(JLabel.CENTER_ALIGNMENT);
contentpane2.add(fastfoodp2);
//next code sets up the combo box options and adds it to the panel
String[] SideDishes = {"Side Salad", "Frend Fries", "Apple Slice" };
sides = new JComboBox(SideDishes);
sides.setAlignmentX(JComboBox.CENTER_ALIGNMENT);
sides.setSelectedIndex(0);
sides.addActionListener(this);
contentpane2.add(sides);
//sets up price label for selected item
c2 = new JLabel("Cost of Side Dish");
c2.setBorder(BorderFactory.createEmptyBorder(20,0,0,0));
contentpane2.add(c2);
//next code is for Drinks
//makes it visible
frame.setContentPane(contentpane1);
frame.pack();
frame.setVisible(true);
frame.setContentPane(contentpane2);
frame.pack();
frame.setVisible(true);
frame.add(contentpane1, BorderLayout.WEST);
frame.add(contentpane2, BorderLayout.CENTER);
//next code is for sides
}
public void actionPerformed(ActionEvent event)
{
JComboBox comboBox = (JComboBox)event.getSource();
String maindishname =(String)comboBox.getSelectedItem();
if (maindishname == "Cheeese Burger")
{
c1.setText("$3.50 for Cheese Burger");
}
else if (maindishname == "Chicken Sandwich")
{
c1.setText("$2.50 for Chicken Sandwich");
}
else if (maindishname == "Hot Dog")
{
c1.setText("$2.50 for Hot Dog");
}
else if (maindishname == "Pizza")
{
c1.setText("$2.00 for Pizza");
}
else if (maindishname == "Salad")
{
c1.setText("$1.50 for Salad");
}
JComboBox sides = (JComboBox)event.getSource();
String sidedishname = (String)sides.getSelectedItem();
if(sidedishname == "Side Salad")
{
c2.setText("$0.50 for Side Salad");
}
else if (sidedishname == "French Fries")
{
c2.setText("$1.00 for French Fries");
}
else if (sidedishname == "Apple Slice")
{
c2.setText("$0.75 for Applie Slice");
}
}
private static void runGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
FastFood food = new FastFood();
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {public void run() {runGUI();} });
}
}