本文翻译自:The Use of Multiple JFrames: Good or Bad Practice? [closed]

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窗口是否是一种好习惯?


#1楼

参考:https://stackoom.com/question/e5b2/使用多个JFrame-好的还是不好的做法-关闭


#2楼

Make an jInternalFrame into main frame and make it invisible. 使jInternalFrame进入主框架并使其不可见。 Then you can use it for further events. 然后,您可以将其用于其他事件。

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

#3楼

Bad practice definitely. 绝对是错误的做法。 One reason is that it is not very 'user-friendly' for the fact that every JFrame shows a new taskbar icon. 一个原因是,由于每个JFrame显示一个新的任务栏图标,因此它不是非常“用户友好”的。 Controlling multiple JFrame s will have you ripping your hair out. 控制多个JFrame将使您脱颖而出。

Personally, I would use ONE JFrame for your kind of application. 就个人而言,我将对您的应用程序使用一个JFrame Methods of displaying multiple things is up to you, there are many. 显示多种事物的方法取决于您,有很多。 Canvas es, JInternalFrame , CardLayout , even JPanel s possibly. CanvasJInternalFrameCardLayout甚至JPanel

Multiple JFrame objects = Pain, trouble, and problems. 多个JFrame对象=痛苦,麻烦和问题。


#4楼

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: 想到的一些主要缺点:

  • 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的使用很简单这是一种反驳,现在,它比我们通过Desktop and JInternalFrame模式已经想到的要容易得多,并且其他人可以更聪明(或有更多的空闲时间),所以我建议使用它。


#5楼

The multiple JFrame approach has been something I've implemented since I began programming Swing apps. 自从我开始对Swing应用程序进行编程以来,我已经实现了多种JFrame方法。 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! 但是 ,随着我作为开发人员的经验和知识日趋成熟,并开始在线阅读和吸收许多经验丰富的Java开发人员的意见,我尝试摆脱多种JFrame方法(在当前项目和将来都如此)。项目)只能得到满足...得到我的客户的抵制! As I began implementing modal dialogs to control "child" windows and JInternalFrame s for separate components, my clients began to complain! 当我开始实现模态对话框来控制“子”窗口和JInternalFrame用于单独的组件时, 我的客户开始抱怨! 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. 当然,我是承包商,因此我的最终用户可以直接与我(开发人员)联系,这显然不常见。

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 JFrame s, you give your end-user the ability to spread out and control what's on his/her screen. 布局的终极灵活性 -通过允许使用单独的JFrame ,您可以使最终用户能够分散并控制其屏幕上的内容。 The concept feels "open" and non-constricting. 该概念感到“开放”且不受限制。 You lose this when you go towards one big JFrame and a bunch of JInternalFrame s. 当您走向一个大JFrame和一堆JInternalFrame时,就会丢失此错误。
  2. 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. 对于非常模块化的应用程序,它可以很好地工作 -就我而言,我的大多数应用程序都有3-5个大的“模块”,它们实际上彼此之间没有任何关系。 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. 但是,主管可能希望同时打开它们,并且它们在任务栏上是单独的框架,这使他的生活更加轻松。
  3. 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 JFrame s. 最初,两者都是JFrame However, I wanted the data entry screen to be a JDialog whose parent was the data viewer. 但是,我希望数据输入屏幕是一个JDialog ,其父对象是数据查看器。 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. 对于JDialog来说这是不可能的,对于JInternalFrame当然也不可能。 I begrudgingly changed it back to being separate JFrames for his sanity, but it taught me an important lesson. 为了他的理智,我勉强将其改回为单独的JFrames ,但它教会了我一个重要的教训。
  4. 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 . 我不明白为什么创建JInternalFrameJFrame容易。 In fact, in my experience, JInternalFrames offer much less flexibility. 实际上,以我的经验, JInternalFrames提供的灵活性要差得多。 I have developed a systematic way of handling the opening & closing of JFrame s in my apps that really works well. 我已经开发出一种系统的方法来处理我的应用程序中JFrame的打开和关闭,这确实很好。 I control the frame almost completely from within the frame's code itself; 我几乎完全从框架代码本身内控制框架。 the creation of the new frame, SwingWorker s 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 JFrame s 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. 新框架的建立, SwingWorker s表示控制在后台线程和EDT GUI代码数据的检索,恢复/大家带来前方的框架,如果用户试图两次打开它,等你只需要打开我JFrame s被称为公共静态方法open() ,并且open方法与windowClosing()事件结合处理其余的事件(框架是否已打开?是否未打开,但正在加载?等),我将此方法用作模板,因此在每个框架上实现并不难。
  5. 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 JFrame s, how many more resources would you really be consuming? 尽管也许可以说JFrameJInternalFrame需要更多空间,但是即使您打开100个JFrame ,您实际上还要消耗多少资源? 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). 如果您担心的是由于资源导致的内存泄漏:调用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. Microsoft Excel是一个很好的例子,每帧多帧/单文档( SDI )与每帧单帧/多文档( MDI )。 Some of MDI benefits: 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 (eg 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窗口中写入时,有可能在一个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, ie, every window can only have a single document): SDI(单文档界面,即每个窗口只能有一个文档):

