1. Add buttons "First" and "Last" to the GUI for the picture gallery
that will cause the display of the first and last image respectively.
Add appropriate methods to the ImageGallery class as needed. To
start on this exercise, first copy of the .java files from
the directory
$CISC370HOME/exampl-progs/buttons/pictureGalleryWButtons to
a subdirectory of your home directory.
To prepare programming assignments for
submission, do the following for each exercise.
script // Produces a file named typescript
whoami
date
pwd
cat *.java
javac *.java // mainFile should be the name of the file containing
java mainFile // your main() method
exit // to exit script
Now print the typescript file for submittal at the beginning
of lecture.
On strauss, you can use xv as demoed in lecture to
grab and print a copy of the graphics output from your Java programs that
should be handed-in with your assignment.
Today's Lecture Topics
- An review of containers, components, layout managers, and event
handlers (listeners)
- A
first example of an event handler for a button component
in
a frame container.
In this example, the button is a simple toggle switch that toggles
the background of the frame between black and white. This code can
be found in the directory example-progs/buttons/firstButton.
The new features/ideas
to particularly note in this example are the following:
- In the ButtonPanel constructor note the creation of the
onOffButton as an object of the JButton class and its placement
on the ButtonPanel via the add() method of the Container class,
which is inherited by the JPanel class.
- The use of the addActionListener() method in the ButtonPanel
constructor to register the onOffButton event handler with the
button.
- The actionPerformed() method of the ButtonPanel class that must
be there to implement the ActionListener interface. This method
toggles the background color between black and white and calls
the repaint() method to schedule repainting of the panel.
- The SimpleFrame class has a main() method for testing this
code. It contains nothing new that has not been used in other
frame classes in previous examples.
- A
more complex example of using button controls
. In this example,
we add two buttons, previous and next, to our picture gallery
example that allow the user to step forward and backwards through the
images. The code for this example is in the directory
example-progs/buttons/pictureGalleryWButtons.
The new features/ideas to particularly note in this example are:
- The addition of the methods decrImageNo() and incrImageNo() to
the ImageGallery class. These should have been in the
version of ImageGallery found in the directory
example-progs/pictureGallery/version2.
- In the ButtonPanel class note the two instance variables
prevButton and nextButton, their
initialization in the constructor with JButton objects, their
addition to the ButtonPanel, and the registration of the event
handler for each. Also note the implementation of the
actionPerformed() method that serves as the event handler for
both buttons. It calls the decrImageNo() or incrImageNo()
method of the ImageGallery class as is appropriate for the
button pushed. Those methods then notify the observer which
causes the new image to be displayed.
- The IGDisplayer class is the same as the one found in the
directory example-progs/pictureGallery/version2.
- The ImageFrame class contains a main method for testing the
code in this example. It initializes the URLs and titles for
the images as has been done in the previous picture gallery
examples. It then creates the image gallery, IG displayer, and
button panel objects. A frame is created and the IG displayer
and button panels are added to the frame and the frame is
shown.
Back to the
CISC 370 homepage.
This page has been accessed
times since 20 Mar 2000.
Corrections, suggestions and comments to Bob Caviness
Copyright
2000 B. F. Caviness