1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| Action actionBouton1 = new AbstractAction("1") {
public void actionPerformed(ActionEvent e) {
// code à faire si on appuie sur 1
System.out.println("bouton 1");
}
};
Action actionBouton2 = new AbstractAction("2") {
public void actionPerformed(ActionEvent e) {
// code à faire si on appuie sur 2
System.out.println("bouton 2");
}
};
InputMap inputMap = new InputMap();
inputMap.put(KeyStroke.getKeyStroke('1'),
"touche1");
inputMap.put(KeyStroke.getKeyStroke('2'),
"touche2");
ActionMap actionMap = new ActionMap();
actionMap.put("touche1",
actionBouton1);
actionMap.put("touche2",
actionBouton2);
JButton bout1 = new JButton(actionBouton1);
JButton bout2 = new JButton(actionBouton2);
bout1 .setInputMap(JButton.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);
bout1 .setActionMap(actionMap);
bout2 .setInputMap(JButton.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);
bout2 .setActionMap(actionMap); |
Partager