想知道爲什麼這段代碼不會運行。它編譯,但沒有顯示GUI或任何由於某種原因,請幫助!這是一個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();} });
}
}
如果您可以稍微縮小問題的範圍,或者至少縮小可能負責的代碼(使潛在答案更容易) – ASGM 2013-03-03 18:45:56