cs 106a, lecture 23 gcanvasand interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1....

56
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, and others. CS 106A, Lecture 23 GCanvas and Interactors suggested reading: Java Ch. 10.5-10.6

Upload: others

Post on 23-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

Thisdocumentiscopyright(C)StanfordComputerScienceandMartyStepp,licensedunderCreativeCommonsAttribution2.5License.Allrightsreserved.BasedonslidescreatedbyKeithSchwarz,MehranSahami,EricRoberts,StuartReges,andothers.

CS106A,Lecture23GCanvas andInteractors

suggestedreading:JavaCh.10.5-10.6

Page 2: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

2

Plan for today•Recap:Inheritance•ExtendingGCanvas•Example:Aquarium•Interactors–JButton–JLabel–JTextField

•Example:TipCalculator

Page 3: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

3

Plan for today•Recap:Inheritance•ExtendingGCanvas•Example:Aquarium•Interactors–JButton–JLabel–JTextField

•Example:TipCalculator

Page 4: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

4

Inheritance

Inheritanceletsusrelateourvariable

typestooneanother.

Page 5: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

5

Inheritance

Employee

Programmer

KarelProgrammer

Variabletypescanseemto“inherit”fromeachother.Wedon’twanttohavetoduplicatecodeforeachone!

Page 6: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

6

