0

ボタンが押されたときにアクションを実行する Java のメソッドはありますか? キーパッドのボタン '5' を押すと、GUI の説明 '5' でボタンが起動される KeyListener を作成したいので、これが必要です。

編集

私はActionListenerを手に入れました:

public void actionPerformed(ActionEvent event){
    if(button.getLabel().equals("1")){
     // do something
    }
 }

とKeyListener

public void keyPressed(KeyEvent e){
  if (e.getKeyCode() == VK_NUMPAD1) {
        //    do what would happen if I pressed the mouse on the button
        //    I do not know how to execute it thought that it was pressing down the 
        // button with the label '1 '
  }
}
4

2 に答える 2

0

以下の例は問題ありません。if 文の中に入れなければならないのは次のとおりです。

if (e.getKeyCode() == VK_NUMPAD5) {
    nameOfButtonToClick.doClick();
}

あなたのボタンはおそらくフィールド値として宣言されているので、明らかにそれらは同じスコープ内になければなりませんか?

于 2014-04-18T11:28:03.573 に答える