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

发布于 2024-08-25 14:02:46 字数 1685 浏览 3 评论 0原文

我想从窗口(JFrame)中删除旧的 JPanel 并添加一个新的。我该怎么做呢?

我尝试了以下操作:

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

我的代码更新了“旧”JPanel,然后它使整个 JPanel 不可见,但它不起作用。编译器抱怨用 <------------ 指示的行。它写道:预期的、非法的类型开始

添加:

我已经成功地完成了我需要的操作,并通过以下方式完成了它:

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

它有效,但在我看来它不像是一个安全的解决方案,原因如下:

  1. 我更改并添加了元素来自另一个线程的 JFrame。
  2. 在我已经“打包”JFrame 并使其可见之后,我将一个新的 JPanel 添加到 JFrame 中。

我应该这样做吗?

I would like to remove an old JPanel from the Window (JFrame) and add a new one. 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. The compiler complains about the line indicated with <------------. It writes: <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.
  2. I add a new JPanel to a JFrame, after I have already "packed" the JFrame and made it visible.

Should I be doing that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花落人断肠 2024-09-01 14:02:46

setVisible(false),即使在正确的位置,实际上也不会从容器中删除面板。如果要替换面板,请执行以下操作:

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

请注意,frame.getContentPane().add(Component) 与frame.add(Component) 相同 - 组件实际上包含在内容窗格中。

setVisible(false), even in the correct place, will not actually remove the panel from the container. 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.

雨的味道风的声音 2024-09-01 14:02:46

不要忘记或忽视使用布局的方法,即 CardLayout 作为框架布局,以允许这种类型的行为(例如,对于“向导”来说这是一个很好的策略)。这样做的一个优点是它不会导致任何奇怪的闪光或绘制效果,因为这就是此布局的目的 - 允许面板被交换,假设它们具有专有的“不动产”或可以共享相同的区域(即“向导”般的行为。)

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). 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 (i.e. "Wizard" like behavior.)

爱已欠费 2024-09-01 14:02:46

您可以使用

  Frame.setContentPane(jPanel);

You can use

  Frame.setContentPane(jPanel);
樱花细雨 2024-09-01 14:02:46
partnerSelectionPanel.setVisible(false); \\ <------------

该行实际上是方法运行的结果。

你可能想要这样的东西:

public void run() {
   label.setText(i + " seconds left.");
   try {
      Thread.sleep (i * 1000);
   } catch (InterruptedException e) {
      handleException (e);
   }
   partnerSelectionPanel.setVisible(false);
}
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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文