对JFrame添加组件有两种方式:1) 用getContentPane()方法获得JFrame的内容面板,再对其加入组件:frame.getContentPane().add(childCompontent)
常分开来写
Container container=getContentPanel();(隐式的this.getContentPanel()) ;得到jframe的内容面板
以后只要把容器加到container就可以了。
2) 建立一个JPanel或JDesktopPane之类的中间容器,把组件添加到容器中,用setContentPane()方法把该容器置为JFrame的内容面板:JPanel contentPane = new JPanel();......//把其他组件添加到JPanel中frame.setContentPane(contentPane);//把contentPane对象设置成为frame的内容面板
一般使用JFrame添加组件时,比如frame是JFrame的一个对象,我一般都是直接使用add()方法将组件加入,但是我看了很多例子,他们都是frame.getContentPane().add(),先得到内容面板,然后再添加组件,这两种方法的区别是什么,为什么后面那个好像用的多些呢?
网友回答:
有区别的
当你创建一个JFrame的时候JFrame jf = new JFrame();
在构造方法JFrame()内部会给jf默认添加一个rootPane
所以执行完JFrame jf = new JFrame();这句话之后jf上面已经添加了一个默认的rootpanel了
然后你再调用jf.add(panel) 这个时候,panel和rootPane是平级的
理由:1,你可以读源代码 ,查看构造方法怎么写的
2,或者你可以测试一下,分别执行
jf.setBackground(Color.blue);
jf.getContentPane().setBackground(Color.black);
这两句代码,看看效果(实际上上面一句并不能改变界面的背景色,下面一句才可以,因为rootPane把jf给挡住了,上面一句是改变了jf的背景色,但是你眼睛看到的并不是jf,其实是rootPane.)
另外
jf.getContentPane()==jf.getRootPane().getContentPane()
上面的比较返回的true
所以你调用jf.getContentPane().add(panel) 其实是把panel添加到rootPane上面了 这个时候panel和rootPane就不是平级的了
JFrame java api
An extended version of java.awt.Frame that adds support for the JFC/Swing component architecture. You can find task-oriented documentation about using JFrame in The Java Tutorial, in the section How to Make Frames.
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 theJFrame. 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:
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 toRootPaneContainerfor details on adding, removing and setting theLayoutManagerof aJFrame.
JFrame 类与 Frame 轻微不兼容。与其他所有 JFC/Swing 顶层容器一样,JFrame 包含一个 JRootPane 作为其唯一的子容器。根据规定,根窗格所提供的内容窗格应该包含 JFrame 所显示的所有非菜单组件。这不同于 AWT Frame。为了方便地使用 add 及其变体,已经重写了 remove 和 setLayout,以在必要时将其转发到 contentPane。这意味着可以编写:
frame.add(child);
子级将被添加到 contentPane。内容窗格始终是非 null 的。试图将其设置为 null 会导致 JFrame 抛出异常。默认的内容窗格上会设置有 BorderLayout 管理器。有关添加、移除和设置JFrame的LayoutManager的详细信息,请参阅RootPaneContainer。产生JFrame的两种方法(不继承和继承)
importjavax.swing.JFrame;public classGameFrame {publicGameFrame()
{
JFrame frame=newJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("3d tetris");
frame.setSize(500,300);
frame.setLocation(400,400);
frame.setVisible(true);
}public static voidmain(String[] args)
{
GameFrame gameFrame=newGameFrame();
}
}
public class GameFrame extendsJFrame{publicGameFrame()
{super("3d tetris"); //设置标题,不要也可以
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("3d tetris");
setSize(500,300);
setLocation(400,400);
setVisible(true);
}public static voidmain(String[] args)
{
GameFrame gameFrame=newGameFrame();
}
}