Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 261 CS110 Lecture 26 Thursday, May 6, 2004 This lecture not delivered from slides Announcements –final exam Thursday, May 20, 8:00 AM McCormack,

Similar presentations


Presentation on theme: "Lecture 261 CS110 Lecture 26 Thursday, May 6, 2004 This lecture not delivered from slides Announcements –final exam Thursday, May 20, 8:00 AM McCormack,"— Presentation transcript:

1 Lecture 261 CS110 Lecture 26 Thursday, May 6, 2004 This lecture not delivered from slides Announcements –final exam Thursday, May 20, 8:00 AM McCormack, Floor 01, Room 0608 –wise1 due next Tuesday Agenda –Questions (WISE and otherwise) –GUI programming –Interfaces

2 Lecture 262 GUI Programming Fun –some introductory courses start here –requires hardware support (PCs) –maybe more in CS110 in time GUI syntax: what windows look like - containers and components GUI semantics: how windows behave – event driven programming System does lots of work for you Learn the APIs, use software tools

3 Lecture 263 10/joi

4 Lecture 264 GUI Syntax (what windows look like) AWT (Abstract Windowing Toolkit) Abstract class Container –subclasses: Panel, Window, Frame –methods: add, setLayout Abstract class Component –subclasses: Button, Checkbox, Textfield, Up to date Java GUI programmers use swing: –classes: JButton, JTextfield,… or they use GUI builders

5 Lecture 265 Building window look and feel class JOIPanel extends Applet extends Panel init method creates a button and adds it to this Panel, sets font: 38 button = new Button( "Press Me" ); 39 this.add( button ); 40 font = new Font("Garamond", Font.BOLD, 48);

6 Lecture 266 Building window behavior Add a listener to the button 43 button.addActionListener( new JOIButtonListener( this ) ); When button is pressed, system sends an actionPerformed message to the listener To see what happens, look at method actionPerformed in class JOIButtonListener

7 Lecture 267 JOIButtonListener // constructor remembers the Panel 27 public JOIButtonListener ( JOIPanel panel ) 28 { 29 this.panel = panel; 30 } // send panel a changeMessage message now! 41 public void actionPerformed ( ActionEvent e ) 42 { 43 panel.changeMessage(); 44 }

8 Lecture 268 Changing the message In JOIPanel: change the message and ask the system to repaint: 51 public void changeMessage() 52 { 53 currentMessage = 54 currentMessage.equals(MESSAGE1) ? MESSAGE2 : MESSAGE1; 55 this.repaint(); 56 }

9 Lecture 269 Changing the message repaint( ) (which is really super.repaint( )) invokes paint( ) A Graphics object is like a programmable pen or paintbrush 67 public void paint(Graphics g) 68 { 69 g.setColor(Color.black); 70 g.setFont(font); 71 g.drawString(currentMessage, 40, 75); 72 }

10 Lecture 2610 Running the program as an application > java JOIPanel public static void main( String[] args ) { Terminal t = new Terminal(); Frame frame = new Frame(); JOIPanel panel = new JOIPanel(); panel.init(); frame.add(panel); frame.setSize(400,120); frame.show(); t.readLine("return to close window "); System.exit(0); }

11 Lecture 2611 Running the program as an applet from a browser: file joi.html This file is written in html (“hypertext markup language”), not Java. Browsers understand html. main method never runs. Execution starts with init.

12 Lecture 2612 Interfaces An interface is like a very abstract class –no fields –only abstract methods A class that implements an interface promises to implement its abstract methods public class JOIButtonListener implements ActionListener

13 Lecture 2613

14 Lecture 2614 All together now public class JOIApplet extends Applet implements ActionListener {... public void actionPerformed ( ActionEvent e ) { currentMessage = currentMessage.equals(MESSAGE1) ? MESSAGE2 : MESSAGE1; this.repaint(); }

15 Lecture 2615 Interfaces in Collection framework Inheritance hierarchy (classes) java.lang.Object java.util.AbstractMap java.util.TreeMap TreeMap implements interfaces: –Cloneable, Map, Serializable, SortedMap AbstractMap implements Map Map foo = new TreeMap( ) // legal

16 Lecture 2616 Juno 10 has a GUI Juno CLI prompts for input when it wants it => the program is in control Juno GUI sits waiting for user input from anywhere mouse can go, then takes action => the user is in control (event driven) > java Juno –g.

17 Lecture 2617 java Juno -e -g


Download ppt "Lecture 261 CS110 Lecture 26 Thursday, May 6, 2004 This lecture not delivered from slides Announcements –final exam Thursday, May 20, 8:00 AM McCormack,"

Similar presentations


Ads by Google