使用多个jframe:好习惯还是坏习惯?

时间:2021-06-11 00:01:22

I'm developing an application which displays images, and plays sounds from a database. I'm trying to decide whether or not to use a separate JFrame to add images to the database from the GUI.

我正在开发一个应用程序,它可以显示图像,并从数据库中播放声音。我正在尝试决定是否使用一个单独的JFrame从GUI向数据库添加图像。

I'm just wondering whether it is good practice to use multiple JFrame windows?

我想知道使用多个JFrame窗口是否是个好习惯?

9 个解决方案

#1


401  

I'm just wondering whether it is good practice to use multiple JFrames?

我想知道使用多个jframe是否是个好习惯?

Bad (bad, bad) practice.

坏的坏,坏的做法。

  • User unfriendly: The user sees multiple icons in their task bar when expecting to see only one. Plus the side effects of the coding problems..
  • 用户不友好:当用户希望看到一个图标时,会在任务栏中看到多个图标。加上编码问题的副作用。
  • A nightmare to code and maintain:
    • A modal dialog offers the easy opportunity to focus attention on the content of that dialog - choose/fix/cancel this, then proceed. Multiple frames do not.
    • 模态对话框提供了一个将注意力集中在该对话框内容上的简单机会——选择/修复/取消该对话框,然后继续。多个帧不。
    • A dialog (or floating tool-bar) with a parent will come to front when the parent is clicked on - you'd have to implement that in frames if that was the desired behavior.
    • 当父节点被单击时,与父节点的对话框(或浮动工具栏)将出现在前面——如果这是需要的行为,您必须在框架中实现它。
  • 代码和维护的噩梦:模态对话框提供了将注意力集中在对话框内容上的简单机会——选择/修复/取消它,然后继续。多个帧不。当父节点被单击时,与父节点的对话框(或浮动工具栏)将出现在前面——如果这是需要的行为,您必须在框架中实现它。

There are any number of ways of displaying many elements in one GUI, e.g.:

在一个GUI中显示许多元素的方法有很多,例如:

  • CardLayout (short demo.). Good for:
    1. Showing wizard like dialogs.
    2. 显示向导对话框。
    3. Displaying list, tree etc. selections for items that have an associated component.
    4. 显示具有关联组件的项目的列表、树等选项。
    5. Flipping between no component and visible component.
    6. 在无组件和可见组件之间切换。
  • CardLayout(演示)。好:显示像对话框一样的向导。显示具有关联组件的项目的列表、树等选项。在无组件和可见组件之间切换。
  • JInternalFrame/JDesktopPane typically used for an MDI.
  • JInternalFrame/JDesktopPane通常用于MDI。
  • JTabbedPane for groups of components.
  • 用于组件组的JTabbedPane。
  • JSplitPane A way to display two components of which the importance between one or the other (the size) varies according to what the user is doing.
  • JSplitPane显示两个组件的方式,其中一个组件或另一个组件(大小)的重要性随用户的操作而变化。
  • JLayeredPane far many well ..layered components.
  • JLayeredPane许多很多。分层的组件。
  • JToolBar typically contains groups of actions or controls. Can be dragged around the GUI, or off it entirely according to user need. As mentioned above, will minimize/restore according to the parent doing so.
  • JToolBar通常包含一组操作或控件。可以在GUI周围拖拽,也可以完全根据用户需要从GUI中拖出。如上所述,将根据父进程最小化/恢复。
  • As items in a JList (simple example below).
  • 作为JList中的项(下面的简单示例)。
  • As nodes in a JTree.
  • 作为JTree中的节点。
  • Nested layouts.
    使用多个jframe:好习惯还是坏习惯?
  • 嵌套布局。

But if those strategies do not work for a particular use-case, try the following. Establish a single main JFrame, then have JDialog or JOptionPane instances appear for the rest of the free-floating elements, using the frame as the parent for the dialogs.

但是,如果这些策略对于特定的用例不起作用,请尝试以下方法。建立一个主JFrame,然后使用JDialog或JOptionPane实例来显示其余的*浮动元素,使用框架作为对话框的父对象。

Many images

In this case where the multiple elements are images, it would be better to use either of the following instead:

在这种情况下,多个元素都是图像,最好使用以下两种方法:

  1. A single JLabel (centered in a scroll pane) to display whichever image the user is interested in at that moment. As seen in ImageViewer.
    使用多个jframe:好习惯还是坏习惯?
  2. 一个JLabel(居中于滚动窗格中)来显示用户当时感兴趣的任何图像。见imageview。
  3. A single row JList. As seen in this answer. The 'single row' part of that only works if they are all the same dimensions. Alternately, if you are prepared to scale the images on the fly, and they are all the same aspect ratio (e.g. 4:3 or 16:9).
  4. JList单个行。正如在这个答案中看到的。“单行”部分只有在它们都是相同的维度时才会起作用。或者,如果你准备在动态缩放图像,它们都是相同的纵横比(比如4:3或16:9)。

使用多个jframe:好习惯还是坏习惯?

#2


184  

