简体   繁体   中英

How can I properly use a JPanel as container for several other JPanels?

I'm designing a GUI in Swing. I want a particular area of my gui - the center view - to be able to change its display in response to action events. The center view must never change in size no matter what it displays.

Now when I have a JPanel panel1 with a preffered size and put it into panel2 without any specified size, panel2 will display exactly as panel1 would; You can't even see the double JPanel layer. But since my center frame (panel2) should not change in size, I tried setting panel2 to a specific size and giving it a BorderLayout . Now if I call panel2.add(panel1, BorderLayout.center ) where panel1 has the exact same dimension as panel2, the double JPanel layer is visible and the center view displays bad.

How can I add panel1 to panel2, where both have same dimension, so panel2 isn't visible? Or better yet, how should I actually be doing this? (With simple LayoutManagers )

I'll make it an answer since it is the canonical answer to this question:

Use a CardLayout. You can find the tutorial here . A CardLayout would be perfect for making sure that the center JPanel, the card-holder JPanel, won't change size. You can swap cards from pretty much any control structure, be it an ActionListener held by a JButton or from a JComboBox.

For example:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class CardExample extends JPanel {
   enum MyColor {
      RED("Red", Color.RED), YELLOW("Yellow", Color.YELLOW), BLUE("Blue",
            Color.BLUE);
      private String name;
      private Color color;

      private MyColor(String name, Color color) {
         this.name = name;
         this.color = color;
      }

      public String getName() {
         return name;
      }

      public Color getColor() {
         return color;
      }
   }

   private static final int GAP = 3;

   private CardLayout cardlayout = new CardLayout();
   private JPanel cardHolderPanel = new JPanel(cardlayout);

   public CardExample() {
      JPanel btnPanel = new JPanel(new GridLayout(1, 0, GAP, GAP));
      for (MyColor myColor : MyColor.values()) {
         JPanel cardPanel = new JPanel();
         cardPanel.setPreferredSize(new Dimension(200, 200));
         cardPanel.setBackground(myColor.getColor());
         cardHolderPanel.add(cardPanel, myColor.getName());

         btnPanel.add(new JButton(new ButtonAction(myColor)));
      }

      setLayout(new BorderLayout(GAP, GAP));
      setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
      add(cardHolderPanel, BorderLayout.CENTER);
      add(btnPanel, BorderLayout.SOUTH);
   }

   private class ButtonAction extends AbstractAction {
      private MyColor myColor;

      public ButtonAction(MyColor myColor) {
         super(myColor.getName());
         this.myColor = myColor;
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         cardlayout.show(cardHolderPanel, myColor.getName());
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("CardExample");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new CardExample());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM