Using AWT and swing for desktop programming (1)

Source: Internet
Author: User

I. common controls in the AWT package

1. Label

Label (string label, int align)

2. textfield

Textfield (string text, int Cols)

3. Check box checkbox and single-choice button checkboxgroup

Checkbox (string label, Boolean state) <br/> // if the State is true, it indicates that the checkbox is selected, and vice versa. </P> <p>

4. Select box choice

5. Text domain class textarea

6. Button

Ii. event handling

1. Event

Common event types include actionevent, itemevent, keyevent, and mouseevent.

2. in Java, you can use the Monitor interface to process various events.

For example, you can rewrite actionevent by actionreceivmed () and itemstatechanged.

Iii. Framework and panel

1. Framework

The framework is a container-type control.

Main Methods:

1) frame (string strt) // construct the window

2) dispose () // undo window

2. Panel

The Panel class is also a container class control, but it is only a local

3. layout manager

1). borderlayout

In the border layout, the window is divided into five parts according to the southeast and northwest China. Each area has at most one control, and the control is filled with the whole area.

2). gridlayout

The grid layout places all controls in each grid. Each control is of the same size and is fully filled with the entire grid ".

Gridlayout (INT rows, int cols, int hgap, int vgap)

4. An example of a Java program using the AWT package

Import Java. AWT. event. *; <br/> Import Java. AWT. peer. panelpeer; <br/> Import Java. AWT. *; <br/> public class test4 extends frame implements actionlistener, itemlistener {</P> <p> label inform, login, key; <br/> textfield textlogin, textkey; </P> <p> checkbox box1, box2; <br/> checkboxgroup boxgroup; </P> <p> checkbox check1, check2, check3, check4, check5, check6, check7, check8; </P> <p> label name, lsex, holobby; <br/> textfield textname; </P> <p> label birthday; <br/> choice ychoice, MCHOICE, dchoice; </P> <p> textarea areainform; <br/> button benter, bcancel; </P> <p> string sex = "", year = "", month = "", Day = ""; <br/> string film = "", reading = "", network = "", program = "", <br/> art = "", tourism = "", game = "", others = ""; <br/> string strkey = ""; <br/> Boolean flag = true; </P> <p> Public test4 () <br/>{< br/> super ("User personal information"); </P> <p> Inform = new label ("Enter your personal information, account, password, click OK "); <br/> Add (inform, borderlayout. north); </P> <p> panel centerpanel = new Panel (); </P> <p> // Panel1 <br/> panel Panel1 = new Panel (); </P> <p> login = new label ("account:"); <br/> textlogin = new textfield ("", 6 ); <br/> key = new label ("password"); <br/> textkey = new textfield ("", 6); <br/> textkey. setechochar ('*'); <br/> panel1.add (LOGIN); <br/> panel1.add (textlogin); <br/> panel1.add (key ); <br/> panel1.add (textkey); <br/> centerpanel. add (Panel1); </P> <p> // panel2 <br/> panel panel2 = new Panel (); </P> <p> name = new label ("name:"); <br/> lsex = new label ("Gender :"); <br/> textname = new textfield ("", 6); </P> <p> boxgroup = new checkboxgroup (); <br/> box1 = new checkbox ("male", boxgroup, true); <br/> box2 = new checkbox ("female", boxgroup, false ); <br/> panel2.add (name); <br/> panel2.add (textname); <br/> panel2.add (lsex); <br/> panel2.add (box1 ); <br/> panel2.add (box2); <br/> centerpanel. add (panel2); </P> <p> // panel3 <br/> panel panel3 = new Panel (); <br/> Birthday = new label ("Birthday"); <br/> ychoice = New Choice (); <br/> ychoice. add ("1989"); <br/> ychoice. add ("1990"); <br/> MCHOICE = New Choice (); <br/> MCHOICE. add ("November"); <br/> MCHOICE. add ("December"); <br/> dchoice = New Choice (); <br/> dchoice. add ("11"); <br/> dchoice. add ("12"); <br/> ychoice. additemlistener (this); <br/> MCHOICE. additemlistener (this); <br/> dchoice. additemlistener (this); <br/> panel3.add (birthday); <br/> panel3.add (ychoice); <br/> panel3.add (MCHOICE ); <br/> panel3.add (dchoice); <br/> centerpanel. add (panel3); </P> <p> // panel4 <br/> panel panel4 = new Panel (); <br/> panel4.setlayout (New gridlayout (2, 5 )); <br/> Hober = new label ("interest"); <br/> check1 = new checkbox ("movie", false ); <br/> check2 = new checkbox ("read", false); <br/> check3 = new checkbox ("programming", false ); <br/> check4 = new checkbox ("network", false); <br/> check5 = new checkbox ("art", false ); <br/> check6 = new checkbox ("Travel", false); <br/> check7 = new checkbox ("game", false ); <br/> check8 = new checkbox ("other", false); <br/> check1.additemlistener (this); <br/> check2.additemlistener (this ); <br/> check3.additemlistener (this); <br/> check4.additemlistener (this); <br/> check5.additemlistener (this); <br/> check6.additemlistener (this ); <br/> check7.additemlistener (this); <br/> check8.additemlistener (this); <br/> panel4.add (holobby); <br/> panel4.add (check1 ); <br/> panel4.add (check2); <br/> panel4.add (check3); <br/> panel4.add (check4); <br/> panel4.add (new label ()); <br/> panel4.add (check5); <br/> panel4.add (check6); <br/> panel4.add (check7); <br/> panel4.add (check8 ); <br/> centerpanel. add (panel4); </P> <p> areainform = new textarea ("", 3,30); <br/> centerpanel. add (areainform); </P> <p> Add (centerpanel); </P> <p> panel bpanel = new Panel (); <br/> benter = new button ("OK"); <br/> bcancel = new button ("cancel"); <br/> benter. addactionlistener (this); <br/> bcancel. addactionlistener (this); <br/> bpanel. add (benter); <br/> bpanel. add (bcancel); <br/> Add (bpanel, borderlayout. south); </P> <p> setsize (260,320); <br/> setvisible (true ); <br/>}</P> <p> Public void actionreceivmed (actionevent AE) <br/>{< br/> areainform. settext (""); <br/> If (AE. getsource () = benter) <br/>{< br/> If (flag = true) <br/>{< br/> flag = false; <br/> areainform. append ("name:" + textname. gettext () + "\ n"); <br/> areainform. append ("Gender:" + lsex + "\ n"); <br/> areainform. append ("birthday:" + year + month + day + "\ n"); <br/> areainform. append ("Hobbies:" + film + reading + network + program + Art + tourism + Game + others + "\ n"); </P> <p> inform. settext ("enter the password again, and then click OK"); <br/> strkey = textkey. gettext (); <br/> textkey. settext (""); <br/>}< br/> else <br/>{< br/> If (textkey. gettext (). equals (strkey) <br/> inform. settext ("successfully registered"); <br/> else <br/> inform. settext ("enter the password again, and then click OK "); </P> <p >}< br/> else <br/>{< br/> system. exit (0); <br/>}< br/> Public void itemstatechanged (itemevent IE) <br/>{< br/> If (ie. getitemselectable () = ychoice) <br/> year = ychoice. getselecteditem (); </P> <p> If (ie. getitemselectable () = MCHOICE) <br/> month = MCHOICE. getselecteditem (); </P> <p> If (ie. getitemselectable () = ychoice) <br/> day = dchoice. getselecteditem (); </P> <p> If (box1.getstate () <br/> sex = box1.getlabel (); </P> <p> If (box2.getstate ()) <br/> sex = box2.getlabel (); </P> <p> If (check1.getstate () <br/> film = check1.getlabel (); <br/> else <br/> film = ""; </P> <p> If (check2.getstate () <br/> reading = check2.getlabel (); <br/> else <br/> reading = ""; </P> <p> If (check3.getstate () <br/> network = check3.getlabel (); <br/> else <br/> network = ""; </P> <p> If (check4.getstate () <br/> program = check4.getlabel (); <br/> else <br/> program = ""; </P> <p> If (check5.getstate () <br/> art = check5.getlabel (); <br/> else <br/> art = ""; </P> <p> If (check6.getstate () <br/> tourism = check6.getlabel (); <br/> else <br/> tourism = ""; </P> <p> If (check7.getstate () <br/> game = check7.getlabel (); <br/> else <br/> game = ""; </P> <p> If (check8.getstate () <br/> others = check8.getlabel (); <br/> else <br/> others = ""; </P> <p >}< br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> New test4 (); <br/>}</P> <p >}< br/>

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.