Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

swing - Java Jbutton KeyListener

I have a 3x3 grid of jbuttons labeled 1-9 representing the number pad. I have added an actionlistener and keylistener which both call to the same function, so if they click btn1 or press 1 on the number pad the same thing happens.

The problem is when I press 1 on the numpad, I want to see btn1 press down with it, if that makes sense.

Searching didn't lead me to anything, is there a name for that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use:

  • The key bindings API instead of KeyListener. The only thing that the key binding needs to do, is call doClick on the associated button...
  • Use doClick to programmatically "click" the button

For example...

KeyPad

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author shane
 */
public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridLayout(3, 3));
            add(createButton("1", KeyEvent.VK_NUMPAD1));
            add(createButton("2", KeyEvent.VK_NUMPAD2));
            add(createButton("3", KeyEvent.VK_NUMPAD3));
            add(createButton("4", KeyEvent.VK_NUMPAD4));
            add(createButton("5", KeyEvent.VK_NUMPAD5));
            add(createButton("6", KeyEvent.VK_NUMPAD6));
            add(createButton("7", KeyEvent.VK_NUMPAD7));
            add(createButton("8", KeyEvent.VK_NUMPAD8));
            add(createButton("9", KeyEvent.VK_NUMPAD9));
        }

        protected JButton createButton(String name, int virtualKey) {
            JButton btn = new JButton(name);
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println(e.getActionCommand() + " was clicked");
                }
            });
            btn.setMargin(new Insets(8, 8, 8, 8));
            InputMap im = btn.getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = btn.getActionMap();
            im.put(KeyStroke.getKeyStroke(virtualKey, 0), "clickMe");
            am.put("clickMe", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JButton btn = (JButton) e.getSource();
                    btn.doClick();
                }
            });
            return btn;
        }

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...