Java:单帧与多帧 [英] Java: single frame vs multiple frames

查看:456
本文介绍了Java:单帧与多帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

- 单击下一步会破坏当前的JFrame并创建一个新的JFrame,可能会传递给他的构造函数有用的信息(例如,实际窗口大小,用户在当前帧中插入的内容,...)

-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, ...)

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

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

第一个解决方案看起来更好的OOprogramming,因为我可以为不同的帧保留单独的类,我可以避免使用清空帧的大型方法重新填充它。然而,第一个解决方案听起来有点脏,我应该将大量参数传递给新帧。为了表示这种情况,我会选择第二种解决方案。

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.

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

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?

推荐答案

销毁主 JFrame 会很傻 - 更不用说为用户提供刺激了。只需使用一个 JFrame 并更改其内容。

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

要实现安装程序向导,请使用单个 JFrame 在顶部包含一个大的 JPanel ,另一个包含Next,Back,Cancel按钮的小按钮底部。当按下下一步后退按钮时,您将替换大 JPanel 。你可以有许多不同的 JPanel 子类,每个页面对应一个。

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.

有一个 LayoutManager 名为 CardLayout ,这是实现这种情况的理想选择 - 它管理组件的堆栈,并且只显示其中一个那些组件一次。在 JFrame 中使用 BorderLayout 。进入中心位置,使用 CardLayout 放置 JPanel 。然后将向导的各个页面添加到 JPanel ,这样 CardLayout 就可以管理它们。

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.

这篇关于Java:单帧与多帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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