Java Reference
In-Depth Information
that create these components. The next sections describe how to create the following
GUI components:
￿ Windows
￿ Labels
￿ Text fields
￿ Buttons
GUI components, such as labels, are placed in an area called the content pane of the
window. You can think of a content pane as the inner area of the window, below the
title bar and inside the border. You will also learn how to place these GUI components in
the content pane of a window.
In Figure 6-2, when you click the Calculate button, the program displays the area and
perimeter of the rectangle you have specified. This means that clicking the Calculate
button causes the program to execute the code to calculate the area and perimeter and
then display the results. When the Calculate button is clicked, we say that an event has
occurred. The Java system is very prompt in listening for the events generated by a
program and then reacting to those events. This chapter will describe how to write the
code that needs to be executed when a particular event occurs, such as when a button is
clicked. So, in addition to creating windows, labels, text fields, and buttons, you will
learn:
￿ How to access the content pane
￿ How to create event listeners
￿ How to process or handle events
We begin by describing how to create a window.
Creating a Window
GUI components such as windows and labels are, in fact, objects. Recall that an object is
an instance of a particular class. Therefore, these components (objects) are instances of a
particular class type. JFrame is a class and the GUI component window can be created
by using a JFrame object. Various attributes are associated with a window. For example:
￿ Every window has a title.
￿ Every window has width and height.
JFrame
The class JFrame provides various methods to control the attributes of a window. For
example, it has methods to set the window title and methods to specify the height and
width of the window. Table 6-1 describes some of the methods provided by the class
JFrame .
 
 
 
Search WWH ::




Custom Search