GUI keys bound to keyboard and print components

Source: Internet
Author: User

Let's start by stating

Keys bind to keyboard and set shortcut keys are not the same

The key-bound keyboard is a button-like function that can be used when there is no focus (When_in_focused_window), and sometimes as a single event (with its own independent abstractaction subclass).

Set shortcut keys tightly set shortcut keys (a bit farfetched)

Keyboard facts using the monitor is more stringent, is the ActionListener sub-interface action, but the AbstractAction class has helped me realize, we just need to extends abstractaction as before, Then rewrite actionperformed ()

InputMap Inputmap1=button1.getinputmap (Jcomponent.when_in_focused_window); // Get mapping inputmap1.put (Keystroke.getkeystroke ("A"), "dog"); // before getting the key, the following parameter is a mapped keyword // Specifies that the monitor has two statements for the keyboard action of a button ActionMap Actionmap1=button1.getactionmap ()/// get a ActionMap object that can be added to the monitor actionmap.put ("Dog", Listener); // finally added a monitor--

Full code

classMywinextendsJFrameImplementsactionlistener{JButton button1;        Mywin () {init (); SetVisible (true);    Setdefaultcloseoperation (Jframe.exit_on_close); }    voidinit () {setlayout (NewFlowLayout ()); Button1=NewJButton ("click");        Add (button1); Add (NewJTextField (8)); Actionpolice ActionPolice1=NewActionpolice (); InputMap Inputmap1=Button1.getinputmap (jcomponent.when_focused); Inputmap1.put (Keystroke.getkeystroke ("A"), "dog"); ActionMap Actionmap1=Button1.getactionmap (); Actionmap1.put ("Dog", ActionPolice1); }}classActionpoliceextendsabstractaction{ Public voidactionperformed (ActionEvent e) {JButton button1=(JButton) E.getsource (); intx=button1.getbounds (). x; inty=button1.getbounds (). Y; Button1.setlocation (x+10,y+10); }}

About the Print Components section

Unfortunately, don't know what it is, the code has a puzzling problem

Ignore it, hand in the code and run.

classMywinextendsJFrameImplementsactionlistener{PrintJob P=NULL; Graphics g=NULL; JTextArea textArea1=NewJTextArea (10,10); JButton button1=NewJButton ("text box"), button2=NewJButton ("window"), button3=NewJButton ("button"); Mywin () {Super("DFFSD");        Init (); SetVisible (true);    Setdefaultcloseoperation (Jframe.exit_on_close); }    voidinit () {//setlayout (New FlowLayout ());Button1.addactionlistener ( This); Button2.addactionlistener ( This); Button3.addactionlistener ( This); Add (TEXTAREA1,"Center"); JPanel pane1=NewJPanel ();        Pane1.add (button1);        Pane1.add (button2);        Pane1.add (Button3); Add (Pane1,"South"); }     Public voidactionperformed (ActionEvent e) {if(E.getsource () = =button1) {P= ((Component) E.getsource ()). Gettoolkit (). Getprintjob ( This, "OK",NULL); G=P.getgraphics (); G.translate (120, 200);            Textarea1.printall (g);            G.dispose ();        P.end (); }        if(E.getsource () = =button2) {P=gettoolkit (). Getprintjob ( This, "OK",NULL); G=P.getgraphics (); G.translate (120, 200);  This. printall (g);            G.dispose ();        P.end (); }        if(E.getsource () = =button2) {P=gettoolkit (). Getprintjob ( This, "OK",NULL); G=P.getgraphics (); G.translate (120,200);            Button1.printall (g); G.translate (78, 0);            Button2.printall (g); G.translate (66, 0);            Button3.printall (g);            G.dispose ();        P.end (); }    }

GUI keys bound to keyboard and print components

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.