在jframe中有一个jbutton按钮,想按ctrl+enter组合键响应事件,怎么处理
我用了setMnemonic但是这个只能实现alt+enter!高手帮忙,最好用例子!

解决方案 »

  1.   

    写个KeyListener,比如MyKeyListener
    public void keyPressed(KeyEvent e) {
    if(e.getModifiers()==InputEvent.CTRL_MASK&&e.getKeyCode()==KeyEvent.VK_ENTER){
    doSomething();
    }
    }
    KeyListener keyL=new MyKeyListener();
    添加方法public void setKeyListener(Component com) {
    com.addKeyListener(keyL);
    if (com instanceof Container) {
    Container con = (Container) com;
    for (int i = 0; i < con.getComponentCount(); i++) {
    Component c = con.getComponent(i);
    setKeyListener(c);
    }
    }
    }
    调用setKeyListener(button.getTopLevelAncestor());记住在button添加到frame后在调用这个
      

  2.   

    等jframe所有的组件都添加完后再调用
      

  3.   

    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.   

    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
      

  5.   

    用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);