2012-02-16 96 views
2

如何聽按鍵並觸發JButton?Java聽按鈕和鍵盤單擊

例如:

我有 「A」 的JButton作爲面板上的GUI。我在「aButton」上實現了一個buttonListener,它會將屏幕更改爲其他內容。我希望這個按鈕可以通過點擊鼠標和鍵盤來觸發。

如何在觸發此「A」JButton的同時按下鍵盤上的「a」,同時還實現了buttonListener?

我目前的代碼無法觸發按鍵事件。

ButtonListener listener; 
KeyboardListener keyboardListener; 
private JButton aButton; 

public MyButtonPanel() { 
    listener = new ButtonListener(); 

    aButton = new JButton ("A"); 
    aButton.setFont (BUTTON_TEXT); 
    aButton.setPreferredSize (new Dimension (60,30)); 
    aButton.addActionListener (listener); 
    aButton.addKeyListener (keyboardListener); 

    setLayout (new BorderLayout()); 
    add (aButton, BorderLayout.CENTER); 

} 

private class KeyboardListener implements KeyListener 
{ 
    public void keyPressed(KeyEvent arg0) { 
     char c = arg0.getKeyChar(); 
     System.out.println("Pressed " + c); 
    } 

    public void keyReleased(KeyEvent arg0) { 
     char c = arg0.getKeyChar(); 
     System.out.println("Released " + c); 
    } 

    public void keyTyped(KeyEvent arg0) { 
     char c = arg0.getKeyChar(); 
     System.out.println("Typed " + c); 
    } 

} 

private class ButtonListener implements ActionListener { 
    public void actionPerformed (ActionEvent event) { 
    Object source = event.getSource(); 

    if (source == aButton) { 
     System.out.println("This is a"); 
    } 
    } 
} 

}

+1

請閱讀有關助記符的在這裏http://docs.oracle.com/javase/tutorial/uiswing/components/button.html 有一些JButton鍵盤筆畫鏈接的例子 – Sean 2012-02-16 15:51:34

+0

我明白了所以我可以使用像這樣b3.setMnemonic(KeyEvent.VK_E); b3.setActionCommand(「enable」); – nowhere 2012-02-16 16:07:50

回答

5

創建操作。然後通過使用ActionListener將Action添加到按鈕。通過將Action綁定到KeyStroke來處理鍵盤事件。

閱讀Swing tutorial。有一節:

  1. 如何使用操作
  2. 如何使用按鍵綁定

,讓你開始。

或者你也可以只是指定一個助記符來調用按鈕。閱讀JButton API。

+0

我的不好,只是看着你的最後一行。只是我現在不能刪除我的答案,我的不好。它說我今天不能刪除我的答案。雖然信息+1。 – 2012-02-16 16:22:56

1

還有一種方法可以實現您想要的功能,只需將註釋行添加到您的代碼部分即可。

aButton = new JButton ("A"); 
aButton.setFont (BUTTON_TEXT); 
// Doing this will also allow you to use your Keyboard for this JButton. You have to 
// press ALT+A in this case to make it work. 
//aButton.setMnemonic(KeyEvent.VK_A); 

如果你只有一個按鈕,你是關心你可以使按鈕在這種情況下,默認的JButton過,這樣做:

yourFrameObject.getRootPane().setDefaultButton(aButton); 

現在上的輸入按鍵將按照其actionPerformed()方法中的描述完成工作。

注意:默認按鈕功能的確切實現取決於外觀和感覺。例如,在Windows的外觀和感覺中,默認按鈕更改爲具有焦點的按鈕,以便按下Enter鍵單擊焦點按鈕。當沒有按鈕具有焦點時,最初指定爲默認按鈕的按鈕再次成爲默認按鈕。

+0

感謝您的信息..對於這個基本問題抱歉:有沒有辦法讓助記符沒有按ALT鍵激活? – nowhere 2012-02-16 16:31:33

+0

@nowhere:好像KeyBinding就是這樣的答案。否則,如果它只是您關心的一個按鈕,請將該按鈕設置爲默認值,如我在答案的下半部分所述。使您的按鈕成爲默認設置將使其按一下ENTER鍵即可工作。只是我的答案中提到的一行會爲你做這個技巧。助記符意味着與ALT工作,我猜:-) – 2012-02-16 16:35:37

+0

我明白了。好吧,我會看看KeyBinding並在代碼上嘗試。我需要實現多個按鈕。這只是開始。 :)但謝謝你的默認按鈕的解釋。 – nowhere 2012-02-16 16:41:54

1

按順序使用鍵綁定將輸入鍵與特定組件綁定。 你的情況,你可以使用

aButton.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),"pressed"); 
aButton.getActionMap().put("pressed",btnAction); 

其中pressed是特定部件與btnAction這是一個行動的參考鏈接的關鍵字。可以使用AbstractAction()創建一個操作,您可以在其中指定要執行的操作。 如果你想在同一組件響應鼠標點擊,你可以使用的方法

aButton.addActionListener(btnAction); 

因爲ActionActionListener接口擴展,這將不會導致出現問題。