Java CardLayout Main Menu Problem

后端 未结 2 707
醉梦人生
醉梦人生 2020-12-07 05:08

Ok so im working on this game in java called 8 bit chimera. Im working on the main menu right now but when im using the card layout the window wont open for some reason. Her

相关标签:
2条回答
  • 2020-12-07 05:53

    I don't see where you ever set the layout of a container to be CardLayout, and if you don't set the layout to this, you can't magically use it. If you haven't yet gone through the CardLayout tutorial, consider doing so as it's all explained there.

    Edit 1
    Comment from Alexander Kim:

    when i added the cardbagLayout it wont load the image and the button size filled the whole screen. I also took away the grids

    You need to nest your JPanels in order to nest layouts. Use a single JPanel as the CardLayout container whose single function it is is to display other JPanels (the "cards"). These other JPanels will use whatever layouts that are necessary to properly display the components that they hold such as your JButton or "grids" (whatever they are). And even these JPanels may hold other JPanels that use other layouts.

    Again, please read the layout tutorials as it's all described well there. You will not regret doing this.

    Edit 2
    Here's a very simple example that uses a CardLayout. The component displayed by the CardLayout using JPanel (called the cardContainer) is changed depending on which item is selected in a combobox.

    Here's the CardLayout and the JPanel that uses it:

    private CardLayout cardLayout = new CardLayout();

       // *** JPanel to hold the "cards" and to use the CardLayout:
       private JPanel cardContainer = new JPanel(cardLayout); 
    

    And here's how I add a component to the cardlayout-using JPanel:

      JPanel redPanel = new JPanel();
      //...
      String red = "Red Panel";
      cardContainer.add(redPanel, red); // add the JPanel to the container with the String
    

    I also add the String to a JComboBox so I can use this combo box later to tell the CardLayout to display this JPanel (redPanel) if the user selects the item "Red" in this same JComboBox:

      cardCombo.addItem(red); // also add the String to the JComboBox
    

    Here's the ActionListener in the JComboBox that lets me change the item displayed in the cardlayout using JPanel:

      cardCombo.addActionListener(new ActionListener() {
    
         public void actionPerformed(ActionEvent e) {
            String item = cardCombo.getSelectedItem().toString();
    
            // *** if combo box changes it tells the CardLayout to
            // *** swap views based on the item selected in the combo box:
            cardLayout.show(cardContainer, item);
         }
      });
    

    And here's the whole shebang:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class SimpleCardLayoutDemo {
       private CardLayout cardLayout = new CardLayout();
    
       // *** JPanel to hold the "cards" and to use the CardLayout:
       private JPanel cardContainer = new JPanel(cardLayout); 
       private JComboBox cardCombo = new JComboBox();
       private JPanel comboPanel = new JPanel();;
    
       public SimpleCardLayoutDemo() {
          JPanel greenPanel = new JPanel(new BorderLayout());
          greenPanel.setBackground(Color.green);
          greenPanel.add(new JScrollPane(new JTextArea(10, 25)), BorderLayout.CENTER);
          greenPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
          greenPanel.add(new JButton("Bottom Button"), BorderLayout.PAGE_END);
          String green = "Green Panel";
          cardContainer.add(greenPanel, green);
          cardCombo.addItem(green);
    
          JPanel redPanel = new JPanel();
          redPanel.setBackground(Color.red);
          redPanel.add(new JButton("Foo"));
          redPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
          String red = "Red Panel";
          cardContainer.add(redPanel, red);
          cardCombo.addItem(red);
    
          JPanel bluePanel = new JPanel();
          bluePanel.setBackground(Color.blue);
          JLabel label = new JLabel("Blue Panel", SwingConstants.CENTER);
          label.setForeground(Color.white);
          label.setFont(label.getFont().deriveFont(Font.BOLD, 32f));
          bluePanel.add(label);
          String blue = "Blue Panel";
          cardContainer.add(bluePanel, blue);
          cardCombo.addItem(blue);
    
          comboPanel.add(cardCombo);
          cardCombo.addActionListener(new ActionListener() {
    
             public void actionPerformed(ActionEvent e) {
                String item = cardCombo.getSelectedItem().toString();
    
                // *** if combo box changes it tells the CardLayout to
                // *** swap views based on the item selected in the combo box:
                cardLayout.show(cardContainer, item);
             }
          });
       }
    
       public JPanel getCardContainerPanel() {
          return cardContainer;
       }
    
    
       public Component getComboPanel() {
          return comboPanel ;
       }
    
       private static void createAndShowUI() {
          SimpleCardLayoutDemo simplecardDemo = new SimpleCardLayoutDemo();
    
          JFrame frame = new JFrame("Simple CardLayout Demo");
          frame.getContentPane().add(simplecardDemo.getCardContainerPanel(), BorderLayout.CENTER);
          frame.getContentPane().add(simplecardDemo.getComboPanel(), BorderLayout.PAGE_END);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       // to run Swing in a thread-safe way
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    0 讨论(0)
  • 2020-12-07 06:01

    Your problem is with add(BGR,"Breed");. The layout of MainMenuComp is a GridBagLayout, so the constraint must be a GridBagConstraint, not a String (you have "Breed" as the constraint).

    0 讨论(0)
提交回复
热议问题