ComponentsTypes of ComponentsButtonA Button listenerLabel constructorsLabel methodsScrollbar constructorsScrollbarsScrollbar methodsA Scrollbar listenerCheckbox constructorsCheckbox methodsA Checkbox listenerCheckboxGroup methodsA CheckboxGroup listenerChoice constructorChoice methodsA Choice listenerList constructorsList methods IList methods IIA List listenerTextComponentTextField constructorsTextField methodsTextArea constructorsTextComponent listenersSuggestion: Don’t use text listeners!CanvasThe EndJan 14, 2019Components2 Types of ComponentsButtonCanvasCheckboxChoiceLabelListScrollbarTextComponentTextAreaTextField3Buttonnew Button() constructs a Button with no textnew Button(String) constructs a Button with the specified textvoid addActionListener(ActionListener)void removeActionListener(ActionListener)String getLabel()void setLabel(String)String actionEvent.getActionCommand()Returns the “action command” associated with this ButtonBy default, the action command = the text on the Button4A Button listener button.addActionListener (new MyButtonListener ()); class MyButtonListener implements ActionListener { public void actionPerformed (ActionEvent event) { showStatus ("Button clicked."); }}5Label constructorsnew Label() constructs a Label with no textnew Label(String) constructs a Label with the specified textnew Label(String, alignment) constructs a label with the specified text and alignment:Label.LEFTLabel.RIGHTLabel.CENTER(These are int constants defined in the Label class)6Label methodsString getText()void setText(String)int getAlignment()void setAlignment(int)7Scrollbar constructorsnew Scrollbar() creates a vertical Scrollbarnew Scrollbar(int orientation)Scrollbar.HORIZONTAL or Scrollbar.VERTICALnew Scrollbar(int orientation, int initialValue, int bubbleSize, int minimum, int maximum)bubbleSize: size of the bubble (sometimes called elevator)min value of Scrollbar = minimummax value of Scrollbar = maximum - bubbleSize(value of scrollbar is given by left or top edge of bubble)initialValue: the initial position setting of the bubble8ScrollbarsbubbleSizemaximummaximum — bubbleSizeminimumThe units for bubbleSize are the same as those for minimum and maximumThe scrollbar reading is takenfrom the left edge of the bubble9Scrollbar methodsvoid addAdjustmentListener(AdjustmentListener)void removeAdjustmentListener(AdjustmentListener)int getMinimum()void setMinimum(int)int getMaximum()void setMaximum(int)int getValue()void setValue(int)10A Scrollbar listenerscrollbar.addAdjustmentListener(new MyScrollbarListener ()); class MyScrollbarListener implements AdjustmentListener { public void adjustmentValueChanged (AdjustmentEvent event) { showStatus ("Scrollbar: " + scrollbar.getValue ()); }};11Checkbox constructorsnew Checkbox()new Checkbox(String)new Checkbox(String, boolean)new Checkbox(String, boolean, CheckboxGroup)new Checkbox(String, CheckboxGroup, boolean)12Checkbox methodsvoid addItemListener(ItemListener)void removeItemListener(ItemListener)CheckboxGroup getCheckboxGroup()void setCheckboxGroup(CheckboxGroup)String getLabel()void setLabel(String)13A Checkbox listener checkbox.addItemListener (new MyCheckboxListener ()); class MyCheckboxListener implements ItemListener { public void itemStateChanged (ItemEvent event) { showStatus ("Checkbox status is now " + (event.getStateChange () == ItemEvent.SELECTED)); }};14CheckboxGroup methodsCheckbox getSelectedCheckbox()Suggested use:myCG.getSelectedCheckbox().getLabel()void setSelectedCheckbox(Checkbox)15A CheckboxGroup listenerNo such thing—listen to individual Checkboxes checkbox1.addItemListener (myCheckboxGroupListener); checkbox2.addItemListener (myCheckboxGroupListener);class MyCheckboxGroupListener implements ItemListener { public void itemStateChanged (ItemEvent event) { Checkbox d = checkboxGroup.getSelectedCheckbox (); showStatus ("CheckboxGroup: " + d.getLabel ()); }}16Choice constructornew Choice()After constructing a Choice, use add(String) to add items to it17Choice methodsaddItemListener(ItemListener)removeItemListener(ItemListener)void add (String)void remove(String)void removeAll()String getSelectedItem()void select(String)18 A Choice listener choice.addItemListener (new MyChoiceListener ()); class MyChoiceListener implements ItemListener { public void itemStateChanged (ItemEvent event) { showStatus ("Choice: " + choice.getSelectedItem ()); }};19List constructorsnew List() constructs an empty listnew List(int)int: the number of items visible (a scrollbar is automatically added if needed)new List(int, boolean)boolean: true if multiple selection is allowed20List methods Ivoid addItemListener(ItemListener)void removeItemListener(ItemListener)void add(String)void remove(String)void removeAll()21List methods IIString getSelectedItem()String[] getSelectedItems()int getSelectedIndex()int[] getSelectedIndexes()void select(int) // can only select by indexThere are other methods for adding, removing, etc. by index22A List listener list.addItemListener (new MyListListener ()); class MyListListener implements ItemListener { public void itemStateChanged (ItemEvent event) { showStatus ("List: " + list.getSelectedItem ()); }};23TextComponentNo constructorsInstead, construct a TextField or TextAreavoid addTextListener(TextListener)void removeTextListener(TextListener)String getText()void setText(String)boolean isEditable()void setEditable(boolean)24TextField constructorsnew TextField()new TextField(int)int: the approximate desired number of columnsnew TextField(String)String: the initial text in the TextFieldnew TextField(String, int)25TextField methodsvoid addActionListener(ActionListener)void removeActionListener(ActionListener)The ActionListener for a TextField responds only when the Enter key is pressed26TextArea constructorsnew TextArea()new TextArea(int rows, int columns)new TextArea(int rows, int columns, int scroll)TextArea.SCROLLBARS_VERTICAL_ONLYTextArea.SCROLLBARS_HORIZONTAL_ONLYTextArea.SCROLLBARS_BOTHTextArea.SCROLLBARS_NONEnew TextArea(String)new
View Full Document