如何在同一位置用另一个JPanel替换一个JPanel? [英] How can I replace one JPanel with another JPanel in the same position?

查看:1053
本文介绍了如何在同一位置用另一个JPanel替换一个JPanel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大约四帧的JFrame。在给定动作之后,我想在同一位置将一个面板替换为另一个面板。我切换它们的方法看起来像这样(这个对象是一个扩展 JFrame 的类):

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);


推荐答案

你问:


如何在同一位置用另一个JPanel替换一个JPanel?

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

非常容易:使用 CardLayout ,因为此工具是为只是这种情况。

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

如果你有以下常数:

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

并添加你的JPanel就像这样

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);

然后你可以用以下方式交换JPanel:

Then you could swap JPanels with:

cardLayout.show(mainPanel, QUESTION_PANEL);

将显示交换所需的全部内容,假设QUESTION_PANEL是使用的String常量当你将questionPanel添加到mainPanel,并且mainPanel使用CardLayout时。

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.

例如:

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();
         }
      });
   }
}

这篇关于如何在同一位置用另一个JPanel替换一个JPanel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