Java Reference
In-Depth Information
8. Write the code that would produce each of the layouts pictured in problem 7, given the following variable declarations:
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
JButton b3 = new JButton("Button 3");
JButton b4 = new JButton("Button 4");
JButton b5 = new JButton("Wide Button 5");
Section 14.3: Interaction between Components
9. What is an event? What is a listener? What interface does Java use to handle standard action events?
10. Describe the code that you must write for your program to handle an event. What class(es) and method(s) do you
have to write? What messages must be sent to the component in question (such as the button to be clicked)?
11. Write an ActionListener to be attached to a button so that whenever the user clicks that button, an option pane
will pop up that says, “Greetings, Earthling!”
12. Why should complex GUIs that handle events be written in an object-oriented way?
Section 14.4: Additional Components and Events
13. What classes and interfaces does Java use to implement mouse listeners? In what ways does the process of handling
mouse events differ from that of handling action events?
14. Modify the MousePointGUI program's mouse listener so that it sets the frame's background color to blue if the
mouse pointer is in the upper half of the frame and red if it is in the lower half of the frame. ( Hint : Use the label's
getHeight and setForeground methods in your solution.)
Section 14.5: Two-dimensional Graphics
15. What class should be extended in order to draw two-dimensional graphics? What method should be overwritten to
do the drawing?
16. Write a panel class that paints a red circle on itself when it is drawn on the screen.
17. What is a timer? How are timers used with panels in programs that draw graphics?
18. Modify your red circle panel from problem 16 so that the color changes to blue and back again, alternating every
second. Use a timer to “animate” the color changes of the panel.
Exercises
1. Write a complete Java program that creates a window titled “Good thing I studied!” that is 285 by 200 pixels.
 
Search WWH ::




Custom Search