(^378) | Event-Driven Input and Output
his tomb. Likewise, we could read all of the source code in the swingpackage to learn how JFrame
objects work. Fortunately, Java makes it unnecessary for us to do this by relying on the princi-
ple of abstraction: We don’t have to understand what makes JFrameobjects work to use them.
GetaContentPane As we’ve just seen, theJFramethat is referenced byoutputFrameconsists of two
parts: a window frame and a content pane. Rather than instantiate a new content pane ob-
ject, we merely ask theJFrameto give us the one that was created within it automatically when
we instantiated theJFrame. We send this request to theJFrameby calling one of its methods:
outputPane = outputFrame.getContentPane();
The content pane referenced by outputPaneis an empty window that is waiting to be
filled and then shown on the screen. Before we can do so, we need to specify some additional
properties of the frame and content pane. For the frame, we need to indicate what should
happen when it is closed and what its size should be on the screen. For the content pane,
we need to indicate how elements should be arranged within it.
Specify the Action to Take When the Frame Is Closed Specifying what should happen when the frame
is closed by the user is done through a call to an instance method associated with the frame.
This method is named setDefaultCloseOperation. We pass it an argument indicating the ac-
tion that the frame should perform. The JFrameclass provides several named class constants
that are allowed as arguments to this method. We created class constants and used them in
a similar fashion in our Studentclass in Chapter 4.
The only one of these actions that is appropriate for our programs is to exit the pro-
gram (end execution and remove the frame from the screen). The constant for this action is
called EXIT_ON_CLOSE. Because this constant is associated with the JFrameclass, we must write
it with the class name, separated by a period, as shown here within the parentheses:
outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Specify the Size of the Frame We set the size of the frame by calling another instance method,
named setSize. This method takes two arguments that are integer numbers. The first is the
width of the frame and the second is its height. The sizes are specified in pixels.A
typical display screen is 1,024 pixels wide by 768 pixels high, so you need to choose
numbers that produce a frame that fits on the screen in a reasonable manner,
with room for all of the information that it displays. Here is an example call, which
sets the size of the frame to be 300 pixels wide and 200 pixels high:
outputFrame.setSize(300, 200);
Specify a Layout Manager We fill the content pane by adding display elements to it. How does Java
know where the elements should be positioned within the pane? We can either tell it manu-
ally or let Java handle their layout automatically. Manual placement of elements allows us to
precisely control the appearance of output within the pane, but requires us to tediously com-
Pixels An abbreviation of “pic-
ture elements”; the individual
dots that make up an image on
a display screen