使用 2 个或更多框架
我的 java swing 应用程序中有大约 3 个框架。如何处理这些框架的正确方法是什么?我的意思是某种模式或其他东西。现在我总是有一个代表框架的类和一个代表该框架中主要面板的类。现在我已经将框架定义为静态变量,当我想隐藏它们时我调用 classname.frameName.setVisible(false);
这是正确的解决方案吗?
I have about 3 frames in my java swing application. What it the correct way how to handle with these frames? I mean some pattern or something else. Now I have always one class which represent frame and one class for panel which is main in this frame. Now I have defined frames as static variable and when I wanna hide them I callclassname.frameName.setVisible(false);
is this the correct solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除了具有多个
JDialog
实例的CardLayout
或JFrame
的(优秀)建议之外,这里还有一些其他策略可以单独或组合使用,将各种内容窗格折叠到一个框架中。JDesktopPane
/JInternalFames
(JSplitPane
(嗯。) 。JTabbedPane
(嗯。) 。JLayeredPane
,如果您有勇气的话 (嗯。)。JToolBar
- 如果需要可浮动(Tut.< /a>)。JPanel
的不同约束。可能还有更多......
当然,正如 Adamski 指出的那样,还有一些进一步的怪癖需要考虑。
可能将它们组合为子菜单。
Besides the (excellent) suggestions of a
CardLayout
orJFrame
with multipleJDialog
instances, here are some other strategies which might work singly or in combination, to collapse a variety of content panes into a single frame.JDesktopPane
/JInternalFames
(Tut.).JSplitPane
(Tut.).JTabbedPane
(Tut.).JLayeredPane
, if you're feeling brave (Tut.).JToolBar
- floatable if needed (Tut.).JPanel
in a nested layout.There are probably more..
Of course, as Adamski pointed out, there are some further quirks to consider..
Possibly combine them as sub-menus.
看看一个不错的对接框架,例如 MyDoggy。这允许您在单个
JFrame
中显示所有三个组件,但非常灵活,因为您可以并排查看数据、调整大小和最大化组件。Take a look at a decent docking framework such as MyDoggy. This allows you to display all three components in a single
JFrame
, but is very flexible in that you can view the data side-by-side, resize and maximise components.这个设计似乎有缺陷。您应该使用适当的布局管理器,而不是使用多个容器。在这种情况下,我建议使用
CardLayout
。这样,您将拥有一个具有多个可交换视图的单个容器。This design seems flawed. Instead of having multiple containers, you should use an appropriate layout manager. In this case, I recommend using
CardLayout
. This way, you would have a single container with multiple exchangeable views.通过静态引用控制框架似乎是一个非常脆弱的解决方案。如果引用为空怎么办?如果调用
setVisible()
时框架未处于完成状态怎么办?最好将此逻辑分离到一个单独的类中,然后让框架将自己注册到其中,或者预先构建所有内容。
Controlling frames through static references seems to be a very fragile solution. What if the reference is null? What if the frame isn't in a completed state when
setVisible()
is called on it?It would probably be a better idea to separate this logic out into a separate class and either have the frames register themselves to it , or construct everything up front.