Java Reference
In-Depth Information
Self-Test Exercises
39. In the GUI in Display 17.19, why did we make the text field ioField an
instance variable but did not make instance variables of any of the buttons
addButton , subtractButton , or resetButton ?
40. What would happen if the user running the GUI in Display 17.19 were to run
the GUI and simply click the addition button without typing anything into the
text field?
41. What would happen if the user running the GUI in Display 17.19 were to type
the number 10 into the text field and then click the addition button three times?
Explain your answer.
42. Suppose you change the main method in Display 17.19 to the following:
public static void main(String[] args)
{
Calculator calculator1 = new Calculator();
calculator1.setVisible( true );
Calculator calculator2 = new Calculator();
calculator2.setVisible( true );
}
This will cause two calculator windows to be displayed. (If one is on top of the
other, you can use your mouse to move the top one.) If you add numbers in one
of these calculators, will anything change in the other calculator?
43. Suppose you change the main method in Display 17.19 as we described in Self-
Test Exercise 42. This will cause two calculator windows to be displayed. If you
click the close-window button in one of the windows, will one window go away
or will both windows go away?
Chapter Summary
Swing GUIs (graphical user interfaces) are programmed using event-driven pro-
gramming. In event-driven programming, a user action, like a button click, gener-
ates an event, and that event is automatically passed to an event-handling method
that performs the appropriate action.
There are two main techniques for designing a Swing GUI class. You can use
inheritance to create a derived class of one of the library classes such as JFrame or
you can build a GUI by adding components to a container class. You normally
use both of these techniques when defining a Swing GUI class.
A windowing GUI is usually defined as a derived class of the class JFrame .
Search WWH ::




Custom Search