Using Inheritancepublic class Name extends Superclass {

– Example:

public class Programmer extends Employee {...

}

• ByextendingEmployee,thistellsJavathatProgrammer candoeverythinganEmployeecando,plusmore.

• ProgrammerautomaticallyinheritsallofthecodefromEmployee!• ThesuperclassisEmployee,thesubclass isProgrammer.

Page 7: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

7

Example: Programmerpublic class Programmer extends Employee {

private int timeCoding;...public void code() {

timeCoding += 10;}

}

...

Programmer rishi = new Programmer(“Rishi”);rishi.code(); // from Programmerrishi.promote(); // from Employee!

Page 8: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

8

Example: KarelProgrammerpublic class KarelProgrammer extends Programmer {

private int numBeepersPicked();...public void pickBeepers() {

numBeepersPicked += 2;}

}

...KarelProgrammer nick = new KarelProgrammer(“Nick”);nick.pickBeepers(); // from KarelProgrammernick.code(); // from Programmer!nick.promote(); // From Employee!

Page 9: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

9

Plan for today•Recap:Inheritance•ExtendingGCanvas•Example:Aquarium•Interactors–JButton–JLabel–JTextField

•Example:TipCalculator

Page 10: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

10

GCanvas• AGCanvas isthecanvasareathatdisplaysallgraphicalobjectsinaGraphicsProgram.

• WhenyoucreateaGraphicsProgram,itautomaticallycreatesaGCanvas foritself,putsitonthescreen,andusesittoaddallgraphicalshapes.

• GCanvas istheonethatcontainsmethodslike:– getElementAt– add– getWidth– getHeight– …

Page 11: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

11

GCanvaspublic class Graphics extends GraphicsProgram {

public void run() {// A GCanvas has been created for us!GRect rect = new GRect(50, 50);add(rect); // adds to the GCanvas!

...// Checks our GCanvas for elements!GObject obj = getElementAt(25, 25);

}}

Page 12: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

12

Extending GCanvaspublic class Graphics extends Program {

public void run() {// We have to make our own GCanvas nowMyCanvas canvas = new MyCanvas();add(canvas);

// Operate on this canvasGObject obj = canvas.getElementAt(…);

}}

Page 13: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

13

Extending GCanvaspublic class MyCanvas extends GCanvas {

public void addCenteredSquare(int size) {GRect rect = new GRect(size, size);int x = getWidth() / 2.0 –

rect.getWidth() / 2.0;int y = getHeight() / 2.0 –

rect.getHeight() / 2.0;add(rect, x, y);

}}

Page 14: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

14

Extending GCanvaspublic class Graphics extends Program {

public void run() {// We have to make our own GCanvas nowMyCanvas canvas = new MyCanvas();add(canvas);

canvas.addCenteredSquare(20);}

}

Page 15: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

15

Extending GCanvas•Sometimes,wewanttobeabletohaveallofourgraphics-relatedcodeinaseparatefile.

•Todothis,insteadofusingtheprovidedGraphicsProgram canvas,wedefineourownsubclassofGCanvas, haveourprogramextendProgram,andaddourowncanvasourselves.

•Then,allgraphics-relatedcodecangoinourGCanvas subclass.

Page 16: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

16

The init method• init isaspecialpublicmethod,likerun,thatiscalledwhenyourprogramisbeinginitialized.

• Unlikerun,however,itiscalledbefore yourprogramlaunches,lettingyoudoanyinitializationyouneed.

public class MyProgram extends GraphicsProgram {public void init() {

// executed before program launches}

public void run() {// executed after program launches

}}

Page 17: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

17

The init method• init istypicallyusedtoinitializegraphicalcomponents,suchasaddingacustomGCanvas tothescreen.

public class MyProgram extends Program {private MyCanvas canvas;public void init() {

canvas = new MyCanvas();add(canvas);

}

public void run() {canvas.addCenteredSquare(20);

}}

Page 18: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

18

Common Bugs• Whenyouareusingacustomcanvas,makesuretonotcallgetWidth orgetHeight onthecanvasuntilitisshownonscreen!

public class MyProgram extends Program {private MyCanvas canvas;public void init() {

// canvas not created yet!canvas = new MyCanvas();// canvas not added yet!add(canvas);// window not showing yet!

}

public void run() {// good to go

}}

Page 19: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

19

Plan for today•Recap:Inheritance•ExtendingGCanvas•Example:Aquarium•Interactors–JButton–JLabel–JTextField

•Example:TipCalculator

Page 20: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

20

Example: Aquarium• Let’swriteagraphicalprogramcalledAquarium thatsimulatesfishswimmingaround.

• Todecomposeourcode,wecanmakeourownGCanvas subclass.

Page 21: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

21

Plan for today•Recap:Inheritance•ExtendingGCanvas•Example:Aquarium•Interactors–JButton–JLabel–JTextField

•Example:TipCalculator

Page 22: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

22

Interactive ProgramsSofar,wehavelearnedabouttwowaysofmakinginteractiveprograms:• ReadinguserinputinConsolePrograms•DetectingmouseeventsinGraphicsPrograms

Today,we’regoingtolearnaboutathird:interactors.

Page 23: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

23

Interactors

Page 24: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

24

Interactors

JComponent

JButton

JTextFieldJLabel

Page 25: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

25

Plan for today•Recap:Inheritance•ExtendingGCanvas•Example:Aquarium•Interactors–JButton–JLabel–JTextField

•Example:TipCalculator

Page 26: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

26

JButton

Page 27: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

27

JButtonimport java.awt.event.*;import javax.swing.*;

JButton button = new JButton(“Press me”);add(button, SOUTH);

Page 28: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

28

Window Regions• Ingraphicsorconsoleprograms,thewindowisdividedintofiveregions:

• TheCENTER regionistypicallywheretheactionhappens.– ConsoleProgram addsaconsolethere– GraphicsProgram putsaGCanvas there

• Otherregionsarevisibleonlyifyouaddaninteractortothemusingadd(component, REGION);

• Interactorsareautomaticallycenteredwithineachregion.

Page 29: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

29

Responding To Button ClicksTorespondtoeventsfrominteractors,wemustdothefollowing:1. CalladdActionListeners() attheendofinit,onceweare

doneaddingbuttons.ThistellsJavatoletusknowifanyofthepreviousbuttonswereclicked.

2. ImplementthepublicactionPerformedmethod.Thismethodiscalledwheneverabuttonisclicked.

Page 30: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

30

JButton Examplepublic class Interactors extends ConsoleProgram {

public void init() {JButton yayButton = new JButton("Yay");add(yayButton, SOUTH);JButton nayButton = new JButton("Nay");add(nayButton, SOUTH);addActionListeners();

}

public void actionPerformed(ActionEvent event) {... // ?

}}

Page 31: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

31

ActionEvent• TheActionEvent parametercontainsusefuleventinformation.

– UsegetSource orgetActionCommand tofigureoutwhatbuttonorcomponentwasinteractedwith.

public void actionPerformed(ActionEvent event) {String command = event.getActionCommand();if (command.equals("Save File")) {

// user clicked the Save File button...

}}

Method Descriptione.getActionCommand() atextdescriptionoftheevent

(e.g.thetextofthebuttonclicked)e.getSource() theinteractorthatgeneratedtheevent

Page 32: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

32

JButton Example

Page 33: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

33

JButton Examplepublic class Interactors extends ConsoleProgram {

private JButton yayButton;private JButton nayButton;public void init() {

yayButton = new JButton("Yay");add(yayButton, SOUTH);nayButton = new JButton("Nay");add(nayButton, SOUTH);addActionListeners();

}

public void actionPerformed(ActionEvent event) {if (event.getSource() == yayButton) {

println("Yay");} else if (event.getSource() == nayButton) {

println("Nay");}

}}

Page 34: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

34

JButton Examplepublic class Interactors extends ConsoleProgram {

private JButton yayButton;private JButton nayButton;public void init() {

yayButton = new JButton("Yay");add(yayButton, SOUTH);nayButton = new JButton("Nay");add(nayButton, SOUTH);addActionListeners();

}

public void actionPerformed(ActionEvent event) {if (event.getSource() == yayButton) {

println("Yay");} else if (event.getSource() == nayButton) {

println("Nay");}

}}

Page 35: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

35

JButton Examplepublic class Interactors extends ConsoleProgram {

private JButton yayButton;private JButton nayButton;public void init() {

yayButton = new JButton("Yay");add(yayButton, SOUTH);nayButton = new JButton("Nay");add(nayButton, SOUTH);addActionListeners();

}

public void actionPerformed(ActionEvent event) {if (event.getSource() == yayButton) {

println("Yay");} else if (event.getSource() == nayButton) {

println("Nay");}

}}

Page 36: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

36

JButton Examplepublic class Interactors extends ConsoleProgram {

private JButton yayButton;private JButton nayButton;public void init() {

yayButton = new JButton("Yay");add(yayButton, SOUTH);nayButton = new JButton("Nay");add(nayButton, SOUTH);addActionListeners();

}

public void actionPerformed(ActionEvent event) {if (event.getSource() == yayButton) {

println("Yay");} else if (event.getSource() == nayButton) {

println("Nay");}

}}

Page 37: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

37

JButton Examplepublic class Interactors extends ConsoleProgram {

private JButton yayButton;private JButton nayButton;public void init() {

yayButton = new JButton("Yay");add(yayButton, SOUTH);nayButton = new JButton("Nay");add(nayButton, SOUTH);addActionListeners();

}

public void actionPerformed(ActionEvent event) {if (event.getSource() == yayButton) {

println("Yay");} else if (event.getSource() == nayButton) {

println("Nay");}

}}

Page 38: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

38

JButton Examplepublic class Interactors extends ConsoleProgram {

private JButton yayButton;private JButton nayButton;public void init() {

yayButton = new JButton("Yay");add(yayButton, SOUTH);nayButton = new JButton("Nay");add(nayButton, SOUTH);addActionListeners();

}

public void actionPerformed(ActionEvent event) {if (event.getSource() == yayButton) {

println("Yay");} else if (event.getSource() == nayButton) {

println("Nay");}

}}

Page 39: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

39

JButton Example #2public class Interactors extends ConsoleProgram {

private JButton yayButton;private JButton nayButton;public void init() {

JButton yayButton = new JButton("Yay");add(yayButton, SOUTH);JButton nayButton = new JButton("Nay");add(nayButton, SOUTH);addActionListeners();

}

public void actionPerformed(ActionEvent event) {if (event.getActionCommand().equals("Yay")) {

println("Yay");} else if (event.getActionCommand().equals("Nay")) {

println("Nay");}

}}

Page 40: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

40

JButton Example #2public class Interactors extends ConsoleProgram {

private JButton yayButton;private JButton nayButton;public void init() {

JButton yayButton = new JButton("Yay");add(yayButton, SOUTH);JButton nayButton = new JButton("Nay");add(nayButton, SOUTH);addActionListeners();

}

public void actionPerformed(ActionEvent event) {println(event.getActionCommand());

}}

