Java Reference
In-Depth Information
Self-Test Exercises
1. What Swing class do you normally use to define a window? Any window class
that you define would normally be an object of this class.
2. What units of measure are used in the following call to setSize that appeared
in the main method of the program in Display 17.2? In other words, 300 what?
Inches? Centimeters? Light years? And similarly, 200 what?
firstWindow.setSize(WIDTH, HEIGHT);
which is equivalent to
firstWindow.setSize(300, 200);
3. What is the method call to set the close-window button of the JFrame
someWindow so that nothing happens when the user clicks the close-window
button in someWindow ?
4. What is the method call to set the close-window button of the JFrame
someWindow so that the program ends when the user clicks the close-window
button in someWindow ?
5. What happens when you click the minimizing button of the JFrame shown in
Display 17.2 ?
6. Suppose someWindow is a JFrame and n is an int variable with some value.
Give a Java statement that will make someWindow visible if n is positive and hide
someWindow otherwise.
Buttons
A button object is created in the same way that any other object is created, but you
use the class JButton . For example, the following example from Display 17.2 creates
a button:
JButton endButton = new JButton("Click to end program.");
JButton
The argument to the construct, in this case, "Click to end program." , is a string
that will be written on the button when the button is displayed. If you look at the
picture of the GUI in Display 17.2, you will see that the button is labeled " Click to
end program. "
We have already discussed adding components, such as buttons, to a JFrame . The
button is added to the JFrame by the following line from Display 17.2 :
adding a
button
firstWindow.add(endButton);
In the next subsection, we explain the lines from Display 17.2 involving the method
addActionListener .
 
Search WWH ::




Custom Search