交换框架中的面板

发布于 2024-10-10 03:18:11 字数 530 浏览 0 评论 0原文

所以我有一个框架和2个面板,最初面板A显示在框架中。我已经设置好了,所以当在面板中单击按钮时,它会回调框架并运行这段代码。 (“splash”是最初显示的面板,“game”是我想在按下按钮后显示的面板)

public void startGame()
{
    System.out.println("starting game");
    remove(splash);

    gameThread = new Thread(game, "game thread");
    gameThread.start();
    add(game);

    /*
    this.setSize(0,0);
    this.setSize(450, 450);
    */
}

无论如何,这似乎可以很好地删除一个面板,但在调整窗口大小之前不会显示第二个面板。评论部分似乎解决了这个问题,但我忍不住认为有更好的方法来做到这一点。我浏览了 API,没有发现任何有用的东西。

如果有人知道更好的方法,或者遇到过这个问题。请分享你的知识,这个让我难住了。

So i have a frame and 2 panels, originally panel A is displayed in the frame. i have got it set up so when a button is clicked in the panel it makes a call back to the frame and runs this bit of code. ("splash" is the panel that is originally displayed and "game" is the panel i want to show after the button press)

public void startGame()
{
    System.out.println("starting game");
    remove(splash);

    gameThread = new Thread(game, "game thread");
    gameThread.start();
    add(game);

    /*
    this.setSize(0,0);
    this.setSize(450, 450);
    */
}

anyways this seems to remove the one panel fine but wont display the second panel until you resize the window. the commented section seems to solve this problem but i cant help think there is a better way to do this. i have surfed the API and haven't found anything helpful.

if anyone knows a better way, or has run into this problem. please share your knowledge, this one has me stumped.

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

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

发布评论

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

评论(4

ゃ懵逼小萝莉 2024-10-17 03:18:11

您应该使用 CardLayout 来完成这种事情,而不是删除并添加面板。

You should use a CardLayout for this kind of thing instead of removing and adding panels.

夕嗳→ 2024-10-17 03:18:11

容纳面板的容器需要失效、验证和重新喷漆(全部在 EDT 上)。例如,如果框架是支架,那么

frame.invalidate();
frame.validate();
frame.repaint();

这当然是在添加目标面板之后。

The container holding the panels needs to be invalidated, validated and repainted (all on EDT). For example, if the frame is the holder then

frame.invalidate();
frame.validate();
frame.repaint();

This is of course after the target panel has been added.

天气好吗我好吗 2024-10-17 03:18:11

正如 Michael 指出的那样,CardLayout 是一个很好的方法。

您正在寻找的具体功能是 Container.validate()。它将“导致容器再次布置其子组件。当容器的子组件被修改时......在容器显示之后,应该调用它。”

As Michael pointed out CardLayout is a good way to go.

The specific functionality you are looking for is Container.validate(). It will "cause the container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified ... after the container has been displayed."

裸钻 2024-10-17 03:18:11

现在,我真的不记得自从我遇到了与您相同的问题以来,但是,您是否尝试使用:

this.setVisible(true);

这可能有用

Right now, i really don't remember how since i haded the same problem you have, but, have you try to use:

this.setVisible(true);

That might work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文