A JFrame is a component and top-level container of the JFC/Swing framework.
A JFrame is a component and top-level container of the JFC/Swing framework. A JFrame is a Frame element in Java Swing but is slightly incompatible with Frame. It is normally the outermost component, has a Frame and a title, and is usually filled with menu and JPanel(s) which contain(s) further components.
JFrame is heavyweight, since it's based on creating a "heavyweight" AWT window. Lightweight components can replace internal widgets with java-based implementation that doesn't require the use of JNI (Java Native Interface), but windows are the special case. JFrame does let you do custom rendering, via it's heavy window. Also, if you're using other lightweight stuff, all of them will be added to the contentPane
. Using JFrame makes the rendering more efficient overall than mixing light and heavy components.
JFrame itself is a top-level container and 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.
JFrame contains several layers. The main layer where all Swing components are added is the content pane:
frame.getContentPane().add(new JButton("Ok"), BorderLayout.CENTER);
As a convenience, the add method and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write
frame.add(new JButton("OK"), BorderLayout.CENTER);
and the JButton
will be added to the contentPane.
If you are adding components just to the JFrame itself, you are actually adding them to the content pane (same about removal, etc).
On the top of it, there is glass pane component (it is not a container). It is painted on the top of all components in the content pane. It is invisible by default you need to call setVisible(true)
to show it:
JFrame frame = new JFrame();
frame.getContentPane().add(new JButton("Ok"), BorderLayout.CENTER);
frame.setGlassPane(new JLabel("-------------------------"));
frame.getGlassPane().setVisible(true);
frame.setSize(100,100);
frame.setVisible(true);
The size and location of JFrame are specified in screen coordinates.
Reference: Class JFrame