2012-05-14 1 views
2

Je veux faire un programme avec ces objectifs:En réponse à bouton en utilisant les raccourcis clavier

1) Faire un JButton 2) Fixez le bouton à une touche (la touche "A") à l'aide KeyBindings 3) Exécuter une code lorsque « A » est cliqué

Voici le code que j'ai jusqu'à présent:

// Imports 

Public class Test{ 

JButton button = new JButton(); 

//... 

Test(){ 

button.getInputMap().put(KeyStroke.getKeyStroke("A"), "Pressed"); 


//... 

} 

// Where do I add the code that responds when button is pressed? 
} 

maintenant, où dois-je ajouter le code que je veux exécuter lorsque le bouton est pressé?

Répondre

2

vous devez ajouter un écouteur d'action, spécifiquement pour actionPerformed. déclarer quelque part dans votre constructeur:

import java.awt.event.ActionEvent; 
import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.KeyStroke; 

public class Main { 
    public static void main(String[] argv) throws Exception { 

     JButton component = new JButton(); 
     MyAction action = new MyAction(); 
     component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("F2"), 
      action.getValue(Action.NAME)); 
    } 
} 

class MyAction extends AbstractAction { 
    public MyAction() { 
     super("my action"); 
    } 

    public void actionPerformed(ActionEvent e) { 
     //Here goes the code where the button does something 
     System.out.println("hi");//In this case we print hi 
    } 
} 

Dans cet exemple, si on appuie sur F2, il sera l'équivalent de la touche.

+0

si j'ai plus d'un bouton? Est-ce que j'utilise la même méthode actionPerformed? – Anonymous181

+0

créer une autre classe, dans ce cas son nom est MyAction et nous l'instancions en action, nous pouvons créer une nouvelle classe pour chaque bouton. –

6

Deux façons je peux penser:

  • Ayant JButton et et touches du clavier partagent la même AbstractAction, ou peut-être mieux
  • Appelez simplement doClick() sur le bouton de la liaison clés.

KeyBindingEg.java

import java.awt.event.*; 
import javax.swing.*; 

public class KeyBindingEg extends JPanel { 
    private JButton btnA = new JButton(); 

    public KeyBindingEg() { 
     Action btnAction = new ActionOne("A"); 
     Action keyBindingAction = new ActionTwo(); 

     int condition = JLabel.WHEN_IN_FOCUSED_WINDOW; 
     InputMap inputmap = btnA.getInputMap(condition); 
     ActionMap actionmap = btnA.getActionMap(); 

     final String aKeyPressed = "a key pressed"; 
     inputmap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), aKeyPressed); 

     actionmap.put(aKeyPressed, keyBindingAction); 
     // actionmap.put(aKeyPressed, btnAction); // one or the other, your choice 

     btnA.setAction(btnAction); 
     add(btnA); 
    } 

    private class ActionOne extends AbstractAction { 
     public ActionOne(String text) { 
     super(text); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
     sharedMethod(); 
     } 
    } 

    private class ActionTwo extends AbstractAction { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
     btnA.doClick(); 
     } 
    } 

    private void sharedMethod() { 
     System.out.println("Method called by either key bindings or action listener"); 
    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("KeyBindingEg"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new KeyBindingEg()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 
Questions connexes