Advanced GUIs I A pictures worth a thousand

  • Slides: 18
Download presentation
Advanced GUIs I A picture's worth a thousand words CS 102 -02 Lecture 8

Advanced GUIs I A picture's worth a thousand words CS 102 -02 Lecture 8 -3 May 22, 1998 CS 102 -02 Lecture 8 -3

Agenda • Panels • Text. Areas • Canvas May 22, 1998 CS 102 -02

Agenda • Panels • Text. Areas • Canvas May 22, 1998 CS 102 -02 Lecture 8 -3

Panels • Panels are Containers – Applet inherits from Panel, which is what makes

Panels • Panels are Containers – Applet inherits from Panel, which is what makes an Applet a Container • Generic containers • Hierarchical layouts – Create an Applet – Create multiple Panels, each with its own layout and add Panels to Applet's layout May 22, 1998 CS 102 -02 Lecture 8 -3

Building a Panel public void init(){ button. Panel = new Panel(); buttons = new

Building a Panel public void init(){ button. Panel = new Panel(); buttons = new Button[ 5 ]; button. Panel. set. Layout( new Grid. Layout( 1, buttons. length ) ); for ( int i = 0; i < buttons. length; i++ ) { buttons[ i ] = new Button( "Button " + (i + 1) ); button. Panel. add( buttons[ i ] ); } set. Layout( new Border. Layout() ); add( button. Panel, Border. Layout. SOUTH ); } May 22, 1998 CS 102 -02 Lecture 8 -3

Text, Text and More Text • Like a Text. Field, only bigger – Text.

Text, Text and More Text • Like a Text. Field, only bigger – Text. Fields: One line of text, action events – Text. Areas: Multiple lines, scrollbars, no action events May 22, 1998 CS 102 -02 Lecture 8 -3

Building a Text. Area • What attributes do Text. Areas have? – Number of

Building a Text. Area • What attributes do Text. Areas have? – Number of rows – Number of columns – What kinds of scrollbars do you want displayed? May 22, 1998 CS 102 -02 Lecture 8 -3

Text. Area Constructors Text. Area() Constructs a new text area. Text. Area(int rows, int

Text. Area Constructors Text. Area() Constructs a new text area. Text. Area(int rows, int columns) Constructs a new empty Text. Area with the specified number of rows and columns. Text. Area(String text) Constructs a new text area with the specified text. Text. Area(String text, int rows, int columns) Constructs a new text area with the specified text, and with the specified number of rows and columns. Text. Area(String text, int rows, int columns, int scrollbar. Vis) Constructs a new text area with the specified text, and with the rows, columns, and scroll bar visibility as specified. May 22, 1998 CS 102 -02 Lecture 8 -3

Scrollbars • If you've got more text than will fit in the display portion

Scrollbars • If you've got more text than will fit in the display portion of the Text. Area, use scrollbars • Word wrapping is virtual, not physical • Physical line breaks are always n SCROLLBARS_BOTH Create and display both vertical and horizontal scrollbars. SCROLLBARS_HORIZONTAL_ONLY Create and display horizontal scrollbar only. SCROLLBARS_NONE Do not create or display any scrollbars for the text area. SCROLLBARS_VERTICAL_ONLY Create and display vertical scrollbar only. May 22, 1998 CS 102 -02 Lecture 8 -3

Default Text. Area Values • Text – If you don't specify a String of

Default Text. Area Values • Text – If you don't specify a String of text, the default is: "" (the empty string) • Rows, columns – Default is platform-dependent • Scrollbars – Default is SCROLLBARS_BOTH May 22, 1998 CS 102 -02 Lecture 8 -3

Text. Areas Lack Action • Text. Field components fire text events and an Action.

Text. Areas Lack Action • Text. Field components fire text events and an Action. Event • Text. Area components lack the Action. Event – Link Text. Area to another component and use that to generate events • Both Text. Field and Text. Area components have Text. Event events May 22, 1998 CS 102 -02 Lecture 8 -3

Text. Area Demo • Demonstrates handling Text. Area events in two different ways public

Text. Area Demo • Demonstrates handling Text. Area events in two different ways public class Text. Area. Demo extends Applet implements Action. Listener, Text. Listener May 22, 1998 CS 102 -02 Lecture 8 -3

Setting Up the Text. Areas private Text. Area t 1, t 2; private Button

Setting Up the Text. Areas private Text. Area t 1, t 2; private Button copy; public void init() { String s = "This is a demo string to illustrate " + "copying text from one Text. Area to "; t 1 = new Text. Area(s, 5, 20, Text. Area. SCROLLBARS_NONE); t 1. add. Text. Listener( this ); add( t 1 ); copy = new Button( "Copy >>>" ); copy. add. Action. Listener( this ); add( copy ); } t 2 = new Text. Area( 5, 20 ); t 2. set. Editable( false ); add( t 2 ); May 22, 1998 CS 102 -02 Lecture 8 -3

So You Wanna Be an Action. Listener. . . public void action. Performed(Action. Event

So You Wanna Be an Action. Listener. . . public void action. Performed(Action. Event e) { t 2. set. Text( t 1. get. Selected. Text() ); } May 22, 1998 CS 102 -02 Lecture 8 -3

Listening to Text public void text. Value. Changed( Text. Event e ) { Text.

Listening to Text public void text. Value. Changed( Text. Event e ) { Text. Component source = (Text. Component) e. get. Source(); t 2. set. Text( source. get. Text() ); } May 22, 1998 CS 102 -02 Lecture 8 -3

The Canvas Class • Canvas components give you a simple drawing surface – Already

The Canvas Class • Canvas components give you a simple drawing surface – Already have a paint() method – Blank rectangular area of the screen • Draw • Trap input events from the user • Canvas components are native components – Replaced by lightweight components May 22, 1998 CS 102 -02 Lecture 8 -3

Creating a New Canvas • Canvas() – Constructs a new Canvas. • Default size

Creating a New Canvas • Canvas() – Constructs a new Canvas. • Default size is 0 x 0 pixels – Call set. Size(int width, int height) • paint() looks like: public void paint(Graphics g) { g. set. Color(get. Background()); g. fill. Rect(0, 0, width, height); } May 22, 1998 CS 102 -02 Lecture 8 -3

Subclassing Canvas // Fig. 11. 4: Custom. Canvas. java // A customized Canvas class.

Subclassing Canvas // Fig. 11. 4: Custom. Canvas. java // A customized Canvas class. import java. awt. *; public class Custom. Canvas extends Canvas { public final static int CIRCLE = 1, SQUARE = 2; private int shape; } public void paint( Graphics g ){ if ( shape == CIRCLE ) g. fill. Oval( 50, 10, 60 ); else if ( shape == SQUARE ) g. fill. Rect( 50, 10, 60 ); } public void draw( int s ) { shape = s; repaint(); } May 22, 1998 CS 102 -02 Lecture 8 -3

Using the Custom Canvas public class My. Canvas extends Applet { private Panel p;

Using the Custom Canvas public class My. Canvas extends Applet { private Panel p; private Custom. Canvas c; private Button circle, square; public void init() { // instantiate canvas c = new Custom. Canvas(); c. set. Background( Color. green ); : } } May 22, 1998 CS 102 -02 Lecture 8 -3