The multiple JFrame approach has been something I've implemented since I began programming Swing apps. For the most part, I did it in the beginning because I didn't know any better. However, as I matured in my experience and knowledge as a developer and as began to read and absorb the opinions of so many more experienced Java devs online, I made an attempt to shift away from the multiple JFrame approach (both in current projects and future projects) only to be met with... get this... resistance from my clients! As I began implementing modal dialogs to control "child" windows and JInternalFrames for separate components, my clients began to complain! I was quite surprised, as I was doing what I thought was best-practice! But, as they say, "A happy wife is a happy life." Same goes for your clients. Of course, I am a contractor so my end-users have direct access to me, the developer, which is obviously not a common scenario.

自从我开始编写Swing应用程序以来,我已经实现了多JFrame方法。大部分情况下,我一开始就这么做了,因为我不知道该怎么做。然而,当我成熟经验和知识作为一个开发者,开始阅读和吸收更多的有经验的Java开发者的意见在网上,我试图从多个JFrame转移方法(仅在当前项目和未来的项目)会见了……得到这个……反对我的客户!当我开始实现模态对话框来控制“子”窗口和jinternalframe的独立组件时,我的客户开始抱怨!我很惊讶,因为我正在做我认为是最好的练习!但是,正如他们所说,“幸福的妻子是幸福的生活。”你的客户也是如此。当然,我是一个承包商,所以我的终端用户可以直接访问我,开发人员,这显然不是一个常见的场景。

So, I'm going to explain the benefits of the multiple JFrame approach, as well as myth-bust some of the cons that others have presented.

