22AWTAWTJavacontrols22.1AWTComponent22.1.1add()ContainerAdd()Componentadd(ComponentcompObj)22AWT507compObjcompObjremove()Containervoidremove(Componentobj)objremoveAll()22.1.22022.2LabelLabelLabel()Label(Stringstr)Label(Stringstr,inthow)strstrhowHowLabel.LEFT,Label.RIGHTLabel.CENTERsetText()getText()voidsetText(Stringstr)StringgetText()setText()strgetText()setAlignment()getAlignment()voidsetAlignment(inthow)intgetAlignment()5082Javahow//DemonstrateLabelsimportjava.awt.*;importjava.applet.*;/*appletcode=LabelDemowidth=300height=200/applet*/publicclassLabelDemoextendsapplet{publicvoidinit(){Labelone=newLabel(One);Labeltwo=newLabel(Two);Labelthree=newLabel(Three);//addlabelstoappletwindowadd(one);add(two);add(three);}}LabelDemo22.3ButtonButtonButton()Button(Stringstr)strsetLabel()getLabel()voidsetLabel(Stringstr)22AWT509StringgetLabel()str22.3.1ActionListeneractionPerformed()ActionEventYesNoUndecidedgetActionCommand()getActionCommand()actionPerformed()ActionEvent//DemonstrateButtonsimportjava.awt.*;importjava.awt.event.*;importjava.applet.*;/*appletcode=ButtonDemowidth=250height=150/applet*/publicclassButtonDemoextendsappletimplementsActionListener{Stringmsg=;Buttonyes,no,maybe;publicvoidinit(){yes=newButton(Yes);no=newButton(No);maybe=newButton(Undecided);add(yes);add(no);add(maybe);yes.addActionListener(this);no.addActionListener(this);maybe.addActionListener(this);}publicvoidactionPerformed(ActionEventae){}Stringstr=ae.getActionCommand();if(str.equals(Yes)){msg=YoupressedYes.;}elseif(str.equals(No)){msg=YoupressedNo.;}5102Javaelse{msg=YoupressedUndecided.;}repaint();}publicvoidpaint(Graphicsg){g.drawString(msg,6,100);}}ButtonDemo22-122-1ButtonDemo2getSource()//RecognizeButtonobjects.importjava.awt.*;importjava.awt.event.*;importjava.applet.*;/*appletcode=ButtonListwidth=250height=150/applet*/publicclassButtonListextendsappletimplementsActionListener{Stringmsg=;ButtonbList[]=newButton[3];publicvoidinit(){Buttonyes=newButton(Yes);Buttonno=newButton(No);Buttonmaybe=newButton(Undecided);//storereferencestobuttonsasaddedbList[0]=(Button)add(yes);bList[1]=(Button)add(no);22AWT511bList[2]=(Button)add(maybe);//registertoreceiveactioneventsfor(inti=0;i3;i++){bList[i].addActionListener(this);}}publicvoidactionPerformed(ActionEventae){for(inti=0;i3;i++){if(ae.getSource()==bList[i]){msg=Youpressed+bList[i].getLabel();}}repaint();}publicvoidpaint(Graphicsg){g.drawString(msg,6,100);}}add()actionPerformed()22.4CheckboxCheckboxCheckboxCheckbox()Checkbox(Stringstr)Checkbox(Stringstr,booleanon)Checkbox(Stringstr,booleanon,CheckboxGroupcbGroup)Checkbox(Stringstr,CheckboxGroupcbGroup,booleanon)strontruestrcbGroup5122JavacbGroupnullongetState()setState()getLabel()setLabel()booleangetState()voidsetState(booleanon)StringgetLabel()voidsetLabel(Stringstr)ontruefalsestr22.4.1ItemListeneritemStateChanged()ItemEvent//Demonstratecheckboxes.importjava.awt.*;importjava.awt.event.*;importjava.applet.*;/*appletcode=CheckboxDemowidth=250height=200/applet*/publicclassCheckboxDemoextendsappletimplementsItemListener{Stringmsg=;CheckboxWin98,winNT,solaris,mac;publicvoidinit(){Win98=newCheckbox(Windows98,null,true);winNT=newCheckbox(WindowsNT/2000);solaris=newCheckbox(Solaris);mac=newCheckbox(MacOS);add(Win98);add(winNT);add(solaris);add(mac);Win98.addItemListener(this);winNT.addItemListener(this);solaris.addItemListener(this);mac.addItemListener(this);}22AWT513publicvoiditemStateChanged(ItemEventie){repaint();}//Displaycurrentstateofthecheckboxes.publicvoidpaint(Graphicsg){msg=Currentstate:;g.drawString(msg,6,80);msg=Windows98:+Win98.getState();g.drawString(msg,6,100);msg=WindowsNT/2000:+winNT.getState();g.drawString(msg,6,120);msg=Solaris:+solaris.getState();g.drawString(msg,6,140);msg=MacOS:+mac.getState();g.drawString(msg,6,160);}}22-222-2CheckboxDemo22.5getSelectedCheckbox()setSelectedCheckbox()5142JavaCheckboxgetSelectedCheckbox()voidsetSelectedCheckbox(Checkboxwhich)which//Demonstratecheckboxgroup.importjava.awt.*;importjava.awt.event.*;importjava.applet.*;/*appletcode=CBGroupwidth=250height=200/applet*/publicclassCBGroupextendsappletimplementsItemListener{Stringmsg=;CheckboxWin98,winNT,solaris,mac;CheckboxGroupcbg;publicvoidinit(){cbg=newCheckboxGroup();Win98=newCheckbox(Windows98,cbg,true);winNT=newCheckbox(WindowsNT/2000,cbg,false);solaris=newCheckbox(Solaris,cbg,false);mac=newCheckbox(MacOS,cbg,false);add(Win98);add(winNT);add(solaris);add(mac);Win98.addItemListener(this);winNT.addItemListener(this);solaris.addItemListener(this);mac.addItemListener(this);}publicvoiditemStateChanged(ItemEventie){repaint();}//Displaycurrentstateofthecheckboxes.publicvoidpaint(Graphicsg){msg=Currentselection:;msg+=cbg.getSelectedCheckbox().getLabel();g.drawString(msg,6,100);}}CBGroup22-322AWT51522-3CBGroup22.6ChoiceChoiceaddItem()add()voidaddItem(Stringname)voidadd(Stringname)nameadd()addItem()getSelectedItem()getSelectedIndex()StringgetSelectedItem()intgetSelectedIndex()GetSelectedItem()GetSelectedIndex()getItemCount()select()intget