Java Event handling 2

Source: Internet
Author: User

Document Event

This event is a bit special, you need to return to the document you maintain with getdocument (), and then you can add a monitor

(textarea1.getdocument). Adddocumentlistener (Documentlistener Listen)

There are three methods of Documentlistener interface

 Public void changedupdate (documentevent e)  Public void removeupdate (documentevent e)  Public void insertupdate (documentevent e)

Word Sort code,

 Public classtest{ Public Static voidMain (String args[]) {Component Window1=NewComponent (); Window1.setbounds (40,40,300,200); }}classComponentextendsjframe{JTextArea textarea1,textarea2;    JMenu menu;    JMenuBar MenuBar;    JMenuItem Copyitem,cutitem,pasteitem;        Component () {init (); SetVisible (true);    Setdefaultcloseoperation (Jframe.exit_on_close); }    voidinit () {textArea1=NewJTextArea (6,10); TEXTAREA2=NewJTextArea (6,10);//Textarea1.setlinewrap (true);//Text wrap W//Textarea1.setwrapstyleword (true);//word-line wrap//Textarea2.setlinewrap (true);//Text Wrap Line//Textarea2.setwrapstyleword (true);//word-line wrapSetLayout (NewFlowLayout ()); CopyItem=NewJMenuItem ("Copy (c)")); Cutitem=NewJMenuItem ("Clip (t)"); Pasteitem=NewJMenuItem ("Paste (v)")); MenuBar=NewJMenuBar (); Menu=NewJMenu ("Edit");        Menubar.add (menu); Setjmenubar (MenuBar);//It's going to change a menu .menu.add (CopyItem);        Menu.add (Cutitem);        Menu.add (Pasteitem); Copyitem.setaccelerator (Keystroke.getkeystroke (' C '));//set shortcut keysCutitem.setaccelerator (Keystroke.getkeystroke (' t ')); Pasteitem.setaccelerator (Keystroke.getkeystroke (' V ')); Copyitem.setactioncommand ("Copy"); Copyitem.setactioncommand ("Cut"); Copyitem.setactioncommand ("Paste"); Add (NewJScrollPane (TEXTAREA1)); Add (NewJScrollPane (TEXTAREA2)); Dlistener DListener1=NewDlistener (TEXTAREA1,TEXTAREA2);        (Textarea1.getdocument ()). Adddocumentlistener (DListener1); Alistener AListener1=NewAlistener (TEXTAREA1,TEXTAREA2);        Copyitem.addactionlistener (AListener1);        Cutitem.addactionlistener (AListener1);    Pasteitem.addactionlistener (AListener1); }}classDlistenerImplementsdocumentlistener{JTextArea textarea1,textarea2; Dlistener (JTextArea A,jtextarea b) {textArea1=A; TEXTAREA2=C; }     Public voidchangedupdate (documentevent e) {String regex= "[\\s\\d\\p{punct}]+"; String[] Word=(Textarea1.gettext ()). Split (regex);        Arrays.sort (word); Textarea2.settext (NULL);  for(String A:word) {textarea2.append (a+","); }    }     Public voidRemoveupdate (Documentevent e) {//This is two more abstract methods, now set them to the same as abovechangedupdate (e); }     Public voidinsertupdate (documentevent e) {changedupdate (e); }}classAlistenerImplementsactionlistener{//shortcut key function is not debug toJTextArea textarea1,textarea2; Alistener (JTextArea A,jtextarea b) {textArea1=A; TEXTAREA2=b; }     Public voidactionperformed (ActionEvent e) {String a=E.getactioncommand (); if(A.equals ("Copy") ) textarea2.copy (); Else if(A.equals ("Cut") ) textarea2.cut (); Else if(A.equals ("Paste") ) Textarea1.paste (); }}

Java Event handling 2

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.