Title: CS288
1CS288
2Open PersonGui Files
- Go to Course web site for PersonGui source files
- http//www.computing.surrey.ac.uk/courses/cs288/Ex
amples/PersonGui - Make copies of these files in you home folder and
then open - them in some editor (e.g. Emacs)
- PersonGui.java
- PersonMaker.java
- Person.java
- images.zip(unzip this file in the same directory
as you have put the java files) - Download a copy of these slides so that you can
read them directly,and not simply view them in
Internet Explorer. This is handy as you can then
copy and paste code examples directlyfrom these
slides.
3Overview
At the moment with the PersonGui we can define
age, height, first name and last name. In this
lab you will add a new attribute, eye colour
together with all the supporting code to add a
new entry to the pop-up menu. That means there
will be a new menu item on the pop-up menu to
choose the eye colour of a person.
4Overview
So that when you choose to finally create a new
person in a JFrame, this will appear with an
extra attribute for eye colour
51 PersonMaker.java
Open PersonMaker.java in Emacs. Define a new
instance field private String eye_colour
"" in the same place as you find these
declarations private String first_name
"" private String last_name ""
Next define methods setEyeColour and
getEyeColour in just the same way as you defined
setLastName and getLastName
61 PersonMaker.java
Next change the String Make_Person method, so
that it includes information about the eye colour
of the person. Do this in exactly the same way
that the method includes the age and height of a
person. Check that this code compiles
correctly javac PersonMaker.java
72 Person.java
Next, open the Person.java file in Emacs. Change
the constructor so that it has an extra parameter
for the eye colour public Person(String
first_text, String last_text, String
age_text, String height_text, String
eye_colour_text, boolean bool_cat )
Next declare a new JLabel label_eye_colour with
all the other JLabel declarations To do this
you will have to search through the file for the
keyword JLabel until you find the line where all
the other labels such as label_height are
declared.
82 Person.java
Next find the lines of code label_height new
JLabel(height_text) set_fonts(label_height) Do
the same thing for the eye_colour_text string
and the label_eye_colour JLabel. I.e. create a
new JLabel, using the eye_colour_text string,
assign it to label_eye_colour, and then set the
fonts for this new JLabel.
Next add these lines c.gridx 4 c.gridy
0 add(label_eye_colour, c) immediately after
these lines c.gridx 3 c.gridy
0 add(label_height, c)
92 Person.java
Next change this line c.gridwidth 4 to
c.gridwidth 5
Now go back to the file PersonMaker.java. Change
the creation of Person newContentPane to take
account of the fact we have added an extra
parameter to the Person constructor method for
eye colour. Check that this code compiles
correctly javac Person.java javac
PersonMaker.java (Remember that some times it is
necessary to remove all .class files to make the
compiler properly recompile your source files)
103 PersonGui.java
Open PersonGui.java in Emacs Find the
line static final private String POPUP_2
"Surname of Person" Add a new line immediately
after this static final private String
POPUP_2_1 "Eye Colour of Person" Next find
the text menuItem new
JMenuItem(POPUP_2) menuItem.addActionList
ener(this) popup.add(menuItem) After
this add three appropriate lines so that we have
the same pattern of code here for POPUP_2_1, (so
that exactly the same is done for POPUP_2_1 as is
done for POPUP_2, and POPUP_1 for that matter).
113 PersonGui.java
Now for the harder part. Find the part of the
code else if(POPUP_2.equals(cmd))
String text textField.getText()
textField.setText("")
dude.setLastName(text)
description "The Persons Last Name is " "
\"" dude.getLastName() "\"\n" You now
have to insert code of this same pattern for
POPUP_2_1. The difference is that you will not
use setLastName and getLastName since you want to
define the eye colour of someone. Check in
PersonMaker.java to see what methods you
defined for setting and getting the eye colour
and use those methods for POPUP_2_1. In the new
code you will want the description variable to
say something about eye colour rather than the
last name.
124 Finally
Compile and run the code. Check that you get a
new entry in the JFrame that shows all the
attributes, and that the text frame in the
original JFrame shows the eye colour. Next go
back to PersonMaker.java and change the
constructor so that it include a default value
for eye colour as well as all the other
attributes.