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 co-ordinates 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. Sun 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 Paint Shop Pro, including the title bar and borders, and use PSP’s size as your jFrame.setSize to set the initial size.
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 ). JDK 1.5+ 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 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.
// turn off the entire frame bar, including title, close, minimize, resize... frame.setUndecorated( true ); // turn off just resizing controls frame.setResizable( false );
// 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 the old state. You could use SwingUtilities. invokeLater or EventQueue. invokeLater to probe the new state.
You might to a setSize and setLocation in the constructor, but definitely not a setVisible. setTitle may be done by either.
![]() |
and suggestions to improve this page to Roedy Green : | ||
| Canadian Mind Products | |||
| mindprod.com IP:[65.110.21.43] | |||
| Your face IP:[38.103.63.18] | The information on this page is for non-military use only. | ||
| You are visitor number 42,856. | Military use includes use by defence contractors. | ||
| You can get a fresh copy of this page from: | or possibly from your local J: drive (Java virtual drive/Mindprod website mirror) | ||
| http://mindprod.com/jgloss/jframe.html | J:\mindprod\jgloss\jframe.html | ||