2013-04-26 62 views
1

我在JFrame中有一個按鈕。當我點擊按鈕時,我想將點擊事件與助記符相關聯。比方說,如果我點擊名爲Cut的按鈕,我想要執行ctrl+x並選擇要剪切的文本。按鈕單擊以在JFrame中按Ctrl鍵助記符

jButton1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) 
     { 


     } 
    }); 

要實現此目的要執行的操作是什麼?

回答

3

閱讀button.setMnemonic(KeyEvent.VK_A);更多的信息使用由編輯器工具包提供的默認操作:

JButton cut = new JButton(new DefaultEditorKit.CutAction()); 

這也可以用來創建JMenuItem。

1

答案很簡單 - 使用Actions

這將允許您在單個Action與任意數量的支持類,特別是從AbstractAction延長像,JMenuItemJButton