|
|
|
|
Exercises
Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, available for a small fee from www.BruceEckel.com.
- Create an applet/application using the Console class as shown in
this chapter. Include a text field and three buttons. When you press each
button, make some different text appear in the text field.
- Add a check box to the applet created in Exercise 1, capture the event, and
insert different text into the text field.
- Create an applet/application using Console. In the JDK documentation
from java.sun.com, find the JPasswordField and add this to the
program. If the user types in the correct password, use Joptionpane to
provide a success message to the user.
- Create an applet/application using Console, and add all the Swing
components that have an addActionListener( ) method. (Look these up
in the JDK documentation from java.sun.com. Hint: use the index.) Capture
their events and display an appropriate message for each inside a text field.
- Create an applet/application using Console, with a JButton
and a JTextField. Write and attach the appropriate listener so that if
the button has the focus, characters typed into it will appear in the
JTextField.
- Create an applet/application using Console. Add to the main frame
all the components described in this chapter, including menus and a dialog box.
- Modify TextFields.java so that the characters in t2 retain
the original case that they were typed in, instead of automatically being forced
to upper case.
- Locate and download one or more of the free GUI builder development
environments available on the Internet, or buy a commercial product. Discover
what is necessary to add BangBean to this environment and to use it.
- Add Frog.class to the manifest file as shown in this chapter and run
jar to create a JAR file containing both Frog and BangBean.
Now either download and install the Bean Builder from Sun, or use your own
Beans-enabled program builder tool and add the JAR file to your environment so
you can test the two Beans.
- Create your own JavaBean called Valve that contains two properties:
a boolean called “on” and an int called
“level.” Create a manifest file, use jar to package your
Bean, then load it into the Bean Builder or into a Beans-enabled program builder
tool so that you can test it.
- Modify MessageBoxes.java so that it has an individual
ActionListener for each button (instead of matching the button text).
- Monitor a new type of event in TrackEvent.java by adding the new
event handling code. You’ll need to discover on your own the type of event
that you want to monitor.
- Inherit a new type of button from JButton. Each time you press this
button, it should change its color to a randomly selected value. See
ColorBoxes.java for an example of how to generate a random color value.
- Modify TextPane.java to use a JTextArea instead of a
JTextPane.
- Modify Menus.java to use radio buttons instead of check boxes on the
menus.
- Simplify List.java by passing the array to the constructor and
eliminating the dynamic addition of elements to the list.
- Modify SineWave.java to turn SineDraw into a JavaBean by
adding “getter” and “setter” methods.
- Remember the “sketching box” toy with two knobs, one that
controls the vertical movement of the drawing point, and one that controls the
horizontal movement? Create one of those, using SineWave.java to get you
started. Instead of knobs, use sliders. Add a button that will erase the entire
sketch.
- Starting with SineWave.java, create a program (an applet/application
using the Console class) that draws an animated sine wave that appears to
scroll past the viewing window like an oscilloscope, driving the animation with
a Thread. The speed of the animation should be controlled with a
java.swing.JSlider control.
- Modify Exercise 19 so that multiple sine wave panels are created within the
application. The number of sine wave panels should be controlled by HTML tags or
command-line parameters.
- Modify Exercise 19 so that the java.swing.Timer class is used to
drive the animation. Note the difference between this and
java.util.Timer.
- Create an “asymptotic progress indicator” that gets slower and
slower as it approaches the finish point. Add random erratic behavior so it will
periodically look like it’s starting to speed up.
- Modify Progress.java so that it does not share models, but instead
uses a listener to connect the slider and progress bar.
- Follow the instructions in the section titled “Packaging an applet
into a JAR file” to place TicTacToe.java into a JAR file. Create an
HTML page with the simple version of the applet tag along with the archive
specification to use the JAR file. Run HTMLconverter on file to produce a
working HTML file.
- Create an applet/application using Console. This should have three
sliders, one each for the red, green, and blue values in java.awt.Color.
The rest of the form should be a JPanel that displays the color
determined by the three sliders. Also include non-editable text fields that show
the current RGB values.
- In the JDK documentation for javax.swing, look up the
JColorChooser. Write a program with a button that brings up the color
chooser as a dialog.
- Almost every Swing component is derived from Component, which has a
setCursor( ) method. Look this up in the JDK documentation. Create
an applet and change the cursor to one of the stock cursors in the Cursor
class.
- Starting with ShowAddListeners.java, create a program with the full
functionality of c10:ShowMethods.java.
- Turn c12:TestRegularExpression.java into an interactive Swing
program that allows you to put an input string in one TextArea and a
regular expression in a TextField. The results should be displayed in a
second TextArea.
- Modify InvokeLaterFrame.java to use
invokeAndWait( ).
|
|
|