为什么我的代码不能同时运行两个面板?当我运行它时它没有显示任何东西,但它编译

时间:2021-12-29 08:33:47

Wondering why this code won't run. It compiles but doesnt show the GUI or anything for some reason please help! It's a program with 2, eventually 3 panels in 1 frame. If i delete everything i've done so far, but just keep the stuff for one combo box/one panel it works and shows/compiles perfectly.

想知道为什么这段代码不能运行。它编译,但没有显示GUI或任何由于某种原因请帮助!这是一个2的程序,最终3个面板在1帧。如果我删除到目前为止我所做的一切,但只是保留一个组合框/一个面板的工作,并显示/编译完美。

Anyways here is the code!

无论如何这里是代码!

    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();} });
        }
}

1 个解决方案

#1


4  

BoxLayout does not allow sharing of target components. Replace

BoxLayout不允许共享目标组件。更换

contentpane2.setLayout(new BoxLayout(contentpane1, BoxLayout.PAGE_AXIS));

with

contentpane2.setLayout(new BoxLayout(contentpane2, BoxLayout.PAGE_AXIS));

Note the use of contentpane2 as the target Component.

注意使用contentpane2作为目标Component。

Don't use JFrame#setContentPane if you are adding components to various locations of the BorderLayout of the JFrame.

如果要将组件添加到JFrame的BorderLayout的各个位置,请不要使用JFrame#setContentPane。

You will need to override getPreferredSize in the JPanels contentpane1 and contentpane2 to facilitate correct sizing using pack.

您需要覆盖JPanels contentpane1和contentpane2中的getPreferredSize,以便使用pack正确调整大小。

Also make sure to call JFrame#setVisible after adding all components to the JFrame.

还要确保在将所有组件添加到JFrame后调用JFrame #setVisible。

#1


4  

BoxLayout does not allow sharing of target components. Replace

BoxLayout不允许共享目标组件。更换

contentpane2.setLayout(new BoxLayout(contentpane1, BoxLayout.PAGE_AXIS));

with

contentpane2.setLayout(new BoxLayout(contentpane2, BoxLayout.PAGE_AXIS));

Note the use of contentpane2 as the target Component.

注意使用contentpane2作为目标Component。

Don't use JFrame#setContentPane if you are adding components to various locations of the BorderLayout of the JFrame.

如果要将组件添加到JFrame的BorderLayout的各个位置,请不要使用JFrame#setContentPane。

You will need to override getPreferredSize in the JPanels contentpane1 and contentpane2 to facilitate correct sizing using pack.

您需要覆盖JPanels contentpane1和contentpane2中的getPreferredSize,以便使用pack正确调整大小。

Also make sure to call JFrame#setVisible after adding all components to the JFrame.

还要确保在将所有组件添加到JFrame后调用JFrame #setVisible。