Tips

You don’t use jFrame.add( child ); you use
jFrame.getContentPane().add( child ). This is
especially infuriating since the compiler cannot catch the error. Similarly instead
of jFrame.setLayout( layoutManager ) you use
jFrame.getContentPane().setLayout( layoutManager ).
Ditto for jFrame.getContentPane().setBackground( Color
) and jFrame.remove( child ).
Java version 1.5 or later is more forgiving and your
code still works if you forget to use contentPane.

You continue to use jFrame.setSize, jFrame.validate and jFrame.setVisible.

You can get a decorated frame with JFrame.setDefaultLookAndFeelDecorated( true ); The unusual thing is
this is a static method on JFrame, not a method for JFrame
objects.

You can change the coffee cup icon for the Jframe
with setIconImage.

The AWT (Advanced Windowing Toolkit) equivalent of Jframe is
called Frame.

You nearly always set your own layout manager since by default Jframe comes with the lame BorderLayout.

Jframe is a heavyweight component, more like an
AWT
component than a Swing one. It does not have a paintComponent, but an AWT-style paint.
Don’t paint directly on it. Paint on a JPanel
instead and insert that in the Jframe.

Co-ordinate System

When you specify the Jframe size, make it 22 pixels
wider and 32 pixels taller than you need for the contents to allow room for the top
Jframe bar. To add to the confusion, the origin is in the
top corner under the Jframe bar. So you must add 32 to all
your y coordinates inside a Jframe. This is one more
reason to use layouts and avoid absolute positioning.

There are four way Jframe sizes could have been
measured: all combinations of including or excluding the title bar and including or
excluding the border. Oracle chose to include both. The easiest way to calculate a
Jframe size is to manually adjust the Jframe until it looks right, then take a snapshot of it with PaintShop Pro, including the title bar and borders and
use PSP’s size as your jFrame.setSize to set the
initial size.

Jframe State

// You can control the current state of the Frame,// but you cannot disable the corresponding user controls.// To control the widgets, use a JInternalFrame.frame.setExtendedState(NORMAL );frame.setExtendedState(ICONIFIED );frame.setExtendedState(MAXIMIZED_HORIZ );frame.setExtendedState(MAXIMIZED_VERT );frame.setExtendedState(MAXIMIZED_BOTH );

Beware. getExtendedState in a componentResized event handler gives you the old state. You could use
SwingUtilities. invokeLater or
EventQueue. invokeLater to
probe the new state.

IsA vs HasA

There are two techniques for creating a frame full of components.

Extend Jframe with private variables, getters and setters.

Create a class with private variables, getters and
setters that contains a reference to a standard Jframe
object.

Advantages of Extension

Terser

Simpler

Most tutorials teach it this way.

Advantages of
Composition

Better separation of logic

Avoids exposing all the methods of Jframe (which is
both an advantage and disadvantage).

It gives greater freedom to reimplement with something other than Jframe without having to change all the referencing code.

Logically speaking, your application has a
Jframe. It is not a species of
Jframe.

Most professional programmers prefer to do it this way.

Debugging Harness

Here is a typical debugging harness you would append to every Jframe you write. Note how all initialisation is done on the Swing
event thread via SwingUtilities.

Caller/Callee Responsibility

Normally the caller who instantiates the Jframe does a
setSize to determine its size, then a setLocation to position it, then a validate and finally a setVisible ( true ).

You might do a setSize and setLocation in the constructor, but definitely not a setVisible. setTitle may be done by
either.