因此,我将解释多JFrame方法的好处,以及其他方法提出的一些缺点。

  1. Ultimate flexibility in layout - By allowing separate JFrames, you give your end-user the ability to spread out and control what's on his/her screen. The concept feels "open" and non-constricting. You lose this when you go towards one big JFrame and a bunch of JInternalFrames.
  2. 布局的最大灵活性——通过允许独立的jframe,您的最终用户可以扩展和控制屏幕上的内容。这个概念给人的感觉是“开放的”和不受约束的。当你走向一个大的JFrame和一堆jinternalframe时,你就失去了这个。
  3. Works well for very modularized applications - In my case, most of my applications have 3 - 5 big "modules" that really have nothing to do with each other whatsoever. For instance, one module might be a sales dashboard and one might be an accounting dashboard. They don't talk to each other or anything. However, the executive might want to open both and them being separate frames on the taskbar makes his life easier.
  4. 对于非常模块化的应用程序来说,工作很好——在我的例子中,我的大多数应用程序都有3 - 5个大的“模块”,它们之间根本没有任何关系。例如,一个模块可能是一个销售指示板,一个模块可能是一个会计指示板。他们彼此都不说话。然而,高管们可能想要同时打开这两个窗口,并且在任务栏上设置独立的框架可以让他的生活更轻松。
  5. Makes it easy for end-users to reference outside material - Once, I had this situation: My app had a "data viewer," from which you could click "Add New" and it would open a data entry screen. Initially, both were JFrames. However, I wanted the data entry screen to be a JDialog whose parent was the data viewer. I made the change, and immediately I received a call from an end-user who relied heavily on the fact that he could minimize or close the viewer and keep the editor open while he referenced another part of the program (or a website, I don't remember). He's not on a multi-monitor, so he needed the entry dialog to be first and something else to be second, with the data viewer completely hidden. This was impossible with a JDialog and certainly would've been impossible with a JInternalFrame as well. I begrudgingly changed it back to being separate JFrames for his sanity, but it taught me an important lesson.
  6. 让终端用户很容易引用外部材料——有一次,我遇到了这样的情况:我的应用有一个“数据查看器”,你可以点击“添加新”,它就会打开数据输入屏幕。最初,两人都是jframe。但是,我希望数据输入屏幕是一个JDialog,它的父级是data viewer。我做了修改,立即收到了一个终端用户的电话,他非常依赖这样一个事实:他可以最小化或关闭查看器,并保持编辑器打开,同时引用程序的另一部分(或者一个网站,我记不清了)。他不在一个多显示器上,所以他需要先进入对话框,然后再做其他事情,数据查看器完全隐藏。这对于JDialog来说是不可能的,对于JInternalFrame来说也是不可能的。我勉强地把它改成了单独的jframe,让他保持清醒,但这给了我一个重要的教训。
  7. Myth: Hard to code - This is not true in my experience. I don't see why it would be any easier to create a JInternalFrame than a JFrame. In fact, in my experience, JInternalFrames offer much less flexibility. I have developed a systematic way of handling the opening & closing of JFrames in my apps that really works well. I control the frame almost completely from within the frame's code itself; the creation of the new frame, SwingWorkers that control the retrieval of data on background threads and the GUI code on EDT, restoring/bringing to front the frame if the user tries to open it twice, etc. All you need to open my JFrames is call a public static method open() and the open method, combined with a windowClosing() event handles the rest (is the frame already open? is it not open, but loading? etc.) I made this approach a template so it's not difficult to implement for each frame.
  8. 神话:难以编码——在我的经验中不是这样的。我不明白为什么创建一个JInternalFrame要比创建JFrame更容易。事实上,根据我的经验,jinternalframe提供的灵活性要小得多。我开发了一种系统的方法来处理我的应用程序中jframe的打开和关闭,这种方法非常有效。我几乎完全从帧的代码中控制帧;新框架的创建,SwingWorkers控制后台线程数据的检索和GUI代码,恢复/把前面的框架,如果用户试图打开它两次,等。所有你需要打开我的jframe是一个公共静态方法的调用open()和开放的方法,结合windowClosing()事件处理其余(框架已经开着吗?它不是开着的,而是装着的?我将这种方法作为一个模板,因此为每个框架实现起来并不困难。
  9. Myth/Unproven: Resource Heavy - I'd like to see some facts behind this speculative statement. Although, perhaps, you could say a JFrame needs more space than a JInternalFrame, even if you open up 100 JFrames, how many more resources would you really be consuming? If your concern is memory leaks because of resources: calling dispose() frees all resources used by the frame for garbage collection (and, again I say, a JInternalFrame should invoke exactly the same concern).
  10. 神话/未经证实的:资源重-我想看看这一推测性声明背后的一些事实。虽然,您可能会说JFrame比JInternalFrame需要更多的空间,但即使您打开了100个JFrame,您实际要消耗多少资源?如果您关心的是由于资源而导致的内存泄漏:调用dispose()释放框架用于垃圾收集的所有资源(而且,我再说一遍,JInternalFrame应该调用完全相同的关注点)。

I've written a lot and I feel like I could write more. Anyways, I hope I don't get down-voted simply because it's an unpopular opinion. The question is clearly a valuable one and I hope I've provided a valuable answer, even if it isn't the common opinion.

我写了很多,我觉得我可以写更多。无论如何,我希望我不会因为这是一个不受欢迎的观点而被否决。这个问题显然是一个有价值的问题,我希望我已经给出了一个有价值的答案,即使它不是普遍的观点。

A great example of multiple frames/single document per frame (SDI) vs single frame/multiple documents per frame (MDI) is Microsoft Excel. Some of MDI benefits:

Microsoft Excel是一个很好的例子,说明每帧有多帧/单个文档,而每帧有多帧/多个文档。MDI的一些好处:

  • it is possible to have a few windows in non rectangular shape - so they don't hide desktop or other window from another process (e.g. web browser)
  • 可以有一些非矩形的窗口,这样它们就不会将桌面或其他窗口隐藏到另一个进程中(例如web浏览器)
  • it is possible to open a window from another process over one Excel window while writing in second Excel window - with MDI, trying to write in one of internal windows will give focus to the entire Excel window, hence hiding window from another process
  • 在第二个Excel窗口中写东西时,可以从另一个进程中打开一个窗口——使用MDI,尝试在一个内部窗口中写东西会使整个Excel窗口集中,从而将窗口隐藏到另一个进程中
  • it is possible to have different documents on different screens, which is especially useful when screens do not have the same resolution
  • 在不同的屏幕上有不同的文档是可能的,这在屏幕没有相同的分辨率时特别有用

SDI (Single-Document Interface, i.e., every window can only have a single document):

SDI(单文档界面,即。,每个窗口只能有一个文档):

使用多个jframe:好习惯还是坏习惯?

MDI (Multiple-Document Interface, i.e., every window can have multiple documents):

MDI(多文档界面,即。,每个窗口都可以有多个文档):

使用多个jframe:好习惯还是坏习惯?

#3


45  

I'd like to counter the "not user friendly" argument with an example that I have just been involved with.

我想用一个我刚刚接触过的例子来反驳“不友好用户”的论点。

In our application we have a main window where the users run various 'programs' as separate tabs. As much as possible we have tried to keep our application to this single window.

在我们的应用程序中,我们有一个主窗口,用户在其中作为独立的选项卡运行各种“程序”。我们尽可能地将应用程序保持在这个窗口中。

One of the 'programs' they run presents a list of reports that have been generated by the system, and the user can click on an icon on each line to pop open a report viewer dialog. This viewer is showing the equivalent of the portrait/landscape A4 page(s) of the report, so the users like this window to be quite big, almost filling their screens.

他们运行的一个“程序”显示了一个由系统生成的报告列表,用户可以点击每行上的图标来打开报表查看器对话框。这个查看器显示的是与报告的纵向/横向A4页面相当的页面,所以用户喜欢这个窗口很大,几乎占据了他们的屏幕。

A few months ago we started getting requests from our customers to make these report viewer windows modeless, so that they could have multiple reports open at the same time.

几个月前,我们开始收到客户的请求,让这些报表查看器的windows模型,这样他们可以同时打开多个报告。

For some time I resisted this request as I did not think this was a good solution. However, my mind was changed when I found out how the users were getting around this 'deficiency' of our system.

有一段时间我拒绝了这个请求,因为我认为这不是一个好的解决方案。然而,当我发现用户是如何解决我们系统的“缺陷”时,我的想法改变了。

They were opening a viewer, using the 'Save As' facility to save the report as a PDF to a specific directory, using Acrobat Reader to open the PDF file, and then they would do the same with the next report. They would have multiple Acrobat Readers running with the various report outputs that they wanted to look at.

他们打开了一个查看器,使用“Save As”工具将报告保存为PDF格式,将其保存到特定的目录中,使用Acrobat Reader打开PDF文件,然后他们将对下一个报告进行同样的操作。它们将有多个Acrobat阅读器运行它们想要查看的各种报表输出。

So I relented and made the viewer modeless. This means that each viewer has a task-bar icon.

所以我让步了,让观众没有模型。这意味着每个查看器都有一个任务栏图标。

When the latest version was released to them last week, the overwhelming response from them is that they LOVE it. It's been one of our most popular recent enhancements to the system.

当最新版本上周发布给他们时,他们的强烈反应是他们喜欢它。这是我们最近对系统最流行的改进之一。

So you go ahead and tell your users that what they want is bad, but ultimately it won't do you any favours.

所以你继续告诉你的用户他们想要的是坏的,但最终它不会给你任何好处。

SOME NOTES:

一些注意事项:

  • It seems to be best practice to use JDialog's for these modeless windows
  • 对于这些无模型窗口使用JDialog似乎是最佳实践
  • Use the constructors that use the new ModalityType rather than the boolean modal argument. This is what gives these dialogs the task-bar icon.
  • 使用使用新ModalityType而不是布尔模态参数的构造函数。这就是任务栏图标的作用。
  • For modeless dialogs, pass a null parent to the constructor, but locate them relative to their 'parent' window.
  • 对于非模态对话框,将null父类传递给构造函数,但要相对于“父类”窗口定位它们。
  • Version 6 of Java on Windows has a bug which means that your main window can become 'always on top' without you telling it. Upgrade to version 7 to fix this
  • Windows上的Java版本6有一个错误,这意味着你的主窗口可以在你不告诉它的情况下“总是在顶部”。升级到版本7来解决这个问题。

#4


16  

Make an jInternalFrame into main frame and make it invisible. Then you can use it for further events.

将jInternalFrame设置为主框架并使其不可见。然后你可以用它来做进一步的事情。

jInternalFrame.setSize(300,150);
jInternalFrame.setVisible(true);

#5


16  

It's been a while since the last time i touch swing but in general is a bad practice to do this. Some of the main disadvantages that comes to mind:

自从上次我接触swing以来已经有一段时间了,但总的来说,这样做是不好的。我想到了一些主要的缺点:

  • It's more expensive: you will have to allocate way more resources to draw a JFrame that other kind of window container, such as Dialog or JInternalFrame.

    它更昂贵:您将不得不分配更多的资源来绘制其他类型窗口容器的JFrame,比如Dialog或JInternalFrame。

  • Not user friendly: It is not easy to navigate into a bunch of JFrame stuck together, it will look like your application is a set of applications inconsistent and poorly design.

    不友好的用户:导航到一堆JFrame中并不容易,看起来您的应用程序是一组不一致和设计糟糕的应用程序。

  • It's easy to use JInternalFrame This is kind of retorical, now it's way easier and other people smarter ( or with more spare time) than us have already think through the Desktop and JInternalFrame pattern, so I would recommend to use it.

    使用JInternalFrame很容易,这是一种反驳,现在它比我们已经考虑过桌面和JInternalFrame模式更容易和其他人更聪明(或有更多的空闲时间),所以我建议使用它。

#6


9  

Bad practice definitely. One reason is that it is not very 'user-friendly' for the fact that every JFrame shows a new taskbar icon. Controlling multiple JFrames will have you ripping your hair out.

实践肯定不好。一个原因是它不是很“友好”,因为每个JFrame都显示一个新的任务栏图标。控制多个jframe会让你抓狂。

Personally, I would use ONE JFrame for your kind of application. Methods of displaying multiple things is up to you, there are many. Canvases, JInternalFrame, CardLayout, even JPanels possibly.

就我个人而言,我将为您的应用程序使用一个JFrame。显示多个东西的方法取决于你,有很多。画布、JInternalFrame、CardLayout,甚至可能是jpanel。

Multiple JFrame objects = Pain, trouble, and problems.

多个JFrame对象=痛苦、麻烦和问题。

#7


6  

I think using multiple Jframes is not a good idea.

我认为使用多个jframe不是一个好主意。

Instead we can use JPanels more than one or more JPanel in the same JFrame.

相反,我们可以在同一个JFrame中使用JPanel多于一个或多个JPanel。

Also we can switch between this JPanels. So it gives us freedom to display more than on thing in the JFrame.

我们还可以在jpanel之间切换。它给了我们更多的*来显示JFrame中的东西。

For each JPanel we can design different things and all this JPanel can be displayed on the single JFrameone at a time.

对于每个JPanel,我们可以设计不同的东西,所有的JPanel可以一次显示在单个JFrameone上。

To switch between this JPanels use JMenuBar with JMenuItems for each JPanelor 'JButtonfor eachJPanel`.

要在这个jpanel之间切换,请使用JMenuBar,并为每个JPanelor 'JButtonfor eachJPanel '提供JMenuItems。

More than one JFrame is not a good practice, but there is nothing wrong if we want more than one JFrame.

不止一个JFrame不是一个好的实践,但是如果我们想要一个以上的JFrame就没有问题了。

But its better to change one JFrame for our different needs rather than having multiple JFrames.

但是,根据不同的需要更改一个JFrame比使用多个JFrame要好。

#8


4  

If the frames are going to be the same size, why not create the frame and pass the frame then as a reference to it instead.

如果帧的大小相同,为什么不创建帧并将其作为引用传递给它呢?

When you have passed the frame you can then decide how to populate it. It would be like having a method for calculating the average of a set of figures. Would you create the method over and over again?

当你通过了框架,你就可以决定如何填充它。这就像有一种计算一组数字平均值的方法。你会一次又一次地创建这个方法吗?

#9


3  

It is not a good practice but even though you wish to use it you can use the singleton pattern as its good. I have used the singleton patterns in most of my project its good.

这不是一个好的实践,但是即使您希望使用它,您也可以使用单例模式作为它的优点。我在我的大多数项目中都使用了单例模式。

#1


401  

I'm just wondering whether it is good practice to use multiple JFrames?

我想知道使用多个jframe是否是个好习惯?

Bad (bad, bad) practice.

坏的坏,坏的做法。

  • User unfriendly: The user sees multiple icons in their task bar when expecting to see only one. Plus the side effects of the coding problems..
  • 用户不友好:当用户希望看到一个图标时,会在任务栏中看到多个图标。加上编码问题的副作用。
  • A nightmare to code and maintain:
    • A modal dialog offers the easy opportunity to focus attention on the content of that dialog - choose/fix/cancel this, then proceed. Multiple frames do not.
    • 模态对话框提供了一个将注意力集中在该对话框内容上的简单机会——选择/修复/取消该对话框,然后继续。多个帧不。
    • A dialog (or floating tool-bar) with a parent will come to front when the parent is clicked on - you'd have to implement that in frames if that was the desired behavior.
    • 当父节点被单击时,与父节点的对话框(或浮动工具栏)将出现在前面——如果这是需要的行为,您必须在框架中实现它。
  • 代码和维护的噩梦:模态对话框提供了将注意力集中在对话框内容上的简单机会——选择/修复/取消它,然后继续。多个帧不。当父节点被单击时,与父节点的对话框(或浮动工具栏)将出现在前面——如果这是需要的行为,您必须在框架中实现它。

There are any number of ways of displaying many elements in one GUI, e.g.:

在一个GUI中显示许多元素的方法有很多,例如:

  • CardLayout (short demo.). Good for:
    1. Showing wizard like dialogs.
    2. 显示向导对话框。
    3. Displaying list, tree etc. selections for items that have an associated component.
    4. 显示具有关联组件的项目的列表、树等选项。
    5. Flipping between no component and visible component.
    6. 在无组件和可见组件之间切换。
  • CardLayout(演示)。好:显示像对话框一样的向导。显示具有关联组件的项目的列表、树等选项。在无组件和可见组件之间切换。
  • JInternalFrame/JDesktopPane typically used for an MDI.
  • JInternalFrame/JDesktopPane通常用于MDI。
  • JTabbedPane for groups of components.
  • 用于组件组的JTabbedPane。
  • JSplitPane A way to display two components of which the importance between one or the other (the size) varies according to what the user is doing.
  • JSplitPane显示两个组件的方式,其中一个组件或另一个组件(大小)的重要性随用户的操作而变化。
  • JLayeredPane far many well ..layered components.
  • JLayeredPane许多很多。分层的组件。
  • JToolBar typically contains groups of actions or controls. Can be dragged around the GUI, or off it entirely according to user need. As mentioned above, will minimize/restore according to the parent doing so.
  • JToolBar通常包含一组操作或控件。可以在GUI周围拖拽,也可以完全根据用户需要从GUI中拖出。如上所述,将根据父进程最小化/恢复。
  • As items in a JList (simple example below).
  • 作为JList中的项(下面的简单示例)。
  • As nodes in a JTree.
  • 作为JTree中的节点。
  • Nested layouts.
    使用多个jframe:好习惯还是坏习惯?
  • 嵌套布局。

But if those strategies do not work for a particular use-case, try the following. Establish a single main JFrame, then have JDialog or JOptionPane instances appear for the rest of the free-floating elements, using the frame as the parent for the dialogs.

但是,如果这些策略对于特定的用例不起作用,请尝试以下方法。建立一个主JFrame,然后使用JDialog或JOptionPane实例来显示其余的*浮动元素,使用框架作为对话框的父对象。

Many images

In this case where the multiple elements are images, it would be better to use either of the following instead:

在这种情况下,多个元素都是图像,最好使用以下两种方法:

  1. A single JLabel (centered in a scroll pane) to display whichever image the user is interested in at that moment. As seen in ImageViewer.
    使用多个jframe:好习惯还是坏习惯?
  2. 一个JLabel(居中于滚动窗格中)来显示用户当时感兴趣的任何图像。见imageview。
  3. A single row JList. As seen in this answer. The 'single row' part of that only works if they are all the same dimensions. Alternately, if you are prepared to scale the images on the fly, and they are all the same aspect ratio (e.g. 4:3 or 16:9).
  4. JList单个行。正如在这个答案中看到的。“单行”部分只有在它们都是相同的维度时才会起作用。或者,如果你准备在动态缩放图像,它们都是相同的纵横比(比如4:3或16:9)。

使用多个jframe:好习惯还是坏习惯?

#2


184  

The multiple JFrame approach has been something I've implemented since I began programming Swing apps. For the most part, I did it in the beginning because I didn't know any better. However, as I matured in my experience and knowledge as a developer and as began to read and absorb the opinions of so many more experienced Java devs online, I made an attempt to shift away from the multiple JFrame approach (both in current projects and future projects) only to be met with... get this... resistance from my clients! As I began implementing modal dialogs to control "child" windows and JInternalFrames for separate components, my clients began to complain! I was quite surprised, as I was doing what I thought was best-practice! But, as they say, "A happy wife is a happy life." Same goes for your clients. Of course, I am a contractor so my end-users have direct access to me, the developer, which is obviously not a common scenario.

自从我开始编写Swing应用程序以来,我已经实现了多JFrame方法。大部分情况下,我一开始就这么做了,因为我不知道该怎么做。然而,当我成熟经验和知识作为一个开发者,开始阅读和吸收更多的有经验的Java开发者的意见在网上,我试图从多个JFrame转移方法(仅在当前项目和未来的项目)会见了……得到这个……反对我的客户!当我开始实现模态对话框来控制“子”窗口和jinternalframe的独立组件时,我的客户开始抱怨!我很惊讶,因为我正在做我认为是最好的练习!但是,正如他们所说,“幸福的妻子是幸福的生活。”你的客户也是如此。当然,我是一个承包商,所以我的终端用户可以直接访问我,开发人员,这显然不是一个常见的场景。

So, I'm going to explain the benefits of the multiple JFrame approach, as well as myth-bust some of the cons that others have presented.

因此,我将解释多JFrame方法的好处,以及其他方法提出的一些缺点。

  1. Ultimate flexibility in layout - By allowing separate JFrames, you give your end-user the ability to spread out and control what's on his/her screen. The concept feels "open" and non-constricting. You lose this when you go towards one big JFrame and a bunch of JInternalFrames.
  2. 布局的最大灵活性——通过允许独立的jframe,您的最终用户可以扩展和控制屏幕上的内容。这个概念给人的感觉是“开放的”和不受约束的。当你走向一个大的JFrame和一堆jinternalframe时,你就失去了这个。
  3. Works well for very modularized applications - In my case, most of my applications have 3 - 5 big "modules" that really have nothing to do with each other whatsoever. For instance, one module might be a sales dashboard and one might be an accounting dashboard. They don't talk to each other or anything. However, the executive might want to open both and them being separate frames on the taskbar makes his life easier.
  4. 对于非常模块化的应用程序来说,工作很好——在我的例子中,我的大多数应用程序都有3 - 5个大的“模块”,它们之间根本没有任何关系。例如,一个模块可能是一个销售指示板,一个模块可能是一个会计指示板。他们彼此都不说话。然而,高管们可能想要同时打开这两个窗口,并且在任务栏上设置独立的框架可以让他的生活更轻松。
  5. Makes it easy for end-users to reference outside material - Once, I had this situation: My app had a "data viewer," from which you could click "Add New" and it would open a data entry screen. Initially, both were JFrames. However, I wanted the data entry screen to be a JDialog whose parent was the data viewer. I made the change, and immediately I received a call from an end-user who relied heavily on the fact that he could minimize or close the viewer and keep the editor open while he referenced another part of the program (or a website, I don't remember). He's not on a multi-monitor, so he needed the entry dialog to be first and something else to be second, with the data viewer completely hidden. This was impossible with a JDialog and certainly would've been impossible with a JInternalFrame as well. I begrudgingly changed it back to being separate JFrames for his sanity, but it taught me an important lesson.
  6. 让终端用户很容易引用外部材料——有一次,我遇到了这样的情况:我的应用有一个“数据查看器”,你可以点击“添加新”,它就会打开数据输入屏幕。最初,两人都是jframe。但是,我希望数据输入屏幕是一个JDialog,它的父级是data viewer。我做了修改,立即收到了一个终端用户的电话,他非常依赖这样一个事实:他可以最小化或关闭查看器,并保持编辑器打开,同时引用程序的另一部分(或者一个网站,我记不清了)。他不在一个多显示器上,所以他需要先进入对话框,然后再做其他事情,数据查看器完全隐藏。这对于JDialog来说是不可能的,对于JInternalFrame来说也是不可能的。我勉强地把它改成了单独的jframe,让他保持清醒,但这给了我一个重要的教训。
  7. Myth: Hard to code - This is not true in my experience. I don't see why it would be any easier to create a JInternalFrame than a JFrame. In fact, in my experience, JInternalFrames offer much less flexibility. I have developed a systematic way of handling the opening & closing of JFrames in my apps that really works well. I control the frame almost completely from within the frame's code itself; the creation of the new frame, SwingWorkers that control the retrieval of data on background threads and the GUI code on EDT, restoring/bringing to front the frame if the user tries to open it twice, etc. All you need to open my JFrames is call a public static method open() and the open method, combined with a windowClosing() event handles the rest (is the frame already open? is it not open, but loading? etc.) I made this approach a template so it's not difficult to implement for each frame.
  8. 神话:难以编码——在我的经验中不是这样的。我不明白为什么创建一个JInternalFrame要比创建JFrame更容易。事实上,根据我的经验,jinternalframe提供的灵活性要小得多。我开发了一种系统的方法来处理我的应用程序中jframe的打开和关闭,这种方法非常有效。我几乎完全从帧的代码中控制帧;新框架的创建,SwingWorkers控制后台线程数据的检索和GUI代码,恢复/把前面的框架,如果用户试图打开它两次,等。所有你需要打开我的jframe是一个公共静态方法的调用open()和开放的方法,结合windowClosing()事件处理其余(框架已经开着吗?它不是开着的,而是装着的?我将这种方法作为一个模板,因此为每个框架实现起来并不困难。
  9. Myth/Unproven: Resource Heavy - I'd like to see some facts behind this speculative statement. Although, perhaps, you could say a JFrame needs more space than a JInternalFrame, even if you open up 100 JFrames, how many more resources would you really be consuming? If your concern is memory leaks because of resources: calling dispose() frees all resources used by the frame for garbage collection (and, again I say, a JInternalFrame should invoke exactly the same concern).
  10. 神话/未经证实的:资源重-我想看看这一推测性声明背后的一些事实。虽然,您可能会说JFrame比JInternalFrame需要更多的空间,但即使您打开了100个JFrame,您实际要消耗多少资源?如果您关心的是由于资源而导致的内存泄漏:调用dispose()释放框架用于垃圾收集的所有资源(而且,我再说一遍,JInternalFrame应该调用完全相同的关注点)。

I've written a lot and I feel like I could write more. Anyways, I hope I don't get down-voted simply because it's an unpopular opinion. The question is clearly a valuable one and I hope I've provided a valuable answer, even if it isn't the common opinion.

我写了很多,我觉得我可以写更多。无论如何,我希望我不会因为这是一个不受欢迎的观点而被否决。这个问题显然是一个有价值的问题,我希望我已经给出了一个有价值的答案,即使它不是普遍的观点。

A great example of multiple frames/single document per frame (SDI) vs single frame/multiple documents per frame (MDI) is Microsoft Excel. Some of MDI benefits:

Microsoft Excel是一个很好的例子,说明每帧有多帧/单个文档,而每帧有多帧/多个文档。MDI的一些好处:

  • it is possible to have a few windows in non rectangular shape - so they don't hide desktop or other window from another process (e.g. web browser)
  • 可以有一些非矩形的窗口,这样它们就不会将桌面或其他窗口隐藏到另一个进程中(例如web浏览器)
  • it is possible to open a window from another process over one Excel window while writing in second Excel window - with MDI, trying to write in one of internal windows will give focus to the entire Excel window, hence hiding window from another process
  • 在第二个Excel窗口中写东西时,可以从另一个进程中打开一个窗口——使用MDI,尝试在一个内部窗口中写东西会使整个Excel窗口集中,从而将窗口隐藏到另一个进程中
  • it is possible to have different documents on different screens, which is especially useful when screens do not have the same resolution
  • 在不同的屏幕上有不同的文档是可能的,这在屏幕没有相同的分辨率时特别有用

SDI (Single-Document Interface, i.e., every window can only have a single document):

SDI(单文档界面,即。,每个窗口只能有一个文档):

使用多个jframe:好习惯还是坏习惯?

MDI (Multiple-Document Interface, i.e., every window can have multiple documents):

MDI(多文档界面,即。,每个窗口都可以有多个文档):

使用多个jframe:好习惯还是坏习惯?

#3


45  

I'd like to counter the "not user friendly" argument with an example that I have just been involved with.

我想用一个我刚刚接触过的例子来反驳“不友好用户”的论点。

In our application we have a main window where the users run various 'programs' as separate tabs. As much as possible we have tried to keep our application to this single window.

在我们的应用程序中,我们有一个主窗口,用户在其中作为独立的选项卡运行各种“程序”。我们尽可能地将应用程序保持在这个窗口中。

One of the 'programs' they run presents a list of reports that have been generated by the system, and the user can click on an icon on each line to pop open a report viewer dialog. This viewer is showing the equivalent of the portrait/landscape A4 page(s) of the report, so the users like this window to be quite big, almost filling their screens.

他们运行的一个“程序”显示了一个由系统生成的报告列表,用户可以点击每行上的图标来打开报表查看器对话框。这个查看器显示的是与报告的纵向/横向A4页面相当的页面,所以用户喜欢这个窗口很大,几乎占据了他们的屏幕。

A few months ago we started getting requests from our customers to make these report viewer windows modeless, so that they could have multiple reports open at the same time.

几个月前,我们开始收到客户的请求,让这些报表查看器的windows模型,这样他们可以同时打开多个报告。

For some time I resisted this request as I did not think this was a good solution. However, my mind was changed when I found out how the users were getting around this 'deficiency' of our system.

有一段时间我拒绝了这个请求,因为我认为这不是一个好的解决方案。然而,当我发现用户是如何解决我们系统的“缺陷”时,我的想法改变了。

They were opening a viewer, using the 'Save As' facility to save the report as a PDF to a specific directory, using Acrobat Reader to open the PDF file, and then they would do the same with the next report. They would have multiple Acrobat Readers running with the various report outputs that they wanted to look at.

他们打开了一个查看器,使用“Save As”工具将报告保存为PDF格式,将其保存到特定的目录中,使用Acrobat Reader打开PDF文件,然后他们将对下一个报告进行同样的操作。它们将有多个Acrobat阅读器运行它们想要查看的各种报表输出。

So I relented and made the viewer modeless. This means that each viewer has a task-bar icon.

所以我让步了,让观众没有模型。这意味着每个查看器都有一个任务栏图标。

When the latest version was released to them last week, the overwhelming response from them is that they LOVE it. It's been one of our most popular recent enhancements to the system.

当最新版本上周发布给他们时,他们的强烈反应是他们喜欢它。这是我们最近对系统最流行的改进之一。

So you go ahead and tell your users that what they want is bad, but ultimately it won't do you any favours.

所以你继续告诉你的用户他们想要的是坏的,但最终它不会给你任何好处。

SOME NOTES:

一些注意事项:

  • It seems to be best practice to use JDialog's for these modeless windows
  • 对于这些无模型窗口使用JDialog似乎是最佳实践
  • Use the constructors that use the new ModalityType rather than the boolean modal argument. This is what gives these dialogs the task-bar icon.
  • 使用使用新ModalityType而不是布尔模态参数的构造函数。这就是任务栏图标的作用。
  • For modeless dialogs, pass a null parent to the constructor, but locate them relative to their 'parent' window.
  • 对于非模态对话框,将null父类传递给构造函数,但要相对于“父类”窗口定位它们。
  • Version 6 of Java on Windows has a bug which means that your main window can become 'always on top' without you telling it. Upgrade to version 7 to fix this
  • Windows上的Java版本6有一个错误,这意味着你的主窗口可以在你不告诉它的情况下“总是在顶部”。升级到版本7来解决这个问题。

#4


16  

Make an jInternalFrame into main frame and make it invisible. Then you can use it for further events.

将jInternalFrame设置为主框架并使其不可见。然后你可以用它来做进一步的事情。

jInternalFrame.setSize(300,150);
jInternalFrame.setVisible(true);

#5


16  

It's been a while since the last time i touch swing but in general is a bad practice to do this. Some of the main disadvantages that comes to mind:

自从上次我接触swing以来已经有一段时间了,但总的来说,这样做是不好的。我想到了一些主要的缺点:

  • It's more expensive: you will have to allocate way more resources to draw a JFrame that other kind of window container, such as Dialog or JInternalFrame.

    它更昂贵:您将不得不分配更多的资源来绘制其他类型窗口容器的JFrame,比如Dialog或JInternalFrame。

  • Not user friendly: It is not easy to navigate into a bunch of JFrame stuck together, it will look like your application is a set of applications inconsistent and poorly design.

    不友好的用户:导航到一堆JFrame中并不容易,看起来您的应用程序是一组不一致和设计糟糕的应用程序。

  • It's easy to use JInternalFrame This is kind of retorical, now it's way easier and other people smarter ( or with more spare time) than us have already think through the Desktop and JInternalFrame pattern, so I would recommend to use it.

    使用JInternalFrame很容易,这是一种反驳,现在它比我们已经考虑过桌面和JInternalFrame模式更容易和其他人更聪明(或有更多的空闲时间),所以我建议使用它。

#6


9  

Bad practice definitely. One reason is that it is not very 'user-friendly' for the fact that every JFrame shows a new taskbar icon. Controlling multiple JFrames will have you ripping your hair out.

实践肯定不好。一个原因是它不是很“友好”,因为每个JFrame都显示一个新的任务栏图标。控制多个jframe会让你抓狂。

Personally, I would use ONE JFrame for your kind of application. Methods of displaying multiple things is up to you, there are many. Canvases, JInternalFrame, CardLayout, even JPanels possibly.

就我个人而言,我将为您的应用程序使用一个JFrame。显示多个东西的方法取决于你,有很多。画布、JInternalFrame、CardLayout,甚至可能是jpanel。

Multiple JFrame objects = Pain, trouble, and problems.

多个JFrame对象=痛苦、麻烦和问题。

#7


6  

I think using multiple Jframes is not a good idea.

我认为使用多个jframe不是一个好主意。

Instead we can use JPanels more than one or more JPanel in the same JFrame.

相反,我们可以在同一个JFrame中使用JPanel多于一个或多个JPanel。

Also we can switch between this JPanels. So it gives us freedom to display more than on thing in the JFrame.

我们还可以在jpanel之间切换。它给了我们更多的*来显示JFrame中的东西。

For each JPanel we can design different things and all this JPanel can be displayed on the single JFrameone at a time.

对于每个JPanel,我们可以设计不同的东西,所有的JPanel可以一次显示在单个JFrameone上。

To switch between this JPanels use JMenuBar with JMenuItems for each JPanelor 'JButtonfor eachJPanel`.

要在这个jpanel之间切换,请使用JMenuBar,并为每个JPanelor 'JButtonfor eachJPanel '提供JMenuItems。

More than one JFrame is not a good practice, but there is nothing wrong if we want more than one JFrame.

不止一个JFrame不是一个好的实践,但是如果我们想要一个以上的JFrame就没有问题了。

But its better to change one JFrame for our different needs rather than having multiple JFrames.

但是,根据不同的需要更改一个JFrame比使用多个JFrame要好。

#8


4  

If the frames are going to be the same size, why not create the frame and pass the frame then as a reference to it instead.

如果帧的大小相同,为什么不创建帧并将其作为引用传递给它呢?

When you have passed the frame you can then decide how to populate it. It would be like having a method for calculating the average of a set of figures. Would you create the method over and over again?

当你通过了框架,你就可以决定如何填充它。这就像有一种计算一组数字平均值的方法。你会一次又一次地创建这个方法吗?

#9


3  

It is not a good practice but even though you wish to use it you can use the singleton pattern as its good. I have used the singleton patterns in most of my project its good.

这不是一个好的实践,但是即使您希望使用它,您也可以使用单例模式作为它的优点。我在我的大多数项目中都使用了单例模式。