Lecture 9 Object Oriented Programming in Java Advanced
Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing Toolkit (AWT) June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 1
Today’s Lecture • AWT Fundamentals – http: //developer. java. sun. com/developer/online. Training/ June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 2
History • AWT in JDK 1. 0 • AWT JDK 1. 1 • JFC/Swing June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 3
Purpose of AWT • Build Graphical User Interfaces • Keep native platform look • Uses native platform API June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 4
Hierarchy of GUI Components – superclass to all classes is Component – Component • • Button Canvas Checkbox Choice Label Text. Field Container – – Panel SCroll. Pan Window Frame • List • Scroll. Bar June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 5
Containers • • containers group widgets or other containers Containers can embed other containers can have a particular layout examples: – Panel – Applet – Frame June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 6
Widgets • • • Button Canvas Label Checkbox Choice List Text. Field Text. Area examples: – Button b = new Button(); – Label l = new Label(“OK”); June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 7
Event Handlers • Event handlers are registered with Components and handle events that are generated – mouse clicks – keyboard keys – windows events • Event handlers often implement Action. Listener • example: – Action. Listener the. Listener = new My. Listener(); – Button b = new Button(); – b. add. Action. Listener(the. Listener); June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 8
How to build a GUI • • • Add components to a Container Create Event handler classes Setup event handlers to respond to clicks, . . . Display the GUI Therefore you always need: – one or more containers – one or more Components – one or more Event Handler June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 9
Extend the Applet class import java. awt. Button; import java. applet. Applet; public class The. Applet extends Applet { public void init() { // add() one or more widgets to the applet itself // create an event handler for a widget } } June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 10
Add a widget to the Applet • Example: //Create the widget Button a. Button = new Button(“some text”); // add(Component c) is a method of Container add(a. Button); June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 11
Create an Action. Listener import java. awt. event. Action. Listener; import java. awt. event. Action. Event; import java. awt. Component; public class Beeper implements Action. Listener { public void action. Performed(Action. Event event) { // get the conponent which initiated the event Component c = (Component)event. get. Source(); // beep c. get. Toolkit(). beep(); } } June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 12
Create HTML <html> <applet code=AButton. class width=100 height=100> </applet> </html> June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 13
View the file • Use the appletviewer • or use your browser to bring up the html file June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 14
Standalone GUI • • • Create a class with a main method Create a Frame: f = new Frame(“frame name”); Create a Button and add it to the frame Create an Action. Listener class (reuse Beeper) Register the event handlers with the button Bring up the frame: f. pack(); f. show(); June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 15
Enhanced standalone GUI • Use Button class’ set. Action. Command(String s) method • This method gives a name to the click “action” of the button • example: – b. set. Action. Command(“QUIT”) • Add a few more buttons to your frame and give them various actions June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 16
Enhanced Action Listener • Within the action. Performed(Action. Event event) method, check for which action command is performed • 1) Use the Action. Event method: get. Action. Command() • 2) check to see which event was raised • 3) when event “QUIT” perform a System. exit(0); June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 17
Layouts • Layouts define how the components are laid within the containers • You have used a default layout all along • You can assign a layout explicitly • example – Frame f = new frame(“frame name”); – f. set. Layout(new Grid. Layout(3, 2)); June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 18
Types of layouts • example – – – Grid. Layout Flow. Layout Card. Layout Border. Layout Grid. Bag. Layout June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 19
Nested Classes • Nested Classes are a powerful feature of Java starting with JDK 1. 1 • with nested classes, Java lets you define a class as a member of another class. Such a class is a nested class • Nested class can see all of the class members of its enclosing class, even the private ones June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 20
Nested Class Enclosing. Class{ class Inner. Class{. . . } } Enclosing. Class Inner. Class An inner class is a nested class whose instance exists within an instance of its enclosing class and has direct access to the instance members of its enclosing instance June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 21
Use Inner Classes for Listeners • Create a class that extends Frame • Add Components to the frame in the constructor • Define an inner class which extends Action. Listener • Instantiate the inner class within the frame’s constructor: the inner class now has access to the Frame’s variables June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 22
Example of Inner Class import java. awt. *; import java. awt. event. *; public class App. Frame extends Frame { Button b 1, b 2; public App. Frame(String title) { super(title); // instantiate buttons … // instantiate inner class Action. Listener al = new My. Handler(); b 1. add. Action. Listener(al); } class My. Handler extends Action. Listener { public void action. Performed(Action. Event event) { // get the conponent which initiated the event Component c = (Component)event. get. Source(); if (c==b 1) { //do this…} if (c==b 2) { // do that. . . } } } June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 23
- Slides: 23