简体   繁体   中英

JButton with actionPerformed and keyReleased

我有一个 JButton,我希望它在按下按钮时做一些事情,我希望它在按下一个键时做同样的事情,我该怎么做?

To do something when a button is pressed you should add an ActionListener to that button like this:

JButton button = new JButton();
button.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
});

And to response to key pressed do something like this : (for example if user enters control alt 7)

Action actionListener = new AbstractAction() {
  public void actionPerformed(ActionEvent actionEvent) {
    JButton source = (JButton) actionEvent.getSource();
    System.out.println("Activated: " + source.getText());// just for test
  }
};
//.....

KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
InputMap inputMap = button.getInputMap();
inputMap.put(controlAlt7, ACTION_KEY);
ActionMap actionMap = button.getActionMap();
actionMap.put(ACTION_KEY, actionListener);

As far as I know (haven't messed with Swing in a couple of years...) the only key button events that can act upon a button are Tab which changes the element focus, Enter which fires the actionPerformed method and mnemonics, which also fire the actionPerformed .

To handle events on button clicks, you could do something like so:

 button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
            }
        });  

You may want to look into the Action API. You can see more atHow to Use Actions . You can use the Action for buttons (works like an ActionListener ) and you can add key shortcuts to it.

You can see this example where key shortcuts are added to the toolbar button as well as the menu items. The interesting thing you mat want to notice is the the menu item and the tool bar button share the same Action and thus do the same thing.

在此处输入图片说明

    JButton button = new JButton("Submit");
    //Add action listener to button
    button.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
            //do your work here
            System.out.println("You clicked the submit button");
        }
    });      

This is how to use actionlistener with jbutton

Try this:

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

public class EventedButtonExample extends JFrame {
    private JButton simpleButton;
    private static final long serialVersionUID = 42L;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                EventedButtonExample me = new EventedButtonExample();
                me.initialize();
                me.setVisible(true);
            }
        });
    }

    void initialize() {
        simpleButton = new JButton("I am an simple, evented button!");
        CompoundEventHandler eh = new CompoundEventHandler();
        simpleButton.addActionListener(eh);
        simpleButton.addKeyListener(eh);
        getContentPane().add(simpleButton, BorderLayout.CENTER);
        pack();
    }

    class CompoundEventHandler extends KeyAdapter implements ActionListener {
        public void keyReleased(KeyEvent e) {
            int keyCode = e.getKeyCode();
            System.out.println("Key pressed: " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")");
        }

        public void actionPerformed(ActionEvent e) {
            System.out.println("A button is pressed!");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM