Use of advanced AWT components

Source: Internet
Author: User

1. Font

Package GUI; import Java. AWT. borderlayout; import Java. AWT. choice; import Java. AWT. font; import Java. AWT. frame; import Java. AWT. gridlayout; import Java. AWT. label; import Java. AWT. panel; import Java. AWT. event. itemevent; import Java. AWT. event. itemlistener;/* 1, it is useful to inherit the frame. We want to use a frame and put the result in the frame. Of course, it can be like the previous Code * frame F; then f. add (...) add a component to the frame. This program uses a new method to inherit the frame and then complete the * task in the constructor. * 2. Check the usage of this and super in the constructor. */public class fontexample extends frame implements itemlistener {choice chfont, chstyle, chsize; label L; string fontname [] = {"dialog", "dialoginput", "serif", "sansserif", "monospaced"}; int fontstyle [] = {font. plain, Font. bold, Font. italic, Font. bold | font. italic}; // main function public static void main (string [] ARGs) {New fontexample () ;}// constructor public fontexample () {// call the constructor of the parent class, equivalent to frame F = new frame ("font example"), actually a frame object is generated // This object has no name super ("font example "); // name chfont = New Choice (); For (INT I = 0; I <fontname. length; I ++) chfont. add (fontname [I]); // font chstyle = New Choice (); chstyle. add ("standard"); chstyle. add ("bold"); chstyle. add ("italic"); chstyle. add ("bold italic"); // chsize = New Choice (); For (INT I = 8; I <72; I = I + 2) chsize. add (integer. tostring (I); chfont. additemlistener (this); chstyle. additemli Stener (this); chsize. additemlistener (this); // put the three options into a panel object with one row and three columns. Panel P = new Panel (New gridlayout (); p. add (chfont); p. add (chstyle); p. add (chsize); // generate a label object L = new label ("AWT font test"); // call the method of the current frame object, why can both this and super be added ????? Add (p, borderlayout. north); add (L, borderlayout. center); Pack (); setvisible (true) ;}// Event Response Public void itemstatechanged (itemevent e) {string name = chfont. getselecteditem (); int style = fontstyle [chstyle. getselectedindex ()]; int size = integer. parseint (chsize. getselecteditem (); font F = new font (name, style, size); // get the font object through Event Response, three parameters of the font object L. setfont (f); // set the font style pack () of the label ();}}

2, color

Package GUI; import Java. AWT. *; import Java. AWT. event. *; public class colorexample extends frame implements adjustmentlistener {scrollbar red, green, blue; // three scroll bar objects checkbox fore, back; // The foreground and background selection objects checkboxgroup CG; label L; // put the text public static void main (string argv []) {New colorexample ();} public colorexample () {super ("color example "); // generate a frame object. Pay attention to the extends frame !!! // Constructor of the scroll bar, which is used to set the level, the initial value of the scroll bar, the number of values of the scroll volume at a time, the minimum value of the scroll, and the maximum value of Red = new scrollbar (scrollbar. horizontal, 0, 10, 0,255); Green = new scrollbar (scrollbar. horizontal, 0, 10, 0,255); Blue = new scrollbar (scrollbar. horizontal, 0, 10, 0,255); red. addadjustmentlistener (this); green. addadjustmentlistener (this); blue. addadjustmentlistener (this); Panel p1 = new Panel (New gridlayout (3, 2); // P1 to hold the scroll bar, put p1.add (new label ("red value:"); p1.add (red); p1.add (new label ("green value:") in the order of three rows and two columns :")); p1.add (green); p1.add (new label ("blue value:"); p1.add (blue); CG = new checkboxgroup (); // when using group, you can only select fore = new checkbox ("foreground color", CG, true); Back = new checkbox ("background color", CG, false ); panel P2 = new Panel (); // P2 for foreground background. Select p2.add (fore); p2.add (back); L = new label ("color test "); // l put the Labell of the test. setfont (new font ("dialog", Font. bold, 24); add (P1, borderlayout. north); add (L, borderlayout. center); add (P2, borderlayout. south); Pack (); setvisible (true);} public void adjustmentvaluechanged (adjustmentevent e) {int r = red. getvalue (); int G = green. getvalue (); int B = blue. getvalue (); If (fore. getstate () L. setforeground (new color (R, G, B); // set the foreground color elsel. setbackground (new color (R, G, B ));}}

3, menu

Package GUI; import Java. AWT. *; import Java. AWT. event. *;/* 1. The top menu bar menubar is in the menu architecture. A frame window can only have one menubar. You can use setmenubar () * of the frame class to specify the menubar object to be used. * 2. Each menu bar can contain several different menu categories. In the menu bar, there can be menu options menuitem, * split line, or even another menu. * 3 apart from the menuitem option, there is also a checkboxmenuitem class with a check box. * 4 You can also use the menushortcut class to select menu options by using shortcut keys. * 5. Finally, right-click the shortcut menu popupmenu class */public class menuexample extends adapter implements actionlistener, itemlistener {frame F; label L; popupmenu PM; public static void main (string argv []) {New menuexample ();} public menuexample () {f = new frame ("menu example"); F. addmouselistener (this); // to register F, because menu Item1 is a nested menu, which needs to be obtained based on the cursor position when displaying the shortcut menu, it is also a menu option of menu menu1 (note the difference between menu and menu options) menuitem item1_1 = new Menuitem ("item1_1"); menuitem item1_2 = new menuitem ("item1_2"); menuitem item1_3 = new menuitem ("item1_3"); item1_1.addactionlistener (this); consume (this ); item1_3.addactionlistener (this); menu Item1 = new menu ("Item1", false); item1.add (item1_1); item1.add (item1_2); item1.add (item1_3 ); // menu option menuitem item2 menushortcut MS = new menushortcut (keyevent. vk_a, false); menuitem item2 = new Menuitem ("item2", MS); item2.addactionlistener (this); // check menu options checkboxmenuitem item3checkboxmenuitem item3 = new checkboxmenuitem ("item3"); item3.additemlistener (this ); // Add the preceding three menu options to menu menu1 (where menu option Item1 itself is a menu) menu menu1 = new menu ("menu1"); menu1.add (Item1 ); // menu1.add (item2); menu1.addseparator (); // split line menu1.add (item3); // another menu help, same as menu help = new menu ("help "); menuitem help1 = new menuit EM ("Index"); menuitem help2 = new menuitem ("about"); help1.addactionlistener (this); help2.addactionlistener (this); help. add (help1); help. addseparator (); help. add (help2); // shortcut menu pmmenuitem popup1 = new menuitem ("popup1") generated by clicking the mouse; menuitem popup2 = new menuitem ("popup2 "); popup1.addactionlistener (this); popup2.addactionlistener (this); PM = new popupmenu (); PM. add (popup1); PM. add (popup2); // menubar Mb on the top menu bar of the architecture Enu1 and help are placed in the menu bar menubar MB = new menubar (); MB. add (menu1); MB. sethelpmenu (HELP); // and MB. add (HELP) has the same effect. // set the menu bar of F, a label for displaying text, and a shortcut menu PMF. setmenubar (MB); L = new label ("no menu item selected! "); F. add (PM); F. add (L, borderlayout. north); F. setsize (200,100); F. setvisible (true);} // The processing of Single-choice menu options is different from that of check menu options. One is actionreceivmed and the other is itemstatechangedpublic void actionreceivmed (actionevent e) {menuitem MI = (menuitem) E. getsource (); L. settext (MI. getlabel () + "selected! ");} Public void itemstatechanged (itemevent e) {checkboxmenuitem CMI = (checkboxmenuitem) E. getsource (); L. settext (CMI. getlabel () +" selected! ");} // Mouse event generated shortcut menu pmpublic void mouseclicked (mouseevent e) {PM. show (F, E. getx (), E. gety (); // display the shortcut menu popupmenu at the specified coordinate in f }}

4. Dialog

Package GUI; import Java. AWT. *; import Java. AWT. event. *;/* we used the frame class in the previous program. In fact, apart from the frame class, we inherited the window class and another class also inherited from the Windows * class, it is a dialog class used to report messages. * Basically, a dialog object cannot exist independently. It must be accompanied by a frame or dialog object. * The dialog constructor has three parameters, the first specifies the accompanied frame or dialog object, and the second can specify the title of the dialog, Which is * 3 specify the display mode */public class dialogexample extends windowadapter implements actionlistener {frame F; dialog D; checkbox ch; button B; public static void main (string argv []) {New dialogexample ();} public dialogexample () {f = new frame ("dialog example"); CH = new checkbox ("Modal"); B = new button ("show dialog"); B. addactionlistener (this); F. add (CH, borderlayout. north); F. add (B, borderlayout. center); F. pack (); F. setvisible (true);} public void actionreceivmed (actionevent e) {d = new dialog (F, "I'm a dialog", Ch. getstate (); // dialog constructor parameter D. addwindowlistener (this); // Add windowlistener listener for dialog to disable the dialog window. setsize (100,100); B. setenabled (false); // After the dialog is generated, the button becomes invalid. D. show ();} public void windowclosing (invalid wevent e) {d. dispose (); B. setenabled (true );}}

5. filedialog

Package GUI; import Java. AWT. *; import Java. AWT. event. *;/* 1. In the AWT package, only one system dialog box is provided, that is, the filedialog class. It will generate a dialog box for opening or saving files, allowing you to select a * file in the system, later, we will continue to introduce a group of components named swing German. The jdialog in the swing package provides a richer set of dialog box types * 2, in fact, the load and save modes only differ from the text displayed on the dialog box. The filedialog object is only used to select the file path and name, and does not actually perform access operations on the * file ,!!!! There are File Processing classes for file processing. The filedialog object can return the path name to the * file-related class, and then decide how to perform the operation. */Public class filedialogexample implements actionlistener {frame F; filedialog FD; checkbox chload, chsave; checkboxgroup CG; button B; label lbdir, lbfile; public static void main (string ARV []) {New filedialogexample ();} public filedialogexample () {f = new frame ("filedialog example"); FD = new filedialog (f); CG = new checkboxgroup (); chload = new checkbox ("LOAD", true, CG); chsave = new checkbox ("save", false, CG); Panel p1 = new Panel (); p1.add (chload); p1.add (chsave); lbdir = new label ("Directory:"); lbfile = new label ("file name :"); panel P2 = new Panel (New gridlayout (2, 1); p2.add (lbdir); p2.add (lbfile); B = new button ("show"); B. addactionlistener (this); F. add (P1, borderlayout. north); F. add (P2, borderlayout. center); F. add (B, borderlayout. south); F. pack (); F. setvisible (true);} public void actionreceivmed (actionevent e) {If (chload. getstate () FD. setmode (filedialog. load); elsefd. setmode (filedialog. save); FD. show (); // display the filedialog dialog box lbdir. settext ("Directory:" + FD. getdirectory (); // obtain the information of the selected file from the FD object lbfile. settext ("file name:" + FD. getFile (); F. pack ();}}

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.