Java:单帧与多帧
想想经典的安装过程,其中有一个“下一步”按钮,当您单击它时,窗口的内容会发生变化。为了表示这种情况,我想到了两种可能的解决方案:
- 当单击“下一步”时,销毁当前的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
销毁主
JFrame
是愚蠢的——更不用说给用户带来不和谐的感觉了。只需使用一个JFrame
并更改其内容即可。要实现安装程序向导,请使用单个
JFrame
,其中顶部包含一个较大的JPanel
,另一个较小的 JFrame 包含“下一步”、“后退”、“取消”按钮。底部。当按下Next
或Back
按钮时,您将替换大的JPanel
。您可以有许多不同的JPanel
子类,向导的每个“页面”都有一个子类。有一个名为
CardLayout
的LayoutManager
,它非常适合实现此场景 - 它管理组件的“堆栈”,并且一次仅显示其中一个组件。在JFrame
中使用BorderLayout
。在中心位置放置一个带有CardLayout
的JPanel
。然后将向导的各个页面添加到该JPanel
中,以便CardLayout
可以管理它们。Destroying the main
JFrame
would be silly -- not to mention jarring for the user. Just use a singleJFrame
and change its contents.To implement an installer wizard, use a single
JFrame
containing one largeJPanel
on top and a smaller one containing the "Next", "Back", "Cancel" buttons along the bottom. When theNext
orBack
buttons are pressed, you replace the largeJPanel
. You can have many differentJPanel
subclasses, one for each "page" of the wizard.There's a
LayoutManager
calledCardLayout
which is ideal for implementing this scenario -- it manages a "stack" of components, and only shows one of those components at a time. Use aBorderLayout
in theJFrame
. Into the center position put aJPanel
with aCardLayout
. Then add the individual pages of the wizard to thatJPanel
, so theCardLayout
can manage them.CardLayout 非常适合于此。您只需在按下“Next”按钮时交换 JPanel 内容即可。
The CardLayout is well suited for this. You just swapout the JPanel contents when the "Next" button is pressed.