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

試著忘記壹切 提交于 2019-12-04 14:49:12

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!