Java Reference
In-Depth Information
If the boolean passed in is true, the component is made visible. If the value
is false, the component is hidden. The following FrameDemo program creates
a Frame object, sets its bounds, and displays it on the screen. Study the pro-
gram and try to determine its output, which is shown in Figure 12.1.
import java.awt.*;
public class FrameDemo
{
public static void main(String [] args)
{
Frame f = new Frame(“My first window”);
f.setBounds(100,100, 400, 300);
f.setVisible(true);
}
}
If you run the FrameDemo program, you will see a Frame window similar
to the one in Figure 12.1. You can move, resize, minimize, and maximize
the Frame window. However, you can't close the window because closing
a window often implies ending the program. If the user needs to save a
document or other settings before ending, your program needs a chance
to do this.
Therefore, the closing of a Frame window is left to the programmer and
involves handling the WindowEvent generated by a user attempting to
close the window. I will show you how to do this in the next chapter, so
to close the window for now you need to stop the JVM. In Windows, this
can be done by pressing Ctrl+c from the command prompt.
javax.swing.JFrame Class
The javax.swing.JFrame class represents a window similar to Frame, except
that JFrame adds support for the Swing component architecture. A JFrame is a
heavyweight component, meaning that it has the look and feel of the native
platform. From a user's point of view, a JFrame and a Frame look the same.
Creating and displaying a JFrame is also similar to creating and displaying a
Frame.
However, a JFrame is different in terms of how components are added to the
JFrame. As opposed to a Frame, a JFrame has three panes that components can
be added to: a content pane, a glass pane, and a root pane. Typically, the con-
tent pane will contain all of the components of the JFrame. We will see several
examples in this chapter of adding components to the content pane of a
JFrame.
Search WWH ::




Custom Search