Use of AWT components

Source: Internet
Author: User

1, button

Package GUI; import Java. AWT. button; import Java. AWT. frame; import Java. AWT. gridlayout; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener;/* component usage. As mentioned earlier, the button * is also registered with itself */public class buttonexample implements actionlistener {frame F; button B1, B2; // B1, b2 is a button object. Different from the following string b1, b2 represents the string corresponding to the event action. // The Event Processing Method in actionlistener public void actionreceivmed (actionevent E) {string cmd = E. ge Tactioncommand (); If (CMD. Equals ("B1") b2.setenabled (! B2.isenabled (); else b1.setenabled (! B1.isenabled ();} // constructor public buttonexample () {f = new frame ("button example"); F. setlayout (New gridlayout (1, 2); b1 = new button ("Left"); b1.setactioncommand ("B1 "); // use the string "B1" to indicate the Event Response b1.addactionlistener (this) on the button B1; // each component must register listenerb2 = new button ("right "); b2.setactioncommand ("B2"); b2.addactionlistener (this); F. add (B1); F. add (B2); F. pack (); F. setvisible (true);} // main function public static void main (string [] ARGs) {New buttonexample ();}}

2, checkbox

1) multiple options

Package GUI; import Java. AWT. checkbox; import Java. AWT. frame; import Java. AWT. gridlayout; import Java. AWT. event. itemevent; import Java. AWT. event. itemlistener;/* multiple options of checkbox to implement itemlistener */public class checkboxexample implements itemlistener {frame F; checkbox cb [] = new checkbox [3]; // an array of the checkbox type // The event handling method in itemlistener public void itemstatechanged (itemevent e) {checkbox CH = (checkbox) E. getsource (); // The Event source object, which is of the checkbox type string label = CH. getlabel (); // the header of the source checkbox if (E. getstatechange () = itemevent. selected) // itemevent. selected should be a constant, indicating to select system. out. println (Label + "selected"); else system. out. println (Label + "deselected");} // constructor public checkboxexample () {f = new frame ("checkbox example"); F. setlayout (New gridlayout (3, 1); // three methods for initializing the checkbox cb [0] = new checkbox ("item 1 "); CB [1] = new checkbox ("item 2", true); // cb [2] = new checkbox (); cb [2] is selected by default. setlabel ("item 3"); For (INT I = 0; I <3; I ++) {cb [I]. additemlistener (this); // Add listener F for each listener. add (cb [I]); // Add each to f} f. pack (); F. setvisible (true);} // main function public static void main (string [] ARGs) {New checkboxexample ();}}

2) single choice

