Java context menu

Source: Internet
Author: User

Java swing's right-click pop-up menu, which is an example of copying, pasting, and cutting text boxes.

 

 

 

Import Java. AWT. datatransfer. clipboard; <br/> Import Java. AWT. datatransfer. dataflavor; <br/> Import Java. AWT. datatransfer. transferable; <br/> Import Java. AWT. event. actionevent; <br/> Import Java. AWT. event. actionlistener; <br/> Import Java. AWT. event. inputevent; <br/> Import Java. AWT. event. mouseevent; <br/> Import Java. AWT. event. mouselistener; </P> <p> Import javax. swing. jframe; <br/> Import javax. swing. jmenuit Em; <br/> Import javax. swing. jpopupmenu; <br/> Import javax. swing. jscrollpane; <br/> Import javax. swing. jtextarea; <br/> Import javax. swing. keystroke; <br/> Import javax. swing. uimanager; </P> <p>/** <br/> * the Java context menu allows you to copy, paste, and cut text components. <br/> */ </P> <p> public class test extends jframe {</P> <p> Private Static final long serialversionuid =-5942087991012920147l; </P> <p> private jscrollpane pane = NULL; </P> <P> private textareamenu text = NULL; </P> <p> Public test () {<br/> super ("right-click menu "); <br/> try {// windows UI style <br/> uimanager. setlookandfeel ("com. sun. java. swing. plaf. windows. windowslookandfeel "); <br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> text = new textareamenu (); <br/> pane = new jscrollpane (text); <br/> This. getcontentpane (). add (PANE); <br/> This. setdefaclocloseopera Tion (jframe. exit_on_close); <br/> This. setsize (300,200); <br/> This. setvisible (true); <br/>}</P> <p> Public static void main (string ARGs []) {<br/> new test (); <br/>}</P> <p>/** <br/> * jtextarea with function menus <br/> */<br/> class textareamenu extends jtextarea implements mouselistener {</P> <p> Private Static final long serialversionuid =-2308615404205560110l; </P> <p> private jpopupmenu Pop = NULL ;// Pop-up menu </P> <p> private jmenuitem copy = NULL, paste = NULL, Cut = NULL; // three function menus </P> <p> Public textareamenu () {<br/> super (); <br/> Init (); <br/>}</P> <p> private void Init () {<br/> This. addmouselistener (this); <br/> Pop = new jpopupmenu (); <br/> pop. add (copy = new jmenuitem ("copy"); <br/> pop. add (paste = new jmenuitem ("Paste"); <br/> pop. add (cut = new jmenuitem ("cut"); <br/> copy. setaccelerator (Keys Troke. getkeystroke ('C', inputevent. ctrl_mask); <br/> paste. setaccelerator (keystroke. getkeystroke ('V', inputevent. ctrl_mask); <br/> cut. setaccelerator (keystroke. getkeystroke ('x', inputevent. ctrl_mask); <br/> copy. addactionlistener (New actionlistener () {<br/> Public void actionreceivmed (actionevent e) {<br/> action (E); <br/>}< br/> }); <br/> paste. addactionlistener (New actionlistener () {<br/> Public void actionreceivmed (actionevent e) {<br/> action (E); <br/>}< br/>}); <br/> cut. addactionlistener (New actionlistener () {<br/> Public void actionreceivmed (actionevent e) {<br/> action (E); <br/>}< br/> }); <br/> This. add (POP ); <br/>}</P> <p>/** <br/> * menu action <br/> * @ Param e <br/> */<br/> public void action (actionevent E) {<br/> string STR = E. getactioncommand (); <br/> If (Str. equals (Copy. gettext () {// copy <br/> This. copy (); <br/>}else if (Str. equals (paste. gettext () {// paste <br/> This. paste (); <br/>} else if (Str. equals (cut. gettext () {// cut <br/> This. cut (); <br/>}</P> <p> Public jpopupmenu getpop () {<br/> return pop; <br/>}</P> <p> Public void setpop (jpopupmenu pop) {<br/> This. pop = pop; <br/>}</P> <p>/** <br/> * Whether the Clipboard contains text data that can be pasted <br/> *@ return True is text data <br/> */<br/> Public Boolean isclipboardstring () {<br/> Boolean B = false; <br/> clipboard = This. gettoolkit (). getsystemclipboard (); <br/> transferable content = clipboard. getcontents (this); <br/> try {<br/> If (content. gettransferdata (dataflavor. stringflavor) instanceof string) {<br/> B = true; <br/>}< br/>} catch (exception E) {<br/>}< br/> return B; <br/>}</P> <p>/ ** <Br/> * whether the text component has the copy condition <br/> * @ return true: <br/> */<br/> public Boolean iscancopy () {<br/> Boolean B = false; <br/> int start = This. getselectionstart (); <br/> int end = This. getselectionend (); <br/> If (start! = END) <br/> B = true; <br/> return B; <br/>}</P> <p> Public void mouseclicked (mouseevent E) {<br/>}</P> <p> Public void mouseentered (mouseevent e) {<br/>}</P> <p> Public void mouseexited (mouseevent E) {<br/>}</P> <p> Public void mousepressed (mouseevent e) {<br/> If (E. getbutton () = mouseevent. button3) {<br/> copy. setenabled (iscancopy (); <br/> paste. setenabled (isclipboardstring (); <br/> cut. setenabled (iscancopy (); <br/> pop. show (this, E. getx (), E. gety (); <br/>}</P> <p> Public void mousereleased (mouseevent E) {<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.