Ask Questions?

View Latest Questions


 
 

JFrame - Window
Posted on: July 26, 2006 at 12:00 AM
The javax.swing.JFrame class is used to create a "window".

Java: JFrame - Window

Description

The javax.swing.JFrame class is used to create a "window". This window has a few characteristics of its own (title bar, etc), but most of the controls are placed in one of two subareas: content pane or menu bar.

Constructors

You can create a frame like this:

   JFrame w = new JFrame("your title");  // OK, but it's better to use subclassing as below.

Subclass JFrame, build in constructor. Another better way is to define a class, eg MyWindow that extends JFrame, put the code which builds the GUI in the class's constructor, and create an instance of the window from the main program. See example below.

Content pane - Two styles - Get it or set it

There are two common ways to use a JFrame's content pane. Both are commonly used. In both cases, the easiest style is to assign the working version of the content pane to

  • Get the predefined content pane and change it. Every JFrame comes with a default content pane. It doesn't have anything on it, altho it does have a default layout, probably not the one you want to use tho! The getContentPane() method returns a Container object, which interestingly is actually a Jpanel. No problem with this approach.
  • Create a JPanel and make it the content pane. Most of my example code is written this way. This requires a call to setContentPane(...).

Common methods

JFrame    w;   // This is the natural Java "window".
Container c;   // This will typically be a JPanel (subclass of Container).
WindowListener listen; //
String    title;
boolean   b;
JMenuBar  mBar;
Using the content pane. See Content Panes
c = w.getContentPane(); Returns window's content pane. Use either get or set, but not both.</