Java:单帧与多帧

发布于 2025-01-07 07:48:55 字数 462 浏览 0 评论 0原文

想想经典的安装过程,其中有一个“下一步”按钮,当您单击它时,窗口的内容会发生变化。为了表示这种情况,我想到了两种可能的解决方案:

- 当单击“下一步”时,销毁当前的 JFrame 并创建一个新的 JFrame,也许传递给他的构造函数有用的信息(例如实际窗口大小、用户在当前帧中插入的内容) , ...)

-当单击“下一步”时,从当前 JFrame 中删除所有组件,并根据需要添加新组件

第一个解决方案在 OO 编程方面看起来更好,因为我可以为不同的框架保留单独的类,并且可以避免庞大的方法清空框架并重新填充 它。然而,第一个解决方案听起来有点“脏”,我应该将很多参数传递给新框架。为了代表这种情况,我会选择第二种解决方案。

现在考虑一个带有“选项”组件的菜单:在这种情况下,我将在单击“选项”时创建一个新的 JFrame,以便我可以用选项项填充它。这是正确的解决方案吗?有没有办法让我始终知道哪一个是最佳解决方案?有没有我没有想到的解决方案?

Think about the classic installation process, where you have a "next" button and when you click it the content of the window changes. To represent this situation I thought of two possible solutions:

-when "next" is clicked destroy the current JFrame and create a new JFrame, maybe passing to his constructor useful information (e.g. actual window size, content inserted by the user in the current frame, ...)

-when "next" is clicked remove all the components from the current JFrame and add new components as needed

The first solution looks way better about OOprogramming, because I can keep separate classes for different frames and I can avoid huge methods that empty the frame and repopulate it. However the first solution sounds a bit "dirty" and I should pass lots of parameters to the new frame. To represent this situation I would choose the second solution.

Now think about a menu with an "option" component: in this situation I would create a new JFrame when "option" is clicked, so that I can populate it with option items. Is this a correct solution? Is there a way I can always know which one is the best solution? Are there any solutions I didn't think about?

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

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

发布评论

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

评论(2

泡沫很甜 2025-01-14 07:48:55

销毁主 JFrame 是愚蠢的——更不用说给用户带来不和谐的感觉了。只需使用一个 JFrame 并更改其内容即可。

要实现安装程序向导,请使用单个 JFrame,其中顶部包含一个较大的 JPanel,另一个较小的 JFrame 包含“下一步”、“后退”、“取消”按钮。底部。当按下 NextBack 按钮时,您将替换大的 JPanel。您可以有许多不同的 JPanel 子类,向导的每个“页面”都有一个子类。

有一个名为 CardLayoutLayoutManager,它非常适合实现此场景 - 它管理组件的“堆栈”,并且一次仅显示其中一个组件。在 JFrame 中使用 BorderLayout。在中心位置放置一个带有 CardLayoutJPanel。然后将向导的各个页面添加到该 JPanel 中,以便 CardLayout 可以管理它们。

Destroying the main JFrame would be silly -- not to mention jarring for the user. Just use a single JFrame and change its contents.

To implement an installer wizard, use a single JFrame containing one large JPanel on top and a smaller one containing the "Next", "Back", "Cancel" buttons along the bottom. When the Next or Back buttons are pressed, you replace the large JPanel. You can have many different JPanel subclasses, one for each "page" of the wizard.

There's a LayoutManager called CardLayout which is ideal for implementing this scenario -- it manages a "stack" of components, and only shows one of those components at a time. Use a BorderLayout in the JFrame. Into the center position put a JPanel with a CardLayout. Then add the individual pages of the wizard to that JPanel, so the CardLayout can manage them.

滥情空心 2025-01-14 07:48:55

CardLayout 非常适合于此。您只需在按下“Next”按钮时交换 JPanel 内容即可。

The CardLayout is well suited for this. You just swapout the JPanel contents when the "Next" button is pressed.

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