我正在开发一个显示图像的应用程序,并从数据库中播放声音。我正在尝试决定是否使用单独的JFrame从GUI向数据库添加图像。
我只是想知道使用多个JFrame窗口是否是好的实践?
我正在开发一个显示图像的应用程序,并从数据库中播放声音。我正在尝试决定是否使用单独的JFrame从GUI向数据库添加图像。
我只是想知道使用多个JFrame窗口是否是好的实践?
当前回答
如果这两个帧的大小是相同的,为什么不创建一个帧,然后把它作为引用传递给它呢?
当你通过这个框架时,你可以决定如何填充它。这就像有了一种计算一组数字平均值的方法。你会一遍又一遍地创建这个方法吗?
其他回答
这不是一个好的实践,但即使你希望使用它,你也可以使用单例模式。我在我的大部分项目中都使用了单例模式,这很好。
绝对是坏习惯。一个原因是它不是很“用户友好”,因为每个JFrame都显示一个新的任务栏图标。控制多个jframe会让你抓狂。
就我个人而言,我会为您的应用程序使用ONE JFrame。显示多个东西的方法取决于你,有很多。画布,JInternalFrame, CardLayout,甚至JPanels可能。
多个JFrame对象=痛苦、麻烦和问题。
将jInternalFrame放入主框架中,并使其不可见。然后您可以将它用于进一步的事件。
jInternalFrame.setSize(300,150);
jInternalFrame.setVisible(true);
我认为使用多个jframe不是一个好主意。
相反,我们可以在同一个JFrame中使用多个JPanel。
我们还可以在这些jpanel之间切换。它给了我们在JFrame中显示更多东西的自由。
对于每个JPanel,我们可以设计不同的东西,所有这些JPanel都可以一次显示在单个JFrameone上。
要在这个jpanel之间切换,使用JMenuBar和JMenuItems为每个JPanelor 'JButtonfor每个jpanel '。
多于一个JFrame不是一个好的实践,但是如果我们想要多于一个JFrame也没有什么问题。
但是根据不同的需求更改一个JFrame比使用多个JFrame更好。
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.
因此,我将解释多JFrame方法的优点,并戳破其他人提出的一些缺点。
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. 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. 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. 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. 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).
我写了很多,我觉得我还能写更多。不管怎样,我希望我不会因为这是一个不受欢迎的观点而被否决。这个问题显然很有价值,我希望我提供了一个有价值的答案,即使它不是普遍的观点。
多帧/每帧一个文档(SDI) vs单帧/每帧多个文档(MDI)的一个很好的例子是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) 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 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,即每个窗口只能有一个文档):
MDI(多文档接口,即每个窗口可以有多个文档):