Comp Sci 230 Software Construction Applets AWT S

  • Slides: 13
Download presentation
Comp. Sci 230 Software Construction Applets, AWT S 1 2015

Comp. Sci 230 Software Construction Applets, AWT S 1 2015

Learning Goals � You will gain a high-level understanding of two GUI frameworks �

Learning Goals � You will gain a high-level understanding of two GUI frameworks � Applets � AWT � The � Basic details are uninteresting (but necessary if you’re implementing) GUI terminology: � Component, container, panel, window, frame. Layout manager. � Theory: � Inversion of control � Composite design pattern � Painting � Classes 2 and layout are recursive. may have (or “require”) interfaces, they don’t just implement (or “provide”) them. COMPSCI 230: Swing 2

Java applets: a simple GUI framework Applet init( ) start( ) paint( ) stop(

Java applets: a simple GUI framework Applet init( ) start( ) paint( ) stop( ) destroy( ) init( ) start( ) destroy( ) paint( ) stop( ) Application code Applet framework (web browser) 3 COMPSCI 230: Swing 2

Hello World applet import javax. swing. JApplet; import javax. swing. Swing. Utilities; import javax.

Hello World applet import javax. swing. JApplet; import javax. swing. Swing. Utilities; import javax. swing. JLabel; public class Hello. World extends JApplet { //Called when this applet is loaded into the browser. public void init() { //Execute a job on the event-dispatching thread; creating this applet's GUI. try { Swing. Utilities. invoke. And. Wait(new Runnable() { public void run() { JLabel lbl = new JLabel("Hello World"); add(lbl); } }); } catch (Exception e) { System. err. println("create. GUI didn't complete successfully"); } } } 4 COMPSCI 230: Swing 2

Generalisation Object Java’s AWT framework A Component is something that can be displayed on

Generalisation Object Java’s AWT framework A Component is something that can be displayed on a GUI and with which a user can interact. Component repaint( ) schedules a Component instance for repainting. The AWT framework will subsequently call paint( ) on the Component object. paint( ) is a hook method: developers may customise it. Button Checkbox Text. Component Text. Area Text. Field add( ) is a polymorphic method; it can take as actual argument any kind of Component. get. Size( ) : int set. Size( width : int, height : int ) : void set. Visible( visible : boolean ) : void repaint( g : Graphics ) : void add. Mouse. Listener( listener : Mouse. Listener ) : void add. Key. Listener( listener : Key. Listener ) : void Choice Label List Scrollbar A Container is a special component that can nest other components, be they simple components (e. g. Buttons, Labels) or themselves Containers (e. g. Panels). A Window is a special Container that can be stacked, and moved to the front/back. Container set. Layout( mgr : Layout. Manager ) : void add( c : Component ) : void Panel Window Scroll. Panel to. Front( ) to. Back( ) Frame A Frame is a special Window with a title, menubar, border and cursor. 5 Specialisation set. Title( title : String ) : void set. Cursor( c : int ) : void set. Resizable( resizable : boolean ) : void set. Menu. Bar( mb : Menu. Bar ) : void

Component Label Frame Panel Text. Field Label Button Panel Container Button Panel add( c

Component Label Frame Panel Text. Field Label Button Panel Container Button Panel add( c : Component ) : void Frame instance class Container extends Component { private List<Component> children; … public void add( Component c ) { children. add( c ); } } Label instance Panel instance Text. Field 6 instance Panel instance Label instance Panel instance Button COMPSCIinstance 230: Swing 2 instance

Traversing the nested structure class Container extends Component { private List<Component> children; … public

Traversing the nested structure class Container extends Component { private List<Component> children; … public void add( Component c ) { children. add( c ); } public void paint( Graphics g ) { for( Component c : children ) c. paint( ); } } 1: paint( ) Frame instance 2: paint( ) Panel instance Label instance Text. Field instance 7 Panel instance 4: paint( ) 3: paint( ) 7 5 9 6 Label instance 8 Button instance COMPSCI 230: Swing 2

Layout management Why is this design for layout management so effective? Component << interface

Layout management Why is this design for layout management so effective? Component << interface >> Layout. Manager Container set. Layout( mgr : Layout. Manager ) : void do. Layout( ) add( c : Component ) : void Border. Layout layout. Container( c : Container ) minimum. Layout. Size( c : Container ) : Dimension preferred. Layout. Size( c : Container ) : Dimension Box. Layout Flow. Layout 8 Grid. Layout COMPSCI 230: Swing 2

Layout management Panel. With. Border. Layout … Alternative design: for each special kind of

Layout management Panel. With. Border. Layout … Alternative design: for each special kind of container, implement further subclasses, one for each kind of layout strategy. Container Panel. With. Box. Layout … Frame. With. Border. Layout … Frame. With. Box. Layout Composition and interfaces Inheritance This solution requires only one class for each special type of container, and one class for each layout strategy. Any kind of container instance can be configured with any kind of strategy object. Subclass explosion! To support all container (c) and layout strategy (s) combinations, c * s classes would be required. Composition offers run-time flexibility. A Frame can change how it lays out its children simply by swapping its current layout strategy (e. g. Border. Layout) for another (e. g. Grid. Layout). Overly rigid structure. Once a Panel. With. Border. Layout object, for example, has been created, the panel will always be laid out according to the border strategy. 9 COMPSCI 230: Swing 2

Provided and Required Interfaces Component << interface >> Layout. Manager Container set. Layout( mgr

Provided and Required Interfaces Component << interface >> Layout. Manager Container set. Layout( mgr : Layout. Manager ) : void do. Layout( ) add( c : Component ) : void Border. Layout layout. Container( c : Container ) minimum. Layout. Size( c : Container ) : Dimension preferred. Layout. Size( c : Container ) : Dimension Box. Layout Flow. Layout �A Container requires a Layout. Manager �A Border. Layout provides a Layout. Manager 10 Grid. Layout COMPSCI 230: Swing 2

Layout management Layout. Manager instance public class Container extends Component { … private List<

Layout management Layout. Manager instance public class Container extends Component { … private List< Component > children; private Layout. Manager layout. Manager; public void set. Layout( Layout. Manager lm ) { layout. Manager = lm; } public void do. Layout( ) { layout. Manager. layout( this ); } } Label instance 2: layout. Container( this ) 1: do. Layout( ) Panel instance Text. Field instance 11 Frame instance Panel instance Label instance Button instance Panel instance Button instance COMPSCI 230: Swing 2

Layout Managers � Border. Layout: � This scheme defines five areas for the component.

Layout Managers � Border. Layout: � This scheme defines five areas for the component. � All extra space is placed in the center area. � Flow. Layout: � Simplest, just one row � Box. Layout, Grid. Layout, … 12 COMPSCI 230: Swing 2

Learning Goals: Review � You will gain a high-level understanding of two GUI frameworks

Learning Goals: Review � You will gain a high-level understanding of two GUI frameworks � Applets � AWT � The � Basic details are uninteresting (but necessary if you’re implementing) GUI terminology: � Component, container, panel, window, frame. Layout manager. � Theory: � Inversion of control � Composite design pattern � Painting � Classes 13 and layout are recursive. may have (or “require”) interfaces, they don’t just implement (or “provide”) them. COMPSCI 230: Swing 2