Page 41: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

41

Plan for today•Recap:Inheritance•ExtendingGCanvas•Example:Aquarium•Interactors–JButton–JLabel–JTextField

•Example:TipCalculator

Page 42: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

42

JLabel

JLabel label = new JLabel("Hello, world!");add(label, SOUTH);

Page 43: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

43

Plan for today•Recap:Inheritance•ExtendingGCanvas•Example:Aquarium•Interactors–JButton–JLabel–JTextField

•Example:TipCalculator

Page 44: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

44

JTextField

JTextField field = new JTextField(10);add(field, SOUTH);

Page 45: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

45

JTextField

JTextField field = new JTextField(10);add(field, SOUTH);

// Set the text in the text fieldfield.setText("Hello!");

// Get the text currently in the text fieldString text = field.getText();

Page 46: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

46

JTextField Example

Page 47: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

47

JTextField Examplepublic class Interactors extends ConsoleProgram {

private JTextField textField;public void init() {

textField = new JTextField(10);add(textField, SOUTH);JButton goButton = new JButton("Go");add(goButton, SOUTH);addActionListeners();

}

public void actionPerformed(ActionEvent event) {println(textField.getText());

}}

Page 48: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

48

Detecting ENTER PressedDetectingtheENTERkeypressedinaJTextField requiresextrawork.

