Java - 向 JFrame 添加组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10086936/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:33:16  来源:igfitidea点击:

Java - adding components to JFrame

javaswingjframe

提问by Tim

I've seen a couple of ways of doing theism they both seem to work but I'm just wondering if one is better practice over the other.

我已经看到了几种做有神论的方法,它们似乎都有效,但我只是想知道一种方法是否比另一种更好。

For example, with a JFramecalled myFrameyou could do:

例如,使用JFramecallmyFrame你可以这样做:

myFrame.add(new JButton("OK"));

And you can also do:

你也可以这样做:

Container c = myFrame.getContentPane();
c.add(new JButton("OK"));

Is one of these 'correct'?

其中之一是“正确的”吗?

回答by Robin

A literal copy from the class javadoc of JFrame

来自类 javadoc 的文字副本 JFrame

The JFrame class is slightly incompatible with Frame. Like all other JFC/Swing top-level containers, a JFrame contains a JRootPane as its only child. The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame. This is different from the AWT Frame case. As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:

JFrame 类与 Frame 稍微不兼容。与所有其他 JFC/Swing 顶级容器一样,JFrame 包含一个 JRootPane 作为其唯一子项。根窗格提供的内容窗格通常应包含 JFrame 显示的所有非菜单组件。这与 AWT Frame 情况不同。作为便利添加及其变体,remove 和 setLayout 已被覆盖以根据需要转发到 contentPane。这意味着你可以写:

   frame.add(child);

And the child will be added to the contentPane. The content pane will always be non-null. Attempting to set it to null will cause the JFrame to throw an exception. The default content pane will have a BorderLayout manager set on it. Refer to RootPaneContainer for details on adding, removing and setting the LayoutManager of a JFrame.

并且孩子将被添加到 contentPane。内容窗格将始终为非空。尝试将其设置为 null 将导致 JFrame 抛出异常。默认内容窗格将设置一个 BorderLayout 管理器。有关添加、删除和设置 JFrame 的 LayoutManager 的详细信息,请参阅 RootPaneContainer。

So both are equivalent, and both are correct

所以两者是等价的,两者都是正确的

回答by mKorbel

From Java5 isn't required

不需要来自 Java5

  • to add JComponentsto the ContentPane, just JFrame.add(JComponent)

  • JFramehas implemented BorderLayout, then myFrame.add(new JButton("OK"));is placed to the CENTERarea

  • 添加JComponentsContentPane, 只是JFrame.add(JComponent)

  • JFrame已实施BorderLayout,然后myFrame.add(new JButton("OK"));放置到该CENTER区域

回答by Loyalar

I would definetly say that the

我肯定地说

Container c = myFrame.getContentPane();
c.add(new JButton("OK"));

Is the most practical one. Since you will most likely later on need to use the container that is the

是最实用的。由于您以后很可能需要使用作为

myFrame.getContentPane();

you don't need to write it again later. It will for example be used if you need to set another layout for the frame. But as said earlier, both can be used.

你以后不需要再写了。例如,如果您需要为框架设置另一个布局,将使用它。但如前所述,两者都可以使用。