zoukankan      html  css  js  c++  java
  • e861. 在两个组件之间共享输入映射和事件映射

    By sharing an InputMap or ActionMap, any change to the shared InputMap or ActionMap will affect all components sharing the InputMap or ActionMap. WHEN_FOCUSED and WHEN_ANCESTOR_OF_FOCUSED_COMPONENT types of InputMaps can be shared. WHEN_IN_FOCUSED_WINDOW InputMaps cannot be shared.

        // Get an InputMap from the desired type of component and initialize it
        InputMap im = new JTextArea().getInputMap(JComponent.WHEN_FOCUSED);
        im.put(KeyStroke.getKeyStroke("F2"), "actionName");
        
        // Get an ActionMap from the desired type of component and initialize it
        ActionMap am =  new JTextArea().getActionMap();
        am.put("actionName",
            new AbstractAction("actionName") {
                public void actionPerformed(ActionEvent evt) {
                    process((JTextComponent)evt.getSource());
                }
            }
        );
        
        // Use the shared InputMap and ActionMap
        component1.setInputMap(JComponent.WHEN_FOCUSED, im);
        component2.setInputMap(JComponent.WHEN_FOCUSED, im);
        
        component1.setActionMap(am);
        component2.setActionMap(am);
        
        // Now, any change to the shared InputMap or ActionMap will affect both component1 and component2
        im.put(KeyStroke.getKeyStroke("F3"), "actionName2");
        am.put("actionName2",
            new AbstractAction("actionName2") {
                public void actionPerformed(ActionEvent evt) {
                    process((JTextComponent)evt.getSource());
                }
            }
        );
        
    
    Related Examples
  • 相关阅读:
    学生宿舍水电管理系统 产品需求评审(用户故事)
    nyoj 14-会场安排问题 (贪心)
    好看的鼠标hover效果
    JavaScript-三种弹窗方式
    博客园美化夜间模式
    js写个小时钟
    js获取时间,循环执行任务,延迟执行任务
    Bzoj1103 [POI2007]大都市meg
    POJ2155 Matrix
    POJ3625 Building Roads
  • 原文地址:https://www.cnblogs.com/borter/p/9596221.html
Copyright © 2011-2022 走看看