Upload
loreen-mosley
View
218
Download
0
Embed Size (px)
Citation preview
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
1
INTRODUCTORY PROGRAMMINGWeek 4 Lecture 2
• Fonts– Horstmann 4.6
• ActionEvents – different events from different buttons– Horstmann is too complicated
• Scope and Null pointers
• Further Programming overview
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
2
The Font Class
• Java supplies the Font class to allow text to be displayed in different fonts and styles
• A Font object has a constructor that fixes– the name, the style, the size
• Objects which contain text, such as labels and text fields, have a setFont method
• Read Horstmann 4.6 but don’t worry about the Advanced topic (accurate positioning)
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
3
Font Style and Size
• Style is fixed by one of three public static constants in the Font classFont.PLAIN, Font.BOLD, Font.ITALIC
– These are actually integers and can be added Font.BOLD + Font.ITALIC
• Size is expressed as a point size– 10 or 12 are common sizes for readable text
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
4
The Font Name
• An actual font name such as Times New Roman, Arial, Helvetica, Courier– Uses the font in the operating system
• A logical font name such as Serif, SansSerif, Monospaced. – Uses the most suitable available font
• Use a non-proportional font to control text position (used in FP1)– E.g. Courier (actual) or Monospaced (logical)– Each character takes up the same amount of space
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
5
User Interface Events
• There are many user interface events.
• Today we are interested in button clicks
• Other events might be……– Mouse pressed, released, clicked, dragged,...– Text value changed– Window closed, activated– Check box, list box, combo box changed– etc
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
6
An Event-driven program
• An event driven program waits for an event and then reacts to it– User, not programmer, controls the order in which
events occur– Natural model for a program with a GUI– The programmer specifies which events they wish to
handle– Events which are not handled are ignored
• The java.awt.event package is used to– Listen for events– Transfer control to the correct place in the program
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
7
Java Event Handling Model
Button
OperatingSystem
ActionEventListeningClass
fires action event
passes action eventdetails to
is registered to
listen forEvent source
Event handler
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
8
The event source
• The event source (e.g. Button)– Can generate the event (e.g. button clicks)– Manages the listeners - specifies which class
contains the methods which will handle the event
• The class is specified using the addActionListener method
• In our case, the method which handles the event will be in the same class i.e. the GUI class, and is referred to as this
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
9
The listeners
• The event listener class must provide standard methods to handle the event– The class does this by implementing the
appropriate Listener interface• This interface describes the required methods
• Any class that implements the interface must provide these methods
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
10
The ActionListener Interface
• An interface is used when a collection of objects need to perform some common tasks.
• In this case, the collection of objects is all the GUIs that have buttons
• The interface which specifies the required methods for button clicks is called ActionListener
• The method which handles button clicks is called actionPerformed
• All the information about the button click is provided in an ActionEvent object, which is a parameter to the actionPerformed method.
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
11
What can go wrong with listeners?
• What if you do not register the listener class with the button? (i.e .no addActionListener )
• What if your GUI class implements ActionListener, but does not have an actionPerformed method?
• What if you provide the actionPerformed method and do not implement ActionListener?
• What if you forget to import the java.awt.event package?
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
12
The event and the source
• A GUI will often have several buttons generating ActionEvents– Each button sends a package of details to the
operating system when it is clicked– These details include a reference to the object
firing the event (the event source)– The ActionEvent class has a method getSource()
• this returns the reference to that object, so you can then check which button was clicked
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
13
Summary : Enabling a GUI class to handle ActionEvents
• Import java.awt.event.*
• Add implements ActionListener to the class header
• use the addActionListener method for each button that is to fire an event:myButton.addActionListener(this)
• write an actionPerformed method containing the event handling code, using e.getSource() to determine which event occurred
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
14
Declaring a primitive data type
• When a primitive data type is declared,int num;
a small space is allocated to it. A default value is assigned in this space, which is 0 for numbers, something unprintable for characters.
• When the value is assigned to the variable (num = 3;), the default value is replaced.
num 0
num 3
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
15
Declaring an object
• When an object datatype (Traveller, JButton etc) is declared without assignment (=), a small amount of space is allocated to it to hold a ‘pointer’. – The pointer is the address in memory of the full details for
the object. E.g. JButton myButton; Traveller tourist;
– This value is initially null
myButton
Points nowhere
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
16
Assigning values to an object
• A value is assigned usually by– For objects, calling the Constructor
myButton = new JButton (“Process Now”); tourist = new Traveller(“Monica”, “Geneva”);
• Be careful not to redeclare the object when you assign it. You would then have a – A local variable within the Constructor called myButton
which has had the details (text, colour etc) assigned– AND the original instance variable myButton which still
has no details.
Points to detailsmyButton
Larger area holding details
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
17
Null Pointer Exception• If you try to refer to an OBJECT variable which has been
declared but not been initialised//declared as instance variableE.g. JLabel myLabel;//repeated declaration when laying out componentsJLabel myLabel = new JLabel(“some text”);//alteration in actionPerformed()myLabel.setFont(firstFont);
java.lang.NullPointerExceptionat ButtonGUI.changeToCourier(ButtonGUI.java:82at ButtonGUI.actionPerformed(ButtonGUI.java:71)at javax.swing.AbstractButton.fireActionPerformed
(AbstractButton.java:1786)at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed
(AbstractButton.java:1839)
Etc………..
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
18
Other scope effects
• What if a button is declared as an instance variable and declared again when laying out the components?
• What if a font is declared as an instance variable and never instantiated?
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
19
Strings (recap)
• A sequence of characters• Index numbering starts at 0• Methods to return an altered version of the String
– convert to lower case, substring
• Methods to return a value– character at a certain index– Index of a character– length of a string
J o e B l go g s
0 1 2 3 4 5 86 7 9
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
20
Arrays (Further Programming)
• A sequence of types (primitive or object)• Index numbering starts at 0• Write our own (standard) methods to
– Search for a value
– Sort into order
– Insert a value
– Delete a value
1 51 16 21 34 66 0 55
0 1 2 3 4 5 86 7 9
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
21
Graphical User Interface
• More complex layouts
Text Input/output
• Formatting input and output• Writing and reading complex lines from text
files
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
22
Foundations Programming
• lots of support from tutors and demonstrators• non-assessed exercises• multiple choice test• you can judge whether you are going to find
programming – easy
• choose other programming modules
– or hard• avoid programming-based modules• possibly only able to achieve Diploma• may need to commit more time to the course
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
23
Further Programming
• 3 Lectures per week
• Choose a tutorial time that suits you– Not in first week
• You will be reassigned to lab groups on Mon 3 -5, Tue 3- 5– Not in first week
• Study the lecture handouts and associated programs, then work on the assignments
MSc/PgDip in IT Introductory Programming: Week 4 Lecture 2
24
Assessment• 3 assessed exercises over first 6 weeks
– weight 10% each – inflexible deadlines (unless medical certificate)– marks lost by late submission– 2 hours of lab time per week when tutor/demonstrator
available
• ‘Project’– handed out in week 6, deadline during second week of
second semester– weight 35%– very little support provided
• Exam in May 35%