Graphical User Interfaces GUI Abstract Windows Toolkit AWT
Graphical User Interfaces (GUI) Abstract Windows Toolkit (AWT): java. awt • GUI elements: Primitive Button, Label, Checkbox, Scrollbar, etc. Container Panel, Frame, Dialog, etc. • Layout managers: Flow. Layout, Border. Layout, etc. • Supporting classes: Event handling java. awt. event package Graphics Color, Font, Graphics, etc. Geometry Point, Rectangle, Dimension, etc. Imaging Image class and java. awt. image package
The Component Hierarchy
The Swing Components
Layout Managers • The layout of the elements in a container is handled by the layout manager associated with the container. • Relative positions of the elements are specified, not their absolute coordinates. • The positions and sizes of the element will be automatically adjusted when the window is resized.
The Layout Manager Hierarchy
Buttons and Flow Layout width=400 height=50 width=100 height=120
Buttons and Flow Layout (cont'd) Layout elements in horizontal rows. import java. awt. *; import java. applet. Applet; public class Flow extends Applet { public Flow () { set. Layout(new Flow. Layout()); add(new Button("Java")); add(new Button("C++")); add(new Button("Perl")); add(new Button("Ada")); add(new Button("Smalltalk")); add(new Button("Eiffel")); } }
Border Layout
Border Layout (cont'd) import java. awt. *; import java. applet. Applet; public class Border extends Applet { public Border () { set. Layout(new Border. Layout()); add(new Button("North"), Border. Layout. NORTH); add(new Button("South"), Border. Layout. SOUTH); add(new Button("East"), Border. Layout. EAST); add(new Button("West"), Border. Layout. WEST); add(new Button("Center"), Border. Layout. CENTER); } }
Grid Layout row=3 col=2 row=1 col=0 row=0 col=1
Grid Layout (cont'd) import java. awt. *; import java. applet. Applet; public class Grid extends Applet { public void init () { int row = 0, col = 0; String att = get. Parameter("row"); if (att != null) row = Integer. parse. Int(att); att = get. Parameter("col"); if (att != null) col = Integer. parse. Int(att); if (row == 0 && col == 0) { row = 3; col = 2; }
Grid Layout (cont'd) (class Grid continued. ) } } set. Layout(new Grid. Layout(row, col)); add(new Button("Java")); add(new Button("C++")); add(new Button("Perl")); add(new Button("Ada")); add(new Button("Smalltalk")); add(new Button("Eiffel"));
Nested Panels
Nested Panels (cont'd) public class Nested. Panels extends Applet { protected Label message. Bar; protected Choice choice; public Nested. Panels () { // set up the center panel Panel center = new Panel(); center. set. Layout(new Border. Layout()); center. add(new Button("south"), Border. Layout. SOUTH); center. add(new Button("north"), Border. Layout. NORTH); center. add(new Button("east"), Border. Layout. EAST); center. add(new Button("west"), Border. Layout. WEST); center. add(new Button("center"), Border. Layout. CENTER);
(class Nested. Panels continued. ) // set up the south panel Panel south = new Panel(); south. set. Layout(new Flow. Layout()); south. add(new Button("Help")); choice = new Choice(); choice. add. Item("one"); choice. add. Item("two"); choice. add. Item("three"); choice. add. Item("four"); choice. add. Item("five"); south. add(choice); message. Bar = new Label("This is a message bar. "); south. add(message. Bar);
Nested Panels (cont'd) (class Nested. Panels continued. ) // set up the outer panel set. Layout(new Border. Layout()); add(new Button("North"), Border. Layout. NORTH); add(new Button("East"), Border. Layout. EAST); add(new Button("West"), Border. Layout. WEST); add(south, Border. Layout. SOUTH); add(center, Border. Layout. CENTER); } }
Event Handling • Event source: buttons, checkboxes, choices listener: any class interested in handling certain events • A listener must • implement an appropriate listener interface; • inform the source that it is interested in handling type of events. • A a certain listener may listen to several sources and different types of events. • The source may also be the listener. • Listeners can be full-fledged classes or inner classes.
The Event Object and Listener Classes Action. Event Item. Event Mouse. Event Key. Event Window. Event Action. Listener Item. Listener Mouse. Motion. Listener Mouse. Adapter Mouse. Motion. Adapter Key. Listener Key. Adapter Window. Listener Window. Adapter • Xyz. Listener are interfaces. • Xyz. Adapter are classes that implement corresponding listener interfaces. the
Nested Panels, Handling Events import java. awt. *; import java. awt. event. *; public class Nested. Panels 2 extends Nested. Panels implements Action. Listener, Item. Listener { public Nested. Panels 2() { super(); // create all the components // register item listener choice. add. Item. Listener(this); // register action listener register. Button. Handler(this); } } <Event handling methods> <Method register. Button. Handler()>
Event Handling Methods public void item. State. Changed(Item. Event event) if (event. get. State. Change() == Item. Event. SELECTED) { message. Bar. set. Text("Choice selected: " + event. get. Item()); } } { public void action. Performed(Action. Event event) { Button source = (Button) event. get. Source(); message. Bar. set. Text("Button pushed: " + source. get. Label()); }
Register The Listener protected void register. Button. Handler(Component comp) { if (comp != null) { if (comp instanceof Button) { Button button = (Button) comp; button. add. Action. Listener(this); } else if (comp instanceof Container) { Container container = (Container) comp; int n = container. get. Component. Count(); for (int i = 0; i < n; i++) register. Button. Handler( container. get. Component(i)); } } }
Event Handling Using Inner Class import java. awt. *; import java. awt. event. *; public class Nested. Panels 3 extends Nested. Panels { public Nested. Panels 3() { super(); Choice. Event. Handler c. Handler = new Choice. Event. Handler(); choice. add. Item. Listener(c. Handler); Button. Event. Handler b. Handler = new Button. Event. Handler(); b. Handler. register. Button. Handler(this); } } <Inner class Choice. Event. Handler> <Inner class Button. Event. Handler>
Inner Class Listener Inner classes • classes that reside inside other (full-fledged) • Intended to be small. • serve as helpers to the enclosing classes. class Choice. Event. Handler implements Item. Listener { public void item. State. Changed(Item. Event event) { if (event. get. State. Change() == Item. Event. SELECTED) { message. Bar. set. Text("Choice selected: " + event. get. Item()); } } }
Inner Class Listener (cont'd) class Button. Event. Handler implements Action. Listener { public void action. Performed(Action. Event event){ Button source = (Button) event. get. Source(); message. Bar. set. Text("Button pushed: " + source. get. Label()); }
Inner Class Listener (cont'd) (class Button. Event. Handler continued. ) } protected void register. Button. Handler(Component comp) { if (comp != null) { if (comp instanceof Button) { Button button = (Button) comp; button. add. Action. Listener(this); } else if (comp instanceof Container) { Container container = (Container) comp; int n = container. get. Component. Count(); for (int i = 0; i < n; i++) register. Button. Handler( container. get. Component(i)); } } }
- Slides: 25