Java Reference
In-Depth Information
Figure 12.1
Frame created in the FrameDemo program.
Let's look at the steps involved in creating a JFrame. You start by instantiat-
ing a JFrame using one of the following constructors:
public JFrame(). Creates a new JFrame with no message in the title bar.
public JFrame(String title).
Creates a new JFrame with the given String
appearing in the title bar.
public JFrame(GraphicsConfiguration gc). Creates a JFrame with the
specified GraphicsConfiguration of a screen device.
public JFrame(String title, GraphicsConfiguration gc).
Creates a JFrame
with the specified title and GraphicsConfiguration.
The constructors are similar to those in the Frame class, and the parameters
have the same uses. The following statement instantiates a JFrame with “My
first JFrame” in the title bar:
JFrame f = new JFrame(“My first JFrame”);
As with Frame objects, this JFrame is initially not visible and has a size of 0
pixels by 0 pixels. You invoke one of the setSize(), setBounds(), or pack() meth-
ods to give the JFrame a size and then invoke setVisible() to make it visible.
The following JFrameDemo program demonstrates creating and displaying a
JFrame object. Study the program and try to determine its output, which is
shown in Figure 12.2.
import javax.swing.*;
public class JFrameDemo
{
public static void main(String [] args)
{
JFrame f = new JFrame(“My first JFrame”);
f.setSize(400, 300);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Search WWH ::




Custom Search