Exercises


Note

The solutions to these exercises are in Appendix B.

  1. Write a program that displays a frame. The frame's paint() method should draw something simple. The application should also maintain a count of the number of times paint() is called. This count should be printed out every time paint() is called. Execute your application, and use it to help determine whether paint() is called when:

    The application starts up.

    The frame is minimized/iconified.

    The frame is restored to normal size after being minimized/iconified.

    The frame is restored to normal size after being minimized/iconified.

    The frame is moved.

    The frame is partially covered by another frame.

    The frame is uncovered.

  2. Every Java thread is represented by an instance of the java.lang.Thread class. You can get a reference to the currently running thread by calling the currentThread() static method of the Thread class. Threads have names. The class has a method called getName(), which returns the name as a string. So you can print out the name of the current thread by calling

    System.out.println(Thread.currentThread().getName());

    Write a simple frame application that makes this call in its main() method and in its paint() method. Verify that main()and paint() are executed in different threads.

  3. Write an application that adds the same action listener to a button twice. For example, if myButton is the button and myListener is the action listener, your code would contain the following lines:

    myButton.addActionListener(myListener); myButton.addActionListener(myListener);

    Your listener's actionPerformed() method should print out a message to tell you that it got called. If you press the button once, do you expect the message to be printed out once or twice? Run your application to see if you guessed right.

    Of course, in real life there would never be a good reason for doing this. But you might do it by accident. For example, you might paste the line into your source code twice by accident. So it's good to know in advance what the symptom will be, so that you can recognize it and fix the problem if it ever comes up.

  4. Suppose a class has an actionPerformed() method, as specified by the ActionListener interface, but the class does not state that it implements the interface. Can an instance of the class be used as a button's action listener?

  5. Run Nim Lab by typing java events.NimLab. Select Disable Buttons... and play the game. This version is the result of three rounds of improvements made to the original program. What additional improvements can you suggest? Think about how the game could be modified to make the GUI easier and more natural.

  6. The various event classes (ActionEvent, ItemEvent, etc.) all inherit the getSource() method from a superclasss. Use the API pages to determine the name of that superclass.

  7. Write an application with a GUI that contains a choice and a text area. When the choice is activated, a message should be written to the text area, stating the choice's selected index.

    Suggested design: Your frame should contain a panel (at North) that contains the choice. The text area should be at South. If you need a guideline, the TextAreaNim program in the "Improving the GUI" section has a similar structure.

  8. Write an application with a GUI that contains a text field and a text area. When the user presses the Enter key in the text field, the text field's contents should be copied into text area, followed by a newline character.

    Your event-handling code will need to retrieve the contents of the text field. You do that by calling the text field's getText() method, which returns a string.

    Suggested design: Your frame should contain a panel at North that contains the text field. The text area should go at Center.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net