Java – how to replace a JPanel with another JPanel in the same location?

I have a JFrame of about four frames After a given action, I want to replace one panel with another in the same position The method I switch them looks like this (this object is a class that extends JFrame):

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

The original creation of the window is as follows:

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

Solution

You ask:

Very easy: use CardLayout because this tool is built for this situation

If you have the following constants:

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

And added your JPanel

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

cardlayout.show(mainPanel,INTRO_PANEL);

Then you can exchange jpanels in the following ways:

cardlayout.show(mainPanel,QUESTION_PANEL);

Hypothetical query_ Panel is the string constant used when you add the problem panel to the main panel, and if the mainpanel uses CardLayout, all the contents required for the exchange will be displayed

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.@R_419_842@(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>