2014-12-03 67 views
0

我有一個3x3的jbuttons網格,標記爲1-9,代表數字鍵盤。我添加了一個actionlistener和keylistener,它們都調用相同的函數,所以如果他們點擊btn1或在數字鍵盤上按1,則會發生同樣的情況。Java Jbutton KeyListener

問題是當我在數字鍵盤上按1時,我想看到btn1按下它,如果這是有道理的。

搜索沒有引導我什麼,有沒有一個名稱?

+1

代碼在哪裏? – BitNinja 2014-12-03 23:57:19

+1

您是否嘗試過調用'doClick'?另外,避免使用'KeyListener'並使用鍵綁定API。 [如何使用鍵綁定](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) – MadProgrammer 2014-12-04 00:04:11

+0

我不認爲你會需要代碼,我正在尋找一種方法。 doClick()而不調用該按鈕的actionlistener。讓我知道如果你真的需要代碼 – user3562657 2014-12-04 00:08:04

回答

5

用途:

  • 鍵綁定API,而不是KeyListener。該鍵的結合需要做的唯一的事情,是對相關的按鈕呼叫doClick ...
  • 使用doClick編程「點擊」按鈕

例如...

KeyPad

import java.awt.EventQueue; 
import java.awt.GridLayout; 
import java.awt.Insets; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import javax.swing.AbstractAction; 
import javax.swing.ActionMap; 
import javax.swing.InputMap; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.KeyStroke; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

/** 
* 
* @author shane 
*/ 
public class Test { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new GridLayout(3, 3)); 
      add(createButton("1", KeyEvent.VK_NUMPAD1)); 
      add(createButton("2", KeyEvent.VK_NUMPAD2)); 
      add(createButton("3", KeyEvent.VK_NUMPAD3)); 
      add(createButton("4", KeyEvent.VK_NUMPAD4)); 
      add(createButton("5", KeyEvent.VK_NUMPAD5)); 
      add(createButton("6", KeyEvent.VK_NUMPAD6)); 
      add(createButton("7", KeyEvent.VK_NUMPAD7)); 
      add(createButton("8", KeyEvent.VK_NUMPAD8)); 
      add(createButton("9", KeyEvent.VK_NUMPAD9)); 
     } 

     protected JButton createButton(String name, int virtualKey) { 
      JButton btn = new JButton(name); 
      btn.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        System.out.println(e.getActionCommand() + " was clicked"); 
       } 
      }); 
      btn.setMargin(new Insets(8, 8, 8, 8)); 
      InputMap im = btn.getInputMap(WHEN_IN_FOCUSED_WINDOW); 
      ActionMap am = btn.getActionMap(); 
      im.put(KeyStroke.getKeyStroke(virtualKey, 0), "clickMe"); 
      am.put("clickMe", new AbstractAction() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        JButton btn = (JButton) e.getSource(); 
        btn.doClick(); 
       } 
      }); 
      return btn; 
     } 

    } 

}