Components Types of Components n n n Button
Components
Types of Components n n n Button Canvas Checkbox Choice Label n n n List Scrollbar Text. Component n n Text. Area Text. Field 2
Button n n new Button() constructs a Button with no text new Button(String) constructs a Button with the specified text void add. Action. Listener(Action. Listener) void remove. Action. Listener(Action. Listener) n String get. Label() void set. Label(String) n String action. Event. get. Action. Command() n n n Returns the “action command” associated with this Button By default, the action command = the text on the Button 3
A Button listener button. add. Action. Listener (new My. Button. Listener ()); class My. Button. Listener implements Action. Listener { public void action. Performed (Action. Event event) { show. Status ("Button clicked. "); } } 4
Label constructors n new Label() constructs a Label with no text new Label(String) constructs a Label with the specified text new Label(String, alignment) constructs a label with the specified text and alignment: n n Label. LEFT Label. RIGHT Label. CENTER (These are int constants defined in the Label class) 5
Label methods n n String get. Text() void set. Text(String) int get. Alignment() void set. Alignment(int) 6
Scrollbar constructors n n new Scrollbar() creates a vertical Scrollbar new Scrollbar(int orientation) n n Scrollbar. HORIZONTAL or Scrollbar. VERTICAL new Scrollbar(int orientation, int initial. Value, int bubble. Size, int minimum, int maximum) n n bubble. Size: size of the bubble (sometimes called elevator) min value of Scrollbar = minimum max value of Scrollbar = maximum - bubble. Size n (value of scrollbar is given by left or top edge of bubble) initial. Value: the initial position setting of the bubble 7
Scrollbars bubble. Size minimum The units for bubble. Size are the same as those for minimum and maximum — bubble. Size The scrollbar reading is taken from the left edge of the bubble 8
Scrollbar methods n n n n void add. Adjustment. Listener(Adjustment. Listener) void remove. Adjustment. Listener(Adjustment. Listener) int get. Minimum() void set. Minimum(int) int get. Maximum() void set. Maximum(int) int get. Value() void set. Value(int) 9
A Scrollbar listener scrollbar. add. Adjustment. Listener(new My. Scrollbar. Listener ()); class My. Scrollbar. Listener implements Adjustment. Listener { public void adjustment. Value. Changed (Adjustment. Event event) { show. Status ("Scrollbar: " + scrollbar. get. Value ()); } }; 10
Checkbox constructors n n new new new Checkbox() Checkbox(String, boolean) Checkbox(String, boolean, Checkbox. Group) Checkbox(String, Checkbox. Group, boolean) 11
Checkbox methods n n n void add. Item. Listener(Item. Listener) void remove. Item. Listener(Item. Listener) Checkbox. Group get. Checkbox. Group() void set. Checkbox. Group(Checkbox. Group) String get. Label() void set. Label(String) 12
A Checkbox listener checkbox. add. Item. Listener (new My. Checkbox. Listener ()); class My. Checkbox. Listener implements Item. Listener { public void item. State. Changed (Item. Event event) { show. Status ("Checkbox status is now " + (event. get. State. Change () == Item. Event. SELECTED)); } }; 13
Checkbox. Group methods n Checkbox get. Selected. Checkbox() n n Suggested use: my. CG. get. Selected. Checkbox(). get. Label() void set. Selected. Checkbox(Checkbox) 14
A Checkbox. Group listener n No such thing—listen to individual Checkboxes checkbox 1. add. Item. Listener (my. Checkbox. Group. Listener); checkbox 2. add. Item. Listener (my. Checkbox. Group. Listener); class My. Checkbox. Group. Listener implements Item. Listener { public void item. State. Changed (Item. Event event) { Checkbox d = checkbox. Group. get. Selected. Checkbox (); show. Status ("Checkbox. Group: " + d. get. Label ()); } } 15
Choice constructor n n new Choice() After constructing a Choice, use add(String) to add items to it 16
Choice methods n n n n add. Item. Listener(Item. Listener) remove. Item. Listener(Item. Listener) void add (String) void remove. All() String get. Selected. Item() void select(String) 17
A Choice listener choice. add. Item. Listener (new My. Choice. Listener ()); class My. Choice. Listener implements Item. Listener { public void item. State. Changed (Item. Event event) { show. Status ("Choice: " + choice. get. Selected. Item ()); } }; 18
List constructors n n new List() constructs an empty list new List(int) n n int: the number of items visible (a scrollbar is automatically added if needed) new List(int, boolean) n boolean: true if multiple selection is allowed 19
List methods I n n n void add. Item. Listener(Item. Listener) void remove. Item. Listener(Item. Listener) void add(String) void remove. All() 20
List methods II n n String get. Selected. Item() String[] get. Selected. Items() n int get. Selected. Index() int[] get. Selected. Indexes() n void select(int) // can only select by index n n There are other methods for adding, removing, etc. by index 21
A List listener list. add. Item. Listener (new My. Listener ()); class My. Listener implements Item. Listener { public void item. State. Changed (Item. Event event) { show. Status ("List: " + list. get. Selected. Item ()); } }; 22
Text. Component n No constructors n n n n Instead, construct a Text. Field or Text. Area void add. Text. Listener(Text. Listener) void remove. Text. Listener(Text. Listener) String get. Text() void set. Text(String) boolean is. Editable() void set. Editable(boolean) 23
Text. Field constructors n n new Text. Field() new Text. Field(int) n n new Text. Field(String) n n int: the approximate desired number of columns String: the initial text in the Text. Field new Text. Field(String, int) 24
Text. Field methods n n n void add. Action. Listener(Action. Listener) void remove. Action. Listener(Action. Listener) The Action. Listener for a Text. Field responds only when the Enter key is pressed 25
Text. Area constructors n new Text. Area() new Text. Area(int rows, int columns, int scroll) n n n Text. Area. SCROLLBARS_VERTICAL_ONLY Text. Area. SCROLLBARS_HORIZONTAL_ONLY Text. Area. SCROLLBARS_BOTH Text. Area. SCROLLBARS_NONE new Text. Area(String) new Text. Area(String, int rows, int columns) 26
Text. Component listeners text. Field. add. Action. Listener (new My. Text. Field. Listeners ()); text. Field. add. Text. Listener (new My. Text. Field. Listeners ()); class My. Text. Field. Listeners implements Action. Listener, Text. Listener { public void action. Performed (Action. Event event) { show. Status ("Text. Field (Action. Listener): " + text. Field. get. Text ()); } public void text. Value. Changed (Text. Event event) { show. Status ("Text. Field (Text. Listener): " + text. Field. get. Text ()); } }; 27
Suggestion: Don’t use text listeners! n Text fields are intended to be passive; that is, nothing should happen immediately as a result of typing into them n n Consequently, I’ve seen a lot of programs in which text listeners do their work at the wrong time It usually works much better to ignore text fields until an active control (such as a Button) is used, and then do get. Text() to find the current value in the text field 28
Canvas n n This is a rectangular blank area that you can paint on Constructor: new Canvas() Don't use Canvas directly; instead, subclass it and override its void paint(Graphics) method Or even simpler: Paint directly on a Panel 29
The End 30
- Slides: 30