简体   繁体   English

如何删除旧的JPanel并添加新的JPanel?

[英]How do I remove an old JPanel and add a new one?

I would like to remove an old JPanel from the Window (JFrame) and add a new one. 我想从Window(JFrame)中删除一个旧的JPanel并添加一个新的。 How should I do it? 我该怎么办?

I tried the following: 我尝试了以下方法:

public static void showGUI() {
    JFrame frame = new JFrame("Colored Trails");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
    frame.add(partnerSelectionPanel);
    frame.setSize(600,400);
    frame.setVisible(true);
}

private static void updateGUI(final int i, final JLabel label, final JPanel partnerSelectionPanel) {
    SwingUtilities.invokeLater( 
        new Runnable() {
            public void run() {
                label.setText(i + " seconds left.");
            }
            partnerSelectionPanel.setVisible(false); \\ <------------
        }
    );
}

My code updates the "old" JPanel and then it makes the whole JPanel invisible, but it does not work. 我的代码更新了“旧” JPanel,然后使整个JPanel不可见,但它不起作用。 The compiler complains about the line indicated with <------------ . 编译器抱怨<------------指示的行。 It writes: <identifier> expected, illegal start of type . 它写道: <identifier> expected, illegal start of type

ADDED: 添加:

I have managed to do what I needed and I did it in the following way: 我已经设法完成了我需要做的事情,并且通过以下方式做到了:

public static void showGUI() {
    frame = new JFrame("Colored Trails");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
    frame.add(partnerSelectionPanel);
    //frame.add(selectionFinishedPanel);
    frame.setSize(600,400);
    frame.setVisible(true);
}

public static Thread counter = new Thread() {
    public void run() {
        for (int i=4; i>0; i=i-1) {
            updateGUI(i,label);
            try {Thread.sleep(1000);} catch(InterruptedException e) {};
        }
        partnerSelectionPanel.setVisible(false);
        frame.add(selectionFinishedPanel);
    }
};

It works but it does not look to me like a safe solution for the following reasons: 它可以工作,但由于以下原因,在我看来它不是一个安全的解决方案:

  1. I change and add elements to the JFrame from another thread. 我从另一个线程更改元素并将其添加到JFrame。
  2. I add a new JPanel to a JFrame, after I have already "packed" the JFrame and made it visible. 在我已经“打包” JFrame并且使其可见之后,我向JFrame添加了新的JPanel。

Should I be doing that? 我应该这样做吗?

setVisible(false), even in the correct place, will not actually remove the panel from the container. setVisible(false),即使在正确的位置,也实际上不会从容器中移除面板。 If you want to replace the panel do this: 如果要更换面板,请执行以下操作:

frame.getContentPane().remove(partnerSelectionPanel);
frame.add(new JPanel());
frame.getContentPane().invalidate();
frame.getContentPane().validate();

Note that frame.getContentPane().add(Component) is the same as frame.add(Component) - the components are actually contained within the content pane. 请注意,frame.getContentPane()。add(Component)与frame.add(Component)相同-组件实际上包含在内容窗格中。

Don't forget or overlook the approach of using the Layout, namely the CardLayout as the Frames Layout, to allow this type of behavior (This is a good strategy for a "Wizard" for example). 不要忘记或忽略使用布局(即CardLayout作为框架布局)来允许这种类型的行为的方法(例如,这对于“向导”来说是个好策略)。 One advantage to this is it doesn't cause any weird flash or draw effects as that is what this Layout is meant to do--Allow a panels to be swapped out, assuming they have exclusive "real estate" or can share the same areas (ie "Wizard" like behavior.) 这样做的一个优点是,它不会引起任何怪异的闪光或绘制效果,这就是此Layout的目的-允许面板交换出来,前提是它们具有专有的“房地产”或可以共享相同的区域(即“向导”之类的行为。)

您可以使用

  Frame.setContentPane(jPanel);
partnerSelectionPanel.setVisible(false); \\ <------------

This line is actualy out the method run. 这行实际上是方法运行的结果。

You probably want something like this: 您可能想要这样的东西:

public void run() {
   label.setText(i + " seconds left.");
   try {
      Thread.sleep (i * 1000);
   } catch (InterruptedException e) {
      handleException (e);
   }
   partnerSelectionPanel.setVisible(false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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