0

I'm writing my first calculator in Java Swing and I would like, when I press for example "1" of my keyboard, it execute the ActionListener of my button.

No "ALT-1" or "CTRL-1", just the key "1".

Here is an example of my buttons :

 n0.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String nombre0 = Integer.toString(nb0);
            textFieldAns.setText(textFieldAns.getText() + nombre0);
        }
    });

I don't know if you want an another parts of my code

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Antoine
  • 13
  • 1
  • 5
  • 1
    Possible duplicate of [Add key bindings to JButtons that get their actions from action commands?](https://stackoverflow.com/questions/33601591/add-key-bindings-to-jbuttons-that-get-their-actions-from-action-commands) – Bleach Nov 16 '18 at 12:47
  • See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses a `KeyListener` to detect keyboard activity. – Andrew Thompson Nov 16 '18 at 12:48
  • See: https://stackoverflow.com/questions/33739623/how-to-add-a-shortcut-key-for-a-jbutton-in-java/33739732#33739732. It demonstrates how to use Key Bindings to share a common Action. – camickr Nov 16 '18 at 15:55
  • I try all of your tips and liks but nothing work :/ – Antoine Nov 19 '18 at 10:07

0 Answers0