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