JTextField field = new JTextField(10);// Tells Java to listen for ENTER on the text fieldfield.addActionListener(this);// Sets the action command (like JButtons) to “Go”field.setActionCommand("Go");add(field, SOUTH);

Page 49: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

49

Detecting ENTER PressedDetectingtheENTERkeypressedinaJTextField requiresextrawork.

JTextField field = new JTextField(10);field.addActionListener(this);field.setActionCommand("Go");add(field, SOUTH);

...public void actionPerformed(ActionEvent event) {

if (event.getActionCommand().equals(“Go”)) {...

}}

Page 50: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

50

getActionCommand

Oftentimes,atextfieldhasa“corresponding”buttonthattakesactionwiththeenteredtext.Ifwesetthetextfield’sactioncommandtobethesame asitscorrespondingbutton,wecancheckfor

bothaclickandENTERatonce!

Page 51: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

51

getActionCommandpublic void init() {

JButton button = new JButton("Go");add(button, SOUTH);JTextField field = new JTextField(10);field.addActionListener(this);field.setActionCommand("Go");add(field, SOUTH);addActionListeners();

}

public void actionPerformed(ActionEvent event) {if (event.getActionCommand().equals(“Go”)) {

...}

}

Page 52: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

52

getActionCommandpublic void init() {

JButton button = new JButton("Go");add(button, SOUTH);JTextField field = new JTextField(10);field.addActionListener(this);field.setActionCommand("Go");add(field, SOUTH);addActionListeners();

}

public void actionPerformed(ActionEvent event) {if (event.getActionCommand().equals(“Go”)) {

...}

}

Page 53: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

53

Plan for today•Recap:Inheritance•ExtendingGCanvas•Example:Aquarium•Interactors–JButton–JLabel–JTextField

•Example:TipCalculator

Page 54: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

54

Practice: TipCalculatorLet’swriteaprogramcalledTipCalculator thatusesinteractorstocalculatethetipforabill.

Page 55: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

55

Practice: TipCalculatorLet’swriteaprogramcalledTipCalculator thatusesinteractorstocalculatethetipforabill.• Theprogramshouldcalculatetheappropriatetipdependingonthebuttontheuserclickson

• Theconsoleshouldclearwhenanewtipiscalculated(hint:useclearConsole()).

• ConvertastringintoadoubleusingDouble.parseDouble(str);

Page 56: CS 106A, Lecture 23 GCanvasand Interactorsweb.stanford.edu/class/archive/cs/cs106a/cs106a... · 1. Call addActionListeners()at the end of init, once we are done adding buttons. This

56

Recap•Recap:Inheritance•ExtendingGCanvas•Example:Aquarium•Interactors:JButton,JLabel,JTextField•Example:TipCalculator

Nexttime:NameSurfer overview