Java 在 Swing 中使用框架、窗格或面板有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4479528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what's the use of a frame , a pane or a panel in swing?
提问by Ahmed
I read that JFrame is made of several panes ..what are panes and why is Jframe made of panes ? And why there is a JPanel while it seems that the JFrame looks exactly like the JPanel but with a menu bar and a close button so what's the need for a JPanel ? Can anybody explain to me clearly the definition and use of those 3 components ?
我读到 JFrame 是由几个窗格组成的……什么是窗格,为什么 Jframe 是由窗格组成的?为什么有一个 JPanel ,而 JFrame 看起来与 JPanel 完全一样,但有一个菜单栏和一个关闭按钮,那么 JPanel 有什么需要呢?任何人都可以向我清楚地解释这 3 个组件的定义和用途吗?
采纳答案by Vincent Ramdhanie
There are top level containers such as JFrame. These can serve as the main window in which a GUI is built.
有顶级容器,例如 JFrame。这些可以用作构建 GUI 的主窗口。
Then there are intermediate level containers. These must be placed in other containers, they cannot exist by themselves. They either help you organize components or they add functionality. A JPanel is a very simple container that helps you to organize other components. While a JSplitPane adds the functionality of having two panes that are variable sized.
然后是中级容器。这些必须放在其他容器中,它们不能单独存在。它们要么帮助您组织组件,要么添加功能。JPanel 是一个非常简单的容器,可帮助您组织其他组件。JSplitPane 添加了具有两个可变大小的窗格的功能。
When you have a complex GUI you may want to use JPanels to organize various areas of your GUI and then add each of the panels to your JFrame.
当您有一个复杂的 GUI 时,您可能希望使用 JPanel 来组织 GUI 的各个区域,然后将每个面板添加到您的 JFrame。
In Java the Swing API makes use of the Composite Design Pattern. This means that you can compose very complex objects from other objects and still treat the composite objects the same way as the simple objects. So you can put a JPanel into a JPanel and it still behaves like a JPanel.
在 Java 中,Swing API 使用复合设计模式。这意味着您可以从其他对象组合非常复杂的对象,并且仍然以与简单对象相同的方式处理复合对象。所以你可以将一个 JPanel 放入一个 JPanel 中,它的行为仍然像一个 JPanel。
Think of it like a tackle box (or sewing kit). It is made of a big container. But rather than put many small objects into this big container and make it difficult to manage later you can place some smaller compartments inside the big box. Then hooks and sinkers etc go in the compartments. Its easier to manage. The big box is the JFrame and the compartments are the JPanels.
把它想象成一个钓具箱(或针线包)。它由一个大容器制成。但是,与其将许多小物件放入这个大容器中并使其以后难以管理,您还可以在大盒子内放置一些较小的隔间。然后挂钩和坠子等进入隔间。它更容易管理。大盒子是 JFrame,隔间是 JPanel。