之间的区别是:使用来自JFrame Class的getContentPane()和来自JRootPane的getContentPane()?

时间:2021-04-29 22:27:07

I had done some research to understand the use of JFrame, and its parts and i find this info's:

我做了一些研究,以了解JFrame的使用,以及它的部分,我发现这个信息是:

1- Parts of JFrame

1-部分JFrame

  • Root Pane
  • Menu Bare
  • Content Pane
  • Glass Pane

2- JFrame implements Interface RootPaneContainer

2- JFrame实现接口RootPaneContainer

return type  |  method name

Container       getContentPane();

Container       getGlassPane();

JLayeredPane    getLayeredPane();

JRootPane       getRootPane();

3- JRootPane JRootPane of a JFrame. JRootPane is a lightweight container used behind the scenes by JFrame.

3-JRootPane JFrame的JRootPane。 JRootPane是JFrame在幕后使用的轻量级容器。

JFrame implement the RootPaneContainer interface, and they all delegate their operations to a JRootPane.

JFrame实现了RootPaneContainer接口,它们都将其操作委托给JRootPane。

Note: The JComponent method getRootPane can be used to obtain the JRootPane that contains a given component.

注意:JComponent方法getRootPane可用于获取包含给定组件的JRootPane。

code :

public static void main(String[] args) {

        //--> create JFrame
        JFrame f = new JFrame("Demo Frame");
        f.setSize(300, 150);
        f.setVisible(true);

        //--> now use JFrame.getContentPane()

        Container c = f.getContentPane();

        //--> use JOptionpane.getContentPane()

        JRootPane op = f.getRootPane();
        op.getContentPane();         
    }

Question : why i should use the method getContentPane() from JRootPane instead of JFrame ?

问题:为什么我应该使用JRootPane中的方法getContentPane()而不是JFrame?

2 个解决方案

#1


2  

No difference. It is just more convenient to call frame.getContentPane() than frame.getRootPane().getContentPane().

没有不同。调用frame.getContentPane()比使用frame.getRootPane()。getContentPane()更方便。

There are 2 reason why Swing does this way.

Swing这样做有两个原因。

1) JFrame needs to implement RootPaneContainer as marker for reasons specified in JavaDoc.

1)由于JavaDoc中指定的原因,JFrame需要将RootPaneContainer实现为标记。

2) It has separate JRootPane so that it can be reused in other RootPaneContainer implementing classes like JDialor and JInternalFrame. It is like Delegation pattern.

2)它具有单独的JRootPane,因此可以在其他RootPaneContainer实现类(如JDialor和JInternalFrame)中重用它。这就像代表团模式。

#2


0  

I found an answer my help someone else :

我找到了答案,我帮助别人:

The interface RootPaneContainer :

RootPaneContainer接口:

is implemented by components that have a single JRootPane child: JDialog, JFrame, JWindow, JApplet, JInternalFrame. The methods in this interface are just covers for the JRootPane properties,

由具有单个JRootPane子代的组件实现:JDialog,JFrame,JWindow,JApplet,JInternalFrame。此接口中的方法只是JRootPane属性的封面,

e.g. getContentPane() is generally implemented like this:

例如getContentPane()通常像这样实现:

 public Container getContentPane() {
     return getRootPane().getContentPane();
 }

getRootPane(); return a JRootPane object

的getRootPane();返回一个JRootPane对象

getContentPanel(); return Container Object

getContentPanel();返回Container对象

#1


2  

No difference. It is just more convenient to call frame.getContentPane() than frame.getRootPane().getContentPane().

没有不同。调用frame.getContentPane()比使用frame.getRootPane()。getContentPane()更方便。

There are 2 reason why Swing does this way.

Swing这样做有两个原因。

1) JFrame needs to implement RootPaneContainer as marker for reasons specified in JavaDoc.

1)由于JavaDoc中指定的原因,JFrame需要将RootPaneContainer实现为标记。

2) It has separate JRootPane so that it can be reused in other RootPaneContainer implementing classes like JDialor and JInternalFrame. It is like Delegation pattern.

2)它具有单独的JRootPane,因此可以在其他RootPaneContainer实现类(如JDialor和JInternalFrame)中重用它。这就像代表团模式。

#2


0  

I found an answer my help someone else :

我找到了答案,我帮助别人:

The interface RootPaneContainer :

RootPaneContainer接口:

is implemented by components that have a single JRootPane child: JDialog, JFrame, JWindow, JApplet, JInternalFrame. The methods in this interface are just covers for the JRootPane properties,

由具有单个JRootPane子代的组件实现:JDialog,JFrame,JWindow,JApplet,JInternalFrame。此接口中的方法只是JRootPane属性的封面,

e.g. getContentPane() is generally implemented like this:

例如getContentPane()通常像这样实现:

 public Container getContentPane() {
     return getRootPane().getContentPane();
 }

getRootPane(); return a JRootPane object

的getRootPane();返回一个JRootPane对象

getContentPanel(); return Container Object

getContentPanel();返回Container对象