Presentation is loading. Please wait.

Presentation is loading. Please wait.

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 21 INTRODUCTORY PROGRAMMING Week 4 Lecture 2 Fonts –Horstmann 4.6 ActionEvents –different events.

Similar presentations


Presentation on theme: "MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 21 INTRODUCTORY PROGRAMMING Week 4 Lecture 2 Fonts –Horstmann 4.6 ActionEvents –different events."— Presentation transcript:

1 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 21 INTRODUCTORY PROGRAMMING Week 4 Lecture 2 Fonts –Horstmann 4.6 ActionEvents –different events from different buttons –Horstmann is too complicated Scope and Null pointers Further Programming overview

2 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 22 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)

3 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 23 Font Style and Size Style is fixed by one of three public static constants in the Font class Font.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

4 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 24 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

5 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 25 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

6 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 26 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

7 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 27 Java Event Handling Model Button Operating System ActionEvent Listening Class fires action event passes action event details to is registered to listen for Event source Event handler

8 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 28 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

9 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 29 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

10 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 210 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.

11 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 211 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 actionPerforme d method and do not implement ActionListener ? What if you forget to import the java.awt.event package?

12 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 212 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

13 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 213 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

14 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 214 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 3

15 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 215 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

16 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 216 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 details myButton Larger area holding details

17 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 217 Null Pointer Exception If you try to refer to an OBJECT variable which has been declared but not been initialised //declared as instance variable E.g. JLabel myLabel; //repeated declaration when laying out components JLabel myLabel = new JLabel(“some text”); //alteration in actionPerformed() myLabel.setFont(firstFont); java.lang.NullPointerException at ButtonGUI.changeToCourier(ButtonGUI.java:82 at ButtonGUI.actionPerformed(ButtonGUI.java:71) at javax.swing.AbstractButton.fireActionPerformed (AbstractButton.java:1786) at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed (AbstractButton.java:1839) Etc………..

18 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 218 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?

19 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 219 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 Joe Blgogs 0123458679

20 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 220 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 0123458679

21 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 221 Graphical User Interface More complex layouts Text Input/output Formatting input and output Writing and reading complex lines from text files

22 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 222 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

23 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 223 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

24 MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 224 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%


Download ppt "MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 21 INTRODUCTORY PROGRAMMING Week 4 Lecture 2 Fonts –Horstmann 4.6 ActionEvents –different events."

Similar presentations


Ads by Google