MDI (Multiple-Document Interface, ie, every window can have multiple documents): MDI(多文档界面,即每个窗口可以有多个文档):


#6楼

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. 几个月前,我们开始收到客户的要求,以使这些报表查看器窗口变为无模式,以便他们可以同时打开多个报表。

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. 他们正在打开查看器,使用“另存为”功能将报告另存为PDF到特定目录,使用Acrobat Reader打开PDF文件,然后对下一个报告执行相同的操作。 They would have multiple Acrobat Readers running with the various report outputs that they wanted to look at. 他们将有多个Acrobat Reader运行,并带有他们想要查看的各种报告输出。

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. 使用使用新ModalityType而不是布尔modal参数的构造函数。 This is what gives these dialogs the task-bar icon. 这就是为这些对话框提供任务栏图标的原因。
  • For modeless dialogs, pass a null parent to the constructor, but locate them relative to their 'parent' window. 对于无模式对话框,请将空父级传递给构造函数,但要相对于其“父级”窗口定位它们。
  • Version 6 of Java on Windows has a bug which means that your main window can become 'always on top' without you telling it. Windows上的Java版本6有一个错误 ,这意味着您的主窗口可能会在不通知您的情况下始终位于“顶部”。 Upgrade to version 7 to fix this 升级到版本7以解决此问题

