When you click the yellow button in the window and press Ctrl + Y, the background color changes to yellow. Multiple event sources call the actionreceivmed () method of the same class.
CTRL + y yellow
CTRL + B green
CTRL + L Black
Related Classes:
public class KeyStrokeextends AWTKeyStroke
A keystroke represents a key action on the keyboard, or equivalent input device.
public abstract class AbstractActionextends Objectimplements Action, Cloneable, Serializable
This class provides default implementations for the jfcAction
Interface. Standard behaviors like the get and set methodsAction
Object properties (icon, text, and enabled) are defined here. The developer need only subclass this
Abstract class and defineactionPerformed
Method.
Related methods:
Jcomponent class
public final InputMap getInputMap(int condition)
ReturnsInputMap
That is usedcondition
.
condition
-One of when_in_focused_window, when_focused, when_ancestor_of_focused_component
public final ActionMap getActionMap()
ReturnsActionMap
Used to determine whatAction
To fire for ParticleKeyStroke
Binding. The returned
ActionMap
, Unless otherwise set, will haveActionMap
From the UI set as the parent.
Code:
Package CN. youthol; import Java. AWT. *; import javax. swing. *; import Java. AWT. event. *; public class main {/*** @ Param ARGs */public static void main (string [] ARGs) {eventqueue. invokelater (New runnable () {public void run () {myframe F = new myframe ("Change Background color"); F. setdefaclocloseoperation (myframe. exit_on_close); F. setvisible (true) ;}}}}/ *** Framework Window */class myframe extends jframe {private jpanel mainpanel; private final int window_width = 800; private Final int window_height = 600; Private Static string yellow = "toyellow"; Private Static string Blue = "toblue"; Private Static string black = "toblack"; Public myframe (String title) {super (title); setsize (window_width, window_height); mainpanel = new jpanel (); add (mainpanel); // set the button and action listener setcomponents ();} private void setcomponents () {// create listener action yellowlistener = new keylistener ("yellow", color. yellow); Action blacklistener = new keylistener ("black", color. black); Action bluelistener = new keylistener ("blue", color. blue); // create buttonjbutton btnyellow = new jbutton (yellowlistener); jbutton btnblack = new jbutton (blacklistener); jbutton btnblue = new jbutton (bluelistener); // Add buttonmainpanel. add (btnyellow); mainpanel. add (btnblack); mainpanel. add (btnblue); // set mapinputmap IMAP = mainpanel. getinputmap (jcomponent. when_ancestor_of_focused_component); IMAP. put (keystroke. getkeystroke ("Ctrl y"), myframe. yellow); IMAP. put (keystroke. getkeystroke ("Ctrl B"), myframe. blue); IMAP. put (keystroke. getkeystroke ("Ctrl l"), myframe. black); actionmap AMAP = mainpanel. getactionmap (); AMAP. put (myframe. yellow, yellowlistener); AMAP. put (myframe. blue, bluelistener); AMAP. put (myframe. black, blacklistener);}/** listener class */private class keylistener extends abstractaction {/** constructor */Public keylistener (string actionname, color C) {putvalue (action. name, actionname); // putvalue (action. short_description, "Change Background"); // This string will appear on the toolbar or button as a promptputvalue ("color", C ); // store the color object to "color", corresponding to the name/value}/** change the background color * @ see Java. AWT. event. actionlistener # actionreceivmed (Java. AWT. event. actionevent) */Public void actionreceivmed (actionevent e) {color C = (color) getvalue ("color"); // retrieve the colormainpanel. setbackground (c); // change the background color }}}