your_button.setMnemonic(java.awt.event.KeyEvent.VK_C);

解决方案 »

  1.   

    少打几个字
    button.setMnemonic( 'c' );当然可以有两种 void setMnemonic(char mnemonic)
              This method is now obsolete, please use setMnemonic(int) to set the mnemonic for a button.
     void setMnemonic(int mnemonic)
              Sets the keyboard mnemonic on the current model.
      

  2.   

    这种方式的快截键是用ALT组合的,,
      

  3.   

    //OK?
    import javax.swing.*;
    import java.awt.event.*;public class JMenuItem3e extends JFrame{ JTextArea theArea = null; public JMenuItem3e(){
      
      super("JMenuItem3e");
      theArea = new JTextArea();
      theArea.setEditable(false);
      getContentPane().add(new JScrollPane(theArea));
      JMenuBar MBar = new JMenuBar();
      MBar.setOpaque(true);
      
      JMenu mfile = buildFileMenu();   MBar.add(mfile);  
      setJMenuBar(MBar);
    }//end of JMenuItem3e() public JMenu buildFileMenu() {
      
      JMenu thefile = new JMenu("File");
      thefile.setMnemonic('F');   JMenuItem newf = new JMenuItem("New",new ImageIcon("icons/new24.gif"));
      JMenuItem open = new JMenuItem("Open",new ImageIcon("icons/open24.gif"));
      JMenuItem close= new JMenuItem("Close",new ImageIcon("icons/close24.gif"));    
      JMenuItem quit = new JMenuItem("Exit",new ImageIcon("icons/exit24.gif"));   newf.setMnemonic('N');
      open.setMnemonic('O');
      close.setMnemonic('L');
      quit.setMnemonic('X');
      
      newf.setAccelerator( KeyStroke.getKeyStroke('N', java.awt.Event.CTRL_MASK, false) );
      open.setAccelerator( KeyStroke.getKeyStroke('O', java.awt.Event.CTRL_MASK, false) );
      close.setAccelerator( KeyStroke.getKeyStroke('L', java.awt.Event.CTRL_MASK, false) );
      quit.setAccelerator( KeyStroke.getKeyStroke('X', java.awt.Event.CTRL_MASK, false) );   thefile.add(newf);   
      thefile.add(open);
      thefile.add(close);        
      thefile.addSeparator();
      thefile.add(quit);   return thefile;
    }//end of buildFileMenu()

    public static void main(String[] args){   JFrame F = new JMenuItem3e();
      F.setSize(400,200);
      F.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0); 
        }
      });//end of addWindowListener
      F.setVisible(true); 
    } // end of main
    }//end of class JMenuItem3e
      

  4.   

    用KeyStroke可以实现,你看一下吧:)ActionListener action=new ActionListener(){
      public void actionPerformed(ActionEvent e){
                System.out.println("^G has pressed.");
      }
    };
    KeyStroke ks=KeyStroke.getKeyStroke(KeyEvent.VK_G,InputEvent.CTRL_MASK,false);//Ctrl+G 
    InputMap map  = jButton4.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    map.put(ks,"key");//这个"key"可以是任意字符串,但是不能重复,就好像是Hashtable中的key - value一样,而下面actionmap中put进去的action就是它的value;
    jButton4.getActionMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
    .put("key",action);
      

  5.   

    谢谢!这种方案对于menu 来说,的确好用,但对于BUTTON ,,没有
    setAccelerator
    方法
      

  6.   

    说点别的:)快捷键定义成“Ctrl+C”不太合理吧。
    因为系统默认的Ctrl+C是拷贝功能啊。
      

  7.   

    我的方法是从最顶层容器开始遍历底下的组件,给每一个组件加上同
    一个KeyListener自己判断处理.