Page 684 - Introduction to Programming with Java: A Problem Solving Approach
P. 684

                650 Chapter 16 GUI Programming Basics
The JFrame class should be the superclass for most of your GUI application windows, so a pro- grammer-definedwindowwillnormallyhaveextends JFrameinitsclassheading.Fortheextends JFrame to work, you must import the JFrame class or import JFrame’s package, javax.swing. As explained above, it’s common to import the javax.swing package for all GUI programs.
The JFrame class is called a container because it contains components (like labels, buttons, menus, and so on). It inherits the ability to contain components from its superclass, the Container class.
JFrame Methods
By extending the JFrame class, you automatically get the standard windows functionality mentioned above. In addition, you inherit a host of windows-related methods. In the SimpleWindow program, we use these inherited methods—setTitle, setSize, setLayout, setDefaultCloseOperation, add, and setVisible. The setLayout and setDefaultCloseOperation methods come directly from the JFrame class. The other methods come from ancestors of the JFrame class—setTitle from the Frame class, add from the Container class, setSize and setVisible from the Component class.
The setTitle method displays a specified string in the current window’s title bar. If setTitle is not called, then the window’s title bar is empty.
The setSize method assigns the width and height of the current window. See Figure 16.4 and note
how the SimpleWindow program assigns the width to 300 and the height to 200. The width and height val-
ues are specified in terms of pixels. A pixel is a computer monitor’s smallest displayable unit, and it displays
as a dot on the screen. If you call setSize with a width of 300 and a height of 200, then your window will
consist of 200 rows where each row contains 300 pixels. Each pixel displays with a certain color. The pixels
Apago PDF Enhancer
form a picture by having different colors for the different pixels. For example, the window depicted in Fig- ure 16.4 might contain blue pixels on the perimeter (for the window’s border), and black pixels in the center (for the window’s message).
To give you perspective on how big a 300-by-200 pixel window is, you need to know the dimensions, in pixels, of an entire computer screen. The dimensions of a computer screen are referred to as the screen’s resolution. Resolution settings are adjustable. Two common resolution settings are 800-by-600 and 1024- by-768. The 800-by-600 setting displays 600 rows where each row contains 800 pixels.
If you forget to call the setSize method, your window will be really small. It will display only the beginning of the title and the three standard window-adjustment buttons—minimize, maximize, and close- window. It won’t display the window’s contents unless you manually resize the window. Here’s what the SimpleWindow program displays if you omit the setSize method call:
The setLayout method assigns a specified layout manager to the current window. The layout man- ager is pre-built software from Sun that determines the positioning of components. In the SimpleWindow program’s setLayout call, we specify the FlowLayout manager, and the FlowLayout man- ager causes components to be positioned in the top-center position. The FlowLayout class is defined in the java.awt package, so don’t forget to import that package. In the next chapter, we describe the FlowLayout manager and other layout managers in more detail. We’re using the FlowLayout manager (as opposed to other layout managers) in this chapter because the FlowLayout manager is the easiest to use, and we’re trying to keep things simple for now.
 


















































































   682   683   684   685   686