gpt4 book ai didi

java - : the use of getContentPane() from JFrame Class & getContentPane() from JRootPane? 和有什么区别

转载 作者:行者123 更新时间:2023-11-30 02:28:08 26 4
gpt4 key购买 nike

我做了一些研究来了解 JFrame 的使用及其部件,我发现了以下信息:

1- JFrame 的部分

  • 根 Pane
  • 菜单裸露
  • 内容 Pane
  • 玻璃板

2- JFrame 实现 RootPaneContainer 接口(interface)

return type  |  method name

Container getContentPane();

Container getGlassPane();

JLayeredPane getLayeredPane();

JRootPane getRootPane();

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

JFrame 实现了 RootPaneContainer 接口(interface),并且它们都将其操作委托(delegate)给 JRootPane。

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

代码:

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();
}

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

最佳答案

没有区别。调用 frame.getContentPane() 比调用 frame.getRootPane().getContentPane() 更方便。

Swing 这样做有两个原因。

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

2) 它具有单独的 JRootPane,以便可以在其他 RootPaneContainer 实现类(如 JDialorJInternalFrame)中重用。就像 Delegation模式。

关于java - : the use of getContentPane() from JFrame Class & getContentPane() from JRootPane? 和有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45043740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com