J2se interface Layout

Source: Internet
Author: User
Tags gety

Recently, I have been viewing j2se interface layout and event control, for example, processing list right-click events on the interface.

 

Mouselistener-mouse listener
Tool

Actionlistener -- parent class of all listeners

Itemlistener -- used to capture events generated by components with items

 

Sample Code:

 

Import Java. AWT. *; <br/> Import Java. AWT. event. *; <br/> Import Java. applet. applet; <br/> public class listdemo extends applet implements mouselistener, actionlistener, <br/> itemlistener {<br/> private textarea output; <br/> private list mainlist, filelist; <br/> private string newline; <br/> private popupmenu popmenu = new popupmenu (); <br/> Public void Init () {<br/> newline = system. getproperty ("line. SEP Arator "); <br/> // <br/> mainlist = new list (); // <br/> mainlist. add ("system information"); <br/> mainlist. add ("transaction member"); <br/> mainlist. add ("failed message processing"); <br/> mainlist. add ("download file list"); <br/> mainlist. addactionlistener (this); <br/> mainlist. additemlistener (this); <br/> mainlist. addmouselistener (this); <br/> string itemtext [] = {"start", "stop" };< br/> for (INT I = 0; I <itemtext. length; I ++) {<br/> menuitem item = new menuitem (I Temtext [I]); <br/> item. addactionlistener (this); <br/> popmenu. add (item); <br/>}< br/> // <br/> filelist = new list (); <br/> filelist. add ("http: // 127.0.0.1/test/test.txt"); <br/> filelist. addactionlistener (this); <br/> filelist. additemlistener (this); <br/> // <br/> gridbaglayout gridbag = new gridbaglayout (); <br/> setlayout (gridbag ); <br/> // <br/> gridbagconstraints lc = new gridbagconstraints (); <br/> LC. fi LL = gridbagconstraints. both; <br/> LC. weightx = 1.0; <br/> LC. weighty = 1.0; <br/> LC. gridheight = 2; <br/> gridbag. setconstraints (mainlist, LC); <br/> Add (mainlist); <br/> gridbag. setconstraints (filelist, LC); <br/> Add (filelist); <br/> output = new textarea (10,100); <br/> output. seteditable (false); <br/> gridbagconstraints Tc = new gridbagconstraints (); <br/> TC. fill = gridbagconstraints. both; <br/> TC. gr Idx = 0; <br/> TC. gridy = 2; <br/> TC. weightx = 1.0; <br/> TC. weighty = 1.0; <br/> TC. gridwidth = gridbagconstraints. remainder; // end row <br/> gridbag. setconstraints (output, Tc); <br/> Add (output); <br/> Add (popmenu ); <br/>}< br/> Public void actionreceivmed (actionevent e) {<br/> List list = (list) (E. getsource (); <br/> string Language = (list = mainlist )? "Main Menu": "file list"; <br/> output. append ("Action event occurred on/" "+ list. getselecteditem () <br/> + "/" in "+ language + ". "+ newline); <br/>}< br/> Public void itemstatechanged (itemevent e) {<br/> List list = (list) (E. getitemselectable (); <br/> string Language = (list = mainlist )? "Main Menu": "file list"; <br/> int Index = (integer) (E. getitem ())). intvalue (); <br/> If (E. getstatechange () = itemevent. selected) {<br/> output. append ("selected index =" + index + "(/" "<br/> + list. getitem (INDEX) + "/") in "+ language + ". "<br/> + newline); <br/>} else {// the item was deselected <br/> output. append ("deselected Index = #" + index + "(/" "<br/> + list. getitem (INDEX) + "/") in "+ language + ". "<br/> + newline); <br/>}< br/> Public void mouseclicked (mouseevent E) {<br/> // todo auto-generated method stub <br/>}< br/> Public void mouseentered (mouseevent E) {<br/> // todo auto-generated method stub <br/>}< br/> Public void mouseexited (mouseevent E) {<br/> // todo auto-generated method stub <br/>}< br/> Public void mousepressed (mouseevent E) {<br/> // todo auto-generated method stub <br/>}< br/> Public void mousereleased (mouseevent E) {<br/> // todo auto-generated method stub <br/> If (E. ispopuptrigger () {<br/> system. out. println ("e. getcomponent () "+ This +" E. getx () "<br/> + E. getx () + "E. gety () "+ E. gety (); <br/> popmenu. show (E. getcomponent (), E. getx (), E. gety (); <br/>}< 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.