简体   繁体   中英

How can I replace one JPanel with another JPanel in the same position?

I have a JFrame with about four frames. After a given action I want to replace one panel with another in the same position. My method for switching them looks like this (the this object is a class which extends JFrame ):

 public void switchPanel(){
    mainPanel.remove(introPanel);
    mainPanel.add(questionPanel);
    this.repaint();
 }

The original creation of the window looks like this:

 mainPanel.add(titlePanel);
 mainPanel.add(sidePanel);
 mainPanel.add(introPanel);

You ask:

How can I replace one JPanel with another JPanel in the same position?

Very easily: use a CardLayout as this tool was built for just this situation.

If you had the following constants:

public static final String INTRO_PANEL = "intro panel";
public static final String QUESTION_PANEL = "question panel";

and added your JPanel's like so

mainPanel.setLayout(cardLayout);
mainPanel.add(introPanel, INTRO_PANEL);
mainPanel.add(questionPanel, QUESTION_PANEL);

cardLayout.show(mainPanel, INTRO_PANEL);

Then you could swap JPanels with:

cardLayout.show(mainPanel, QUESTION_PANEL);

would be all that were needed to show the swap, assuming that QUESTION_PANEL is a String constant that was used when you added the questionPanel to the mainPanel, and that the mainPanel uses the CardLayout.

For example:

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.*;

public class SwapPanels extends JPanel {
   public static final String INTRO_PANEL = "intro panel";
   public static final String QUESTION_PANEL = "question panel";
   private static final int PREF_W = 500;
   private static final int PREF_H = 400;
   private CardLayout cardLayout = new CardLayout();
   private JPanel introPanel;
   private JPanel questionPanel;
   private Random random = new Random();

   public SwapPanels() {
      introPanel = createPanel("Introduction");
      questionPanel = createPanel("Question");
      setLayout(cardLayout);
      add(introPanel, INTRO_PANEL);
      add(questionPanel, QUESTION_PANEL);

      int delay = 3 * 1000; // show intro for 3 seconds
      new Timer(delay, new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            cardLayout.show(SwapPanels.this, QUESTION_PANEL);
            ((Timer) e.getSource()).stop();
         }
      }).start();
   }

   private JPanel createPanel(String title) {
      int rgb = random.nextInt();
      Color color = new Color(rgb);
      JPanel panel = new JPanel(new GridBagLayout());
      panel.setBorder(BorderFactory.createLineBorder(color, 60));
      panel.add(new JLabel(title));
      return panel;
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      SwapPanels mainPanel = new SwapPanels();

      JFrame frame = new JFrame("SwapPanels");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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

after the remove method you have to run the validate method that's all. The code will be like this:

public void switchPanel(){
    mainPanel.remove(introPanel);
    mainPanel.add(questionPanel);
    mainPanel.validate();
}

I hope that helps. Salam

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