700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 基于AWT Swing的GUI编程 - 多个命令共享同一个监听器类

基于AWT Swing的GUI编程 - 多个命令共享同一个监听器类

时间:2020-03-02 09:13:08

相关推荐

基于AWT Swing的GUI编程 - 多个命令共享同一个监听器类

用户点击窗口中的yellow按钮、用户按下Ctrl + Y 键,都会改变背景颜色至黄色。多个事件源调用的都是同一个类的actionPerformed()方法.

Ctrl + Y 黄

Ctrl + B 绿

Ctrl + L 黑

相关类:

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 methods forActionobject properties (icon, text, and enabled) are defined here. The developer need only subclass this abstract class and define theactionPerformedmethod.

相关方法:

JComponent类

public finalInputMapgetInputMap(intcondition)

Returns theInputMapthat is used duringcondition.

condition- one of WHEN_IN_FOCUSED_WINDOW, WHEN_FOCUSED, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT

public finalActionMapgetActionMap()

Returns theActionMapused to determine whatActionto fire for particularKeyStrokebinding. The returnedActionMap, unless otherwise set, will have theActionMapfrom the UI set as the parent.

代码:

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("改变背景颜色");f.setDefaultCloseOperation(MyFrame.EXIT_ON_CLOSE);f.setVisible(true);}});}}/*** 框架窗口*/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);//设置Button和动作监听器setComponents();}private void setComponents(){//创建监听器Action yellowListener = new KeyListener("Yellow",Color.YELLOW);Action blackListener = new KeyListener("Black",Color.BLACK);Action blueListener = new KeyListener("Blue",Color.BLUE);//创建ButtonJButton btnYellow = new JButton(yellowListener);JButton btnBlack = new JButton(blackListener);JButton btnBlue = new JButton(blueListener);//添加ButtonmainPanel.add(btnYellow);mainPanel.add(btnBlack);mainPanel.add(btnBlue);//设置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);}/** 监听器类*/private class KeyListener extends AbstractAction{/** 构造方法*/public KeyListener(String actionName,Color c){putValue(Action.NAME,actionName); //动作名putValue(Action.SHORT_DESCRIPTION,"改变背景"); //这个字符串会出现在工具栏或按钮上,做为一个promptputValue("color",c); //把Color对象存储到"color"中,名/值对应}/** 改变背景颜色* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)*/public void actionPerformed(ActionEvent e){Color c = (Color)getValue("color"); //取出ColormainPanel.setBackground(c); //改变背景颜色}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。