Gui Programming Based on AWT and swing-multiple commands share the same listener class

Source: Internet
Author: User
Tags imap

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 jfcActionInterface. Standard behaviors like the get and set methodsActionObject properties (icon, text, and enabled) are defined here. The developer need only subclass this
Abstract class and defineactionPerformedMethod.

Related methods:

Jcomponent class

public final InputMap getInputMap(int condition)

ReturnsInputMapThat is usedcondition.

condition-One of when_in_focused_window, when_focused, when_ancestor_of_focused_component

public final ActionMap getActionMap()

ReturnsActionMapUsed to determine whatActionTo fire for ParticleKeyStrokeBinding. The returned
ActionMap, Unless otherwise set, will haveActionMapFrom 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 }}}

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.