Working with 2 or more frames

主宰稳场 提交于 2019-11-28 08:49:18
Andrew Thompson

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.

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.

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!