Package GUI; import Java. AWT. checkbox; import Java. AWT. checkboxgroup; import Java. AWT. frame; import Java. AWT. gridlayout;/* 1. The checkboxgroup class is used to achieve a single choice. A checkboxgroup object can contain several checkbox objects, * but in the same checkboxgroup, at the same time, only one checkbox object is selected, which is used to implement * single selection. * 2. events are not processed here. As you can see, Java is not imported into the import. AWT. event. *, checkboxgroupexample * does not follow the implements event processing interface */public class checkboxgroupexample {frame F; checkbox cb [] = new checkbox [4]; // contains four checkbox Arrays: checkboxgroup cbg1 and cbg2; // two checkboxgrouppublic static void main (string [] ARGs) {New checkboxgroupexample ();} public checkboxgroupexample () {f = new frame ("checkboxgroup example"); F. setlayout (New gridlayout (4, 1); cbg1 = new checkboxgroup (); cbg2 = new checkboxgroup (); cb [0] = new checkbox ("item 1 "); CB [1] = new checkbox ("item 2"); // cb [0] and CB [1] are placed in cbg1 cb [0]. setcheckboxgroup (cbg1); cb [1]. setcheckboxgroup (cbg1); // another method is to put cb [2], CB [3] into cbg2, during initialization, define the groupcb [2] = new checkbox ("item 3", cbg2, true); cb [3] = new checkbox ("item 4 ", cbg2, false); For (INT I = 0; I <4; I ++) {f. add (cb [I]); // do not forget to put in f} f. pack (); F. setvisible (true );}}

3, choice

Package GUI; import Java. AWT. borderlayout; import Java. AWT. choice; import Java. AWT. frame; import Java. AWT. event. itemevent; import Java. AWT. event. itemlistener;/* choice drop-down menu to generate a choice object. Add options to choice to check which option is selected. * If choice is detected as itemlistener, it must be registered with it, and implement the itemstatechanged Method */public class choiceexample implements itemlistener {frame F; choice ch; // declare the choice object // The method of the event public void itemstatechanged (itemevent E) {choice c = (choice) E. getsource (); system. out. println ("selected item index:" + C. getselectedindex (); system. out. println ("selected item:" + C. getselecteditem ();} // main function. The parameter passing method of main function is public static void main (string [] ARGs) {New choiceexample (ARGs );} // put initialization in the constructor public choiceexample (string [] ARGs) {f = new frame ("Choice example"); CH = New Choice (); // generate a choice object. Be sure not to declare that it does not point to the object ch. additemlistener (this); // Add the class that implements the itemlistener interface. Note that a for (INT I = 0; I <args. length; I ++) ch. add (ARGs [I]); // Add options to CH, from the command line parameter F. add (CH, borderlayout. north); F. pack (); F. setvisible (true); system. out. println ("totla" + CH. getitemcount () + "items. ");}}

4, list

Package GUI; import Java. AWT. borderlayout; import Java. AWT. frame; import Java. AWT. list; import Java. AWT. event. itemevent; import Java. AWT. event. itemlistener; // use list to implement multiple-choice lists public class listexample implements itemlistener {frame F; List ls; public static void main (string [] ARGs) {New listexample (ARGs );} public listexample (string ARGs []) {f = new frame ("List Example"); // The first two parameters are the number of options to be displayed at the same time and whether to use multiple options, note that all data is input as string type. Convert the data type. // note the construction method ls = new list (integer. parseint (ARGs [0]), Boolean. valueof (ARGs [1]). booleanvalue (); LS. additemlistener (this); For (INT I = 2; I <args. length; I ++) // from the third is the option ls. add (ARGs [I]); F. add (LS, borderlayout. center); F. pack (); F. setvisible (true); system. out. println ("Total" + ls. getitemcount () + "items");} public void itemstatechanged (itemevent e) {// todo auto-generated method stublist L = (list) E. getsource (); int index [] = L. getselectedindexes (); string STR [] = L. getselecteditems (); For (INT I = 0; I <index. length; I ++) {system. out. println (index [I] + "" + STR [I]) ;}}

5, label

Package GUI; import Java. AWT. borderlayout; import Java. AWT. choice; import Java. AWT. frame; import Java. AWT. event. itemevent; import Java. AWT. event. itemlistener; import Java. AWT. label;/* do not import the label in XML * // display the information on the label component public class labelexample implements itemlistener {frame F; choice ch; label result; // declare a lable object // event processing public void itemstatechanged (itemevent e) {choice c = (choice) E. getsource (); string STR = C. getselecteditem () + "selected"; result. settext (STR); // implement interaction between two different components and add STR to the result object, clear the last text content} // constructor public labelexample (string ARGs []) {f = new frame ("lable example "); result = new label ("no item selected"); CH = New Choice (); Ch. additemlistener (this); For (INT I = 0; I <args. length; I ++) {ch. add (ARGs [I]);} f. add (CH, borderlayout. north); F. add (result, borderlayout. south); F. pack (); F. setvisible (true); system. out. println ("Total" + CH. getitemcount () + "items");} // main function public static void main (string [] ARGs) {New labelexample (ARGs );}}

6. scrollbar

Package GUI; import Java. AWT. borderlayout; import Java. AWT. button; import Java. AWT. frame; import Java. AWT. panel; import Java. AWT. scrollbar; import Java. AWT. event. adjustmentevent; import Java. AWT. event. adjustmentlistener;/* scrollbar implements the scroll bar and adjustmentlistener interface */public class scrollbarexample implements adjustmentlistener {frame F; button BTN; Panel P; // note the differences between P and F. scrollbar HSB, VSB; int x = 0, y = 0; public static void main (string [] ARGs) {New scrollbarexample () ;}// constructor public scrollbarexample () {f = new frame ("scrollbar example"); P = new Panel (null); // P constructor BTN = new button ("button"); BTN. setsize (50, 20); BTN. setlocation (x, y); // constructor of The scrollbar object HSB = new scrollbar (scrollbar. horizontal, 0, 10, 0,200); VSB = new scrollbar (scrollbar. vertical, 0, 10, 0,200); HSB. addadjustmentlistener (this); VSB. addadjustmentlistener (this); // do not drop it here. Add BTN to P, and add P to F. The order is P. add (BTN); F. add (p, borderlayout. center); F. add (HSB, borderlayout. south); F. add (VSB, borderlayout. east); F. setsize (250,250); F. setvisible (true);} // public void adjustmentvaluechanged (adjustmentevent e) {scrollbar sb = (scrollbar) E. getsource (); If (sb. getorientation () = scrollbar. horizontal) // getorientation () gets the type of the scroll bar x = sb. getvalue (); else y = sb. getvalue (); BTN. setlocation (x, y); // set the new BTN position }}

7, scrollpane

Package GUI; import Java. AWT. button; import Java. AWT. flowlayout; import Java. AWT. frame; import Java. AWT. panel; import Java. AWT. scrollpane;/* scrollpane determines whether to generate a scroll bar. Add the component to the Panel first, and then add the panel to the scrollpane *. In this way, when the Panel is larger than the scrollpane, scrollpane will automatically generate a scroll bar for us to use */public class scrollpaneexample {public static void main (string [] ARGs) {frame F = new frame ("scrollpane example "); int n = integer. parseint (ARGs [0]); Panel P = new Panel (New flowlayout (); // note the Panel constructor. Button BTN [] = new button [N]; for (INT I = 0; I <n; I ++) {BTN [I] = new button ("button" + I); p. add (BTN [I]); // Add the component to the Panel first} scrollpane sp = new scrollpane (scrollpane. scrollbars_as_needed); // note the scrollpane constructor and parameter sp. add (p); // Add the panel to the scrollpane. When the Panel is larger than the scrollpane, the effect F is automatically generated. add (SP); // finally add scrollpane to frame F. pack (); F. setvisible (true );}}

8, textfield

Package GUI; import Java. AWT. frame; import Java. AWT. gridlayout; import Java. AWT. label; import Java. AWT. textfield; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. AWT. event. textevent; import Java. AWT. event. textlistener;/* use textfield to input text in the textfield component and perform some processing. * display the text in the label at the same time, press enter to clear the textfield area */public class textfieldexample implements textlistener, actionlistener {frame F; label Lb; // label object textfield TF; // textfield object public static void main (string [] ARGs) {New textfieldexample ();} public textfieldexample () {f = new frame ("textfield example"); F. setlayout (New gridlayout (); TF = new textfield ("", 20); // the construction method of textfield object /// added two listeners: TF. addtextlistener (this); TF. addactionlistener (this); // lB = new label (); F. add (TF); F. add (LB); F. pack (); F. setvisible (true);} public void textvaluechanged (textevent e) {lb. settext (TF. gettext (); // implements information interaction between two components} public void actionreceivmed (actionevent e) {TF. settext ("clearing out ");}}

9, textarea

Package GUI; import Java. AWT. frame; import Java. AWT. gridlayout; import Java. AWT. textarea;/* use textarea to display multiple lines of text and automatically generate a scroll bar. The textarea component has more lines and scroll bar settings than the textfield component * but not like the textfield component, it will generate actionevent time, other usage methods are the same */public class textareaexample {public static void main (string [] ARGs) {frame F = new frame ("textarea example"); F. setlayout (New gridlayout (1234567890); string STR = "234567890 34567890 4567890 567890"; // construct a new text area with the specified text, and the specified number of rows, number of columns, and scroll bar visibility textarea TA1 = new textarea (STR, textarea. scrollbars_none); textarea TA2 = new textarea (STR, 2, 10, textarea. scrollbars_horizontal_only); textarea TA3 = new textarea (STR, 2, 10, textarea. scrollbars_vertical_only); textarea TA4 = new textarea (STR, 2, 10, textarea. scrollbars_both); F. add (TA1); F. add (TA2); F. add (TA3); F. add (TA4); F. pack (); F. setvisible (true );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.