使用多个JFrame:好的还是不好的做法? [关闭]相关推荐

  1. java如何停止函数循环_在Java中使用break来退出循环是不好的做法吗?

    好主不. 有时在满足总体要求的循环中可能会出现某些情况,而不满足逻辑循环条件. 在这种情况下,使用break ,来阻止你毫无意义地在循环中循环. 例 String item; for(int x = ...

  2. linux如何切换到独立显卡,Ubuntu中独立显卡不好用如何关闭独立显卡

    现在的笔记本大部分已经是双显卡了,独立显卡和集成显卡切换使用,但在Ubuntu系统中,独立显卡并不好用,有些人想要关闭,那么要如何关闭呢?下面小编就给大家介绍下Ubuntu关闭独立显卡的方法. 先卸载 ...

  3. Java JFrame隐藏标题栏以及最大化最小化关闭和拖动

    CSDN上第一篇博客,请大家多多支持! // 转载请注明出处! 直入主题吧,目前主流的软件,图形化界面一般都没有标题栏,优点是简单大方.然而,界面的最大化最小化和关闭按钮也随之隐藏,窗体也无法拖动.因 ...

  4. break stop java_在Java中使用break退出循环是不好的做法吗?

    小编典典 好主啊 有时,可能会在满足整体要求的循环中发生某些事情,而不满足逻辑循环条件.在这种情况下,break用于阻止您毫无意义地循环. 例 String item; for(int x = 0; ...

  5. java jframe添加按钮_JFrame窗口中如何加按钮啊?(JDK)

    JFrame窗口中如何加按钮啊?(JDK) 我在JFrame窗口中加入按钮,不成功,有人说在JFrame 中加按钮不好,可是我要在窗口中用按钮,我该如何做?在Frame中加按钮也行!请大侠提示!--- ...

  6. C#坏习惯:通过不好的例子学习如何制作好的代码——第3部分

    目录 这篇文章的目标 什么是c#中的静态类? 我们去看看代码 问题 单元测试 未来的变化 如何修复?? !! 第一步 第二步 第三步 第四步 这次改变后我们获得了什么? 单元测试问题--已解决 未来的 ...

  7. 《异步处理在分布式系统中的优化作用》学习笔记

    原文地址:http://www.infoq.com/cn/presentations/optimization-of-asynchronous-processing-in-distributed-sy ...

  8. MySQL 性能调优的10个方法

    MYSQL 应该是最流行了 WEB 后端数据库.WEB 开发语言最近发展很快,PHP, Ruby, Python, Java 各有特点,虽然 NOSQL 最近越來越多的被提到,但是相信大部分架构师还是 ...

  9. 关于javascript代码优化的8点建议

    前面的话 本文将详细介绍JS编程风格的几个要点 松耦合 当修改一个组件而不需要更改其他组件时,就做到了松耦合 1.将JS从CSS中抽离:不要使用CSS表达式 //不好的做法 .box{width: e ...

最新文章

  1. 仅用 480 块 GPU 跑出万亿参数!全球首个“低碳版”巨模型 M6 来了
  2. 利用webSocket使网页和服务器通信
  3. 前端要懂mysql_【灵魂拷问】你真的懂得Mysql的管理和使用吗?
  4. 计算机系学生thinkbook,ThinkBook系列性能差,只能当一台学习机?其实它只是专注于办公...
  5. cocos2dx标准容器_Cocos2d-x3.0模版容器详解之三:cocos2d::Value
  6. 【C语言】(数组方式)输出一组成绩中的最高分与最低分
  7. Android Animation学习(三) ApiDemos解析:XML动画文件的使用
  8. Halcon——使用标定板标定像素当量
  9. 根据共享文件夹的权限进行自动映射网络驱动器
  10. 微信小程序模板文件的引入
  11. 昆仑通态触摸屏如何把参数由触摸屏传递到PLC_昆仑通态触摸屏的串口232通讯功能-----有谁用过?...
  12. linux下ant安装和使用教程,ant安装与简单应用
  13. rs422/rs485通信接口原理图
  14. Pandas学习任务1-3
  15. 转帖自勉:一个猎头的自述
  16. Using platform encoding (UTF-8 actually) to copy filtered resources错误
  17. 视频书单号到底好不好做,能不能赚钱?
  18. 简洁易用报表工具-丰富可视化效果web报表工具
  19. request:fail -202:net::ERR_CERT_AUTHORI
  20. CSS中的text-overflow属性详解 (控制文字在一行显示,超出部分加省略号)

热门文章

  1. Android JNI 第三篇 Java参数类型与本地参数类型对照
  2. Android之app混淆深入分析-层层解析解决开发中痛点
  3. 深入思考编译原理之 理解执行过程和编译过程
  4. c++中的数组和指针,引用
  5. 一次Binder通信最大可以传输多大的数据?
  6. android studio安装中出现Failed to install Intel HAXM错误的解决方法
  7. 程序员必须掌握的核心算法有哪些?
  8. 第十六周程序阅读(6)
  9. python cgi nginx_nginx uwsgi和cgi python脚本
  10. Swift 泛型函数补充