使用 2 个或更多框架

发布于 2024-12-12 03:02:55 字数 198 浏览 0 评论 0原文

我的 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 call
classname.frameName.setVisible(false);

is this the correct solution?

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

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

发布评论

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

评论(4

铃予 2024-12-19 03:02:55

除了具有多个 JDialog 实例的 CardLayoutJFrame 的(优秀)建议之外,这里还有一些其他策略可以单独或组合使用,将各种内容窗格折叠到一个框架中。

  1. JDesktopPane/JInternalFames (
  2. JSplitPane嗯。) 。
  3. JTabbedPane嗯。) 。
  4. JLayeredPane,如果您有勇气的话 (嗯。)。
  5. JToolBar - 如果需要可浮动(Tut.< /a>)。
  6. 嵌套布局JPanel 的不同约束。

可能还有更多......


当然,正如 Adamski 指出的那样,还有一些进一步的怪癖需要考虑。

如果每个框架都有 JMenuBars 或 JMenus 怎么办?

可能将它们组合为子菜单。

Besides the (excellent) suggestions of a CardLayout or JFrame with multiple JDialog instances, here are some other strategies which might work singly or in combination, to collapse a variety of content panes into a single frame.

  1. JDesktopPane/JInternalFames (Tut.).
  2. JSplitPane (Tut.).
  3. JTabbedPane (Tut.).
  4. JLayeredPane, if you're feeling brave (Tut.).
  5. JToolBar - floatable if needed (Tut.).
  6. Different constraints of a JPanel in a nested layout.

There are probably more..


Of course, as Adamski pointed out, there are some further quirks to consider..

What if each frame has JMenuBars or JMenus?

Possibly combine them as sub-menus.

盗琴音 2024-12-19 03:02:55

看看一个不错的对接框架,例如 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.

此生挚爱伱 2024-12-19 03:02:55

这个设计似乎有缺陷。您应该使用适当的布局管理器,而不是使用多个容器。在这种情况下,我建议使用 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.

未央 2024-12-19 03:02:55

通过静态引用控制框架似乎是一个非常脆弱的解决方案。如果引用为空怎么办?如果调用 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.

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