如何按下按键?

时间:2015-03-19 21:23:25

标签: java swing

我想知道是否可以在JButton中按一个按键?

所以例如,如果我有一个标题为New Button的按钮,我用鼠标点击它。我希望它按下我的左箭头键。

还有可能使它一直按下它直到我放开鼠标?所以我或多或少地点击它并且它连续按下我的左箭头键直到我释放鼠标然后停止?

2 个答案:

答案 0 :(得分:3)

  

我点击它并连续按下我的左箭头键直到我松开鼠标然后停止?

这有什么意义?

如果使用键盘按左箭头,则会将KeyStroke调度到具有焦点的组件。因此,如果焦点位于文本字段上,则左箭头会将插入符号移回一个字符。

如果你点击一个按钮,现在焦点在按钮上,如果你向左按箭头发送按钮,就不会发生任何事情。

也许您正在尝试使用左箭头键来执行某种动画。如果是,那么您需要创建Action。然后,您需要添加代码,以便单击按钮或按下左箭头键可以调用此操作。

有关此方法的基本概念,您可以阅读Swing Tutorial。有以下部分:

  1. 如何使用行动
  2. 如何使用键绑定
  3. 有关此方法的工作示例,您可以查看Motion Using the KeyboardMotionWithKeyBindings.java代码使用键盘或按钮执行动画。

    你正试图这样做吗?这是一个简单的“计算器”键盘:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class CalculatorPanel extends JPanel
    {
        private JTextField display;
    
        public CalculatorPanel()
        {
            Action numberAction = new AbstractAction()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
    //              display.setCaretPosition( display.getDocument().getLength() );
                    display.replaceSelection(e.getActionCommand());
                }
            };
    
            setLayout( new BorderLayout() );
    
            display = new JTextField();
            display.setEditable( false );
            display.setHorizontalAlignment(JTextField.RIGHT);
            add(display, BorderLayout.NORTH);
    
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout( new GridLayout(0, 5) );
            add(buttonPanel, BorderLayout.CENTER);
    
            for (int i = 0; i < 10; i++)
            {
                String text = String.valueOf(i);
                JButton button = new JButton( text );
                button.addActionListener( numberAction );
                button.setBorder( new LineBorder(Color.BLACK) );
                button.setPreferredSize( new Dimension(50, 50) );
                buttonPanel.add( button );
    
                InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
                inputMap.put(KeyStroke.getKeyStroke(text), text);
                inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text);
                button.getActionMap().put(text, numberAction);
            }
        }
    
        private static void createAndShowUI()
        {
    //      UIManager.put("Button.margin", new Insets(10, 10, 10, 10) );
    
            JFrame frame = new JFrame("Calculator Panel");
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.add( new CalculatorPanel() );
            frame.pack();
            frame.setLocationRelativeTo( null );
            frame.setVisible(true);
        }
    
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowUI();
                }
            });
        }
    }
    

    单击按钮,该值将显示在文本字段中。

答案 1 :(得分:0)

使用java.awt.Robot课程执行此操作。这样做:

//Creating a new robot:
Robot r = new Robot();

//Pressing a key (Put inside click handler method):
r.keyPress(KeyEvent.VK_LEFT /*VK_RIGHT, VK_TOP, and VK_BOTTOM are also acceptable.*/

//Releasing a key (Put inside release handler method):
r.keyRelease(KeyEvent.VK_LEFT /*VK_RIGHT, VK_TOP, and VK_BOTTOM are also acceptable.*/); //Release key

执行此操作时,您可能希望保持按下的键的值。通过定义具有KeyEvent.VK_*值的全局变量来执行此操作,如下所示:

//In global space
public static /*Can be private, or protected.*/ int keyPressed = null;

//In click  handler body:
keyPressed = KeyEvent.VK_LEFT /*VK_RIGHT, VK_TOP, and VK_BOTTOM are also acceptable.*/; //Set this to the value of the key you are pressing.

//In mouse release handler body:
r.keyRelease(keyPressed);