- Examples
from lecture.
- Study TextForm.java.
In main there is a JFrame. To it are added two JPanels:
The north one is the main form of type TextForm and the south one is a panel containing
the submit button. A TextForm is itself a JPanel consisting
of two JPanels: labelPanel (put in the west) and fieldPanel
put in the center. These two panels have both been give a GridLayout
with one horizontal and vertical components, where is the number of
fields. In the loop in the constructor of TextForm,
the labels are added in turn to
labelPanel and the JTextFields are added first to the left
(change this to RIGHT to see what happens) of a
new panel p and then p is added to the fieldPanel.
( The JTextFields are not added directly to fieldPanel, but first put
in panels of their own.)
- Work on a Graphical User Interface version of Challenge 5: How old are
you? Hint: A good way to do this would be to have three JTextfields for the
user to enter her day month and year of birth. You can use a JOptionPane for
displaying the user's age.(See Deitel and Deitel Pages 569-571)
Try and amend TextForm.java
to do this.
- Work on a Graphical User Interface version of Challenge 6: Guessing Game.
Hint: For this program it would be good to have three JButtons: high, low and
correct. This will enable the user to respond to the computer's guess.
- Work on a Graphical User Interface version of Challenge 9: Hangman.
Hint: For this program it would be good to have buttons for each letter
(use a grid layout) and
a canvas similar to the traffic lights for drawing the hanging man.
Each time a button is pressed it either fills in a blank or draws the
next component of the hanging man. A good solution will also choose a word
randomly from a large list of words. I also used buttons for the word being
guessed. Initially each of these buttons were labelled with - to represent
blank. As the game progresses the labels of the buttons were changed to
represent
correct letter being pressed. Each time a letter button s pressed it is then
disabled to let the user know that this letter has already been guessed.
s.danicic@gold.ac.uk
Sebastian Danicic BSc MSc PhD (Reader in Computer Science)
Dept of Computing, Goldsmiths, University of London, London SE14 6NW
Last updated 2015-09-04