Java Reference
In-Depth Information
FlowLayout . We do this by inserting the following line in the program before adding
the buttons:
frame.setLayout(new FlowLayout());
Setting the layout in this manner ensures that both components will be visible in
the frame. The program now produces the following graphical output:
We'll discuss layout managers in detail in Section 14.2.
You may have noticed that the frame isn't sized quite right—there's extra space at
the bottom and on the sides around the buttons. JFrame objects have a useful method
called pack that resizes them exactly to fit their contents. If you call pack , you don't
need to call setSize on the frame because pack will set its size for you. Calling this
method just before you display the frame on the screen will ensure that the compo-
nents fit snugly within it, without excess whitespace:
frame.pack();
frame.setVisible(true);
The new, packed frame has the following onscreen appearance:
Handling an Event
The user interfaces we've created so far are not interactive—nothing happens
when the user clicks the buttons or types on the components. In order to create
useful interactive GUIs, you must learn how to handle Java events. When the user
clicks on a component, moves the mouse over it, or otherwise interacts with
it, Java's GUI system creates a special kind of object called an event to represent
this action.
Event
An object that represents a user's interaction with a GUI component and
that can be handled by your programs to create interactive components.
 
Search WWH ::




Custom Search