Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class AdministrativeAdministrative –Project proposals –This week’s lab Notes.

Similar presentations


Presentation on theme: "March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class AdministrativeAdministrative –Project proposals –This week’s lab Notes."— Presentation transcript:

1 March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class AdministrativeAdministrative –Project proposals –This week’s lab Notes on Labs 18 & 19Notes on Labs 18 & 19 Making a GUI do something: EventsMaking a GUI do something: Events

2 March 2005 2R. Smith - University of St Thomas - Minnesota Upcoming Events Project Proposals Due WednesdayProject Proposals Due Wednesday –Described on the web site Tomorrow’s LabTomorrow’s Lab –MiniLab 18 Due –Lab 19 Starting

3 March 2005 3R. Smith - University of St Thomas - Minnesota Mini Lab 18 leads to Lab 19 Lab 18’s object is the start of Lab 19Lab 18’s object is the start of Lab 19 Be sure to choose the right name: ShelfEstBe sure to choose the right name: ShelfEst –If you used the wrong name, fix it –This can be a little tricky –The name is case sensitive! My experimental program will create a ShelfEstMy experimental program will create a ShelfEst –If your program works correctly, I’ll be able to create it –If not, then I can’t really test your program –You lose points!

4 March 2005 4R. Smith - University of St Thomas - Minnesota How do we make the button work? Example: swing2 is the visual interfaceExample: swing2 is the visual interface –Swing3 has the real deal In theory what has to happen?In theory what has to happen?

5 March 2005 5R. Smith - University of St Thomas - Minnesota Tying events to GUI objects Is there a way to “point” to a method to run?Is there a way to “point” to a method to run? –I.e. when we push the button, –We call the method pointed to by a button field Some languages allow thisSome languages allow this What makes an ‘object’ different from a primitive type?What makes an ‘object’ different from a primitive type?

6 March 2005 6R. Smith - University of St Thomas - Minnesota Objects provide custom methods We can define a special objectWe can define a special object –It can provide the action to perform for the button “ActionListener” Interface“ActionListener” Interface –An object that provides one method: actionPerformed We define the object in terms of ActionListenerWe define the object in terms of ActionListener Then we save the object with the buttonThen we save the object with the button –“Registering” the action listener –The actionPerformed() method is run when button is pressed –We save an object, not a method

7 March 2005 7R. Smith - University of St Thomas - Minnesota A list of specific changes Create a private “ActionListener” classCreate a private “ActionListener” class –Needs its own class name Write actionPerformed() methodWrite actionPerformed() method –Retrieves text typed in –Converts it to integers or doubles as needed –Calculates the result –Formats the answer in a text string –Puts the string in the bottom label field Use “setText()” method on the label fieldUse “setText()” method on the label field Update panel building to register the listenerUpdate panel building to register the listener –“addActionListener()” method –Passes ActionListener type object as its argument

8 March 2005 8R. Smith - University of St Thomas - Minnesota Specific example: Kilo Conversion Kcvwindow: the window-only exampleKcvwindow: the window-only example Kcvwin: complete with action listenerKcvwin: complete with action listener We define a ‘private class’ CalcListenWe define a ‘private class’ CalcListen –“Implements ActionListener” That means we can use it with Swing buttonsThat means we can use it with Swing buttons –Pass a CalcListen object to addActionListener()

9 March 2005 9R. Smith - University of St Thomas - Minnesota That’s it. Questions?Questions? Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class AdministrativeAdministrative –Project proposals –This week’s lab Notes."

Similar presentations


Ads by Google