Java Reference
In-Depth Information
PITFALL: (continued)
In the program in Display 17.2 this would mean that if the user clicks the close-window
button, the window will hide (become invisible and inaccessible), but the program will not
end, which is a pretty bad situation. Since the window would be hidden, there would be no
way to click the "Click to end program." button. You would need to use some operat-
ing system command that forces the program to end. That is an operating system topic, not
a Java topic, and the exact command depends on which operating system you are using.
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
JButton endButton = new JButton("Click to end program.");
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."
Search WWH ::




Custom Search