Swing


          天行健 君子以自強(qiáng)不息

          posts - 69, comments - 215, trackbacks - 0, articles - 16
             :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          java鍵盤(pán)事件

          Posted on 2007-05-26 10:28 zht 閱讀(6396) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Swing

          最近看到有人問(wèn)怎么模擬鋼琴操作
          想了想怎么實(shí)現(xiàn) 順便看了下鍵盤(pán)事件
          可以用兩種方式
          1、
           public class KeystrokeTest extends JApplet {
           private JButton button = new JButton("button");

           public void init() {
            Container contentPane = getContentPane();
            JPanel panel = new JPanel();
            JCheckBox checkbox = new JCheckBox("checkbox");
            JButton southButton = new JButton("south button");
            Listener listener = new Listener();

            panel.setBorder(BorderFactory
              .createTitledBorder(("Ancestor of button and checkbox")));

            checkbox.registerKeyboardAction(listener, KeyStroke.getKeyStroke(
              KeyEvent.VK_F, 0, false), JComponent.WHEN_FOCUSED);

            panel.registerKeyboardAction(listener, KeyStroke.getKeyStroke(
              KeyEvent.VK_A, InputEvent.ALT_MASK, false),
              JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

            southButton.registerKeyboardAction(listener, KeyStroke.getKeyStroke(
              KeyEvent.VK_W, 0, true), JComponent.WHEN_IN_FOCUSED_WINDOW);

            panel.add(button);
            panel.add(checkbox);

            contentPane.add(panel, "Center");
            contentPane.add(southButton, "South");
           }
          }

          class Listener implements ActionListener {
           public void actionPerformed(ActionEvent e) {
            Object src = e.getSource();
            String cname = src.getClass().getName();

            if (src instanceof JCheckBox) {
             System.out.print("'f' key PRESSED when checkbox");
             System.out.println(" had focus");
            } else if (src instanceof JPanel) {
             System.out.print("'ALT-a' key PRESSED when ancestor");
             System.out.println(" of titled panel had focus");
            } else if (src instanceof JButton) {
             System.out.print("'w' key RELEASED when any");
             System.out.println(" component in window had focus");
            }
            System.out.println("Source: " + cname);
            System.out.println();
           }
          }

          2、
          public class T {
           public static void main(String[] args) {
            try {
             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
            }

            final JTextField tf = new JTextField();
            tf.setEnabled(false);

            JPanel p = new JPanel(new GridLayout(0, 3, 5, 5));
            for (int i = 0; i <= 9; i++) {
             final JButton btn = new JButton(String.valueOf(i));
             p.add(btn);

             btn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
               KeyStroke.getKeyStroke((char) (i + '0')), "PressKeyAction");
             btn.getActionMap().put("PressKeyAction", new AbstractAction() {
              public void actionPerformed(ActionEvent e) {
               btn.doClick();
              }
             });

             btn.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
               tf.setText(tf.getText() + btn.getText());
              }
             });
            }

            JFrame f = new JFrame();
            JComponent contentPane = (JComponent) f.getContentPane();
            contentPane.setLayout(new BorderLayout(0, 5));
            contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

            f.getContentPane().add(p, BorderLayout.CENTER);
            f.getContentPane().add(tf, BorderLayout.NORTH);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
           }
          }

          看了下java的源代碼發(fā)現(xiàn)registerKeyboardAction方法其實(shí)就是把
          btn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
               KeyStroke.getKeyStroke((char) (i + '0')), "PressKeyAction");
             btn.getActionMap().put("PressKeyAction", new AbstractAction() {
              public void actionPerformed(ActionEvent e) {
               btn.doClick();
              }
             });封裝了一下


              public void registerKeyboardAction(ActionListener anAction,String aCommand,KeyStroke aKeyStroke,int aCondition) {

           InputMap inputMap = getInputMap(aCondition, true);

           if (inputMap != null) {
               ActionMap actionMap = getActionMap(true);
               ActionStandin action = new ActionStandin(anAction, aCommand);
               inputMap.put(aKeyStroke, action);
               if (actionMap != null) {
            actionMap.put(action, action);
               }
           }
              }
          不過(guò)api中提到
          registerKeyboardAction方法現(xiàn)在已過(guò)時(shí),對(duì)于類(lèi)似的操作,請(qǐng)結(jié)合使用 getActionMap()getInputMap(),即第二種方法為java推薦的方法
          好像在哪看到過(guò)java中不提倡使用addKeyListener();
          知道的可以說(shuō)以下

          主站蜘蛛池模板: 乌鲁木齐县| 林口县| 广宗县| 杭州市| 贵州省| 区。| 湘西| 稻城县| 互助| 宁晋县| 银川市| 陕西省| 遵义市| 茌平县| 克山县| 贵德县| 武隆县| 泗洪县| 清涧县| 卢氏县| 泽普县| 射阳县| 新闻| 大丰市| 安顺市| 苗栗市| 封丘县| 开江县| 合作市| 苏尼特右旗| 许昌县| 汤阴县| 清丰县| 崇左市| 临洮县| 易门县| 偏关县| 龙泉市| 阳山县| 揭西县| 丹江口市|