Java 我如何使一个按钮按下一个键?

Java 我如何使一个按钮按下一个键?,java,swing,Java,Swing,我想知道是否有可能在JButton中按一个键 例如,如果我有一个名为newbutton的按钮,我用鼠标点击它。我想让它按我的左箭头键 也有可能使它不断按下它,直到我放开鼠标吗?所以或多或少我点击它,它不断按下我的左箭头键,直到我释放鼠标,然后它停止 使用java.awt.Robot类执行此操作。这样做: //Creating a new robot: Robot r = new Robot(); //Pressing a key (Put inside click handler method

我想知道是否有可能在JButton中按一个键

例如,如果我有一个名为newbutton的按钮,我用鼠标点击它。我想让它按我的左箭头键


也有可能使它不断按下它,直到我放开鼠标吗?所以或多或少我点击它,它不断按下我的左箭头键,直到我释放鼠标,然后它停止

使用
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);

使用
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);

使用
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);

使用
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);
我点击它,它不断按下我的左箭头键,直到我释放鼠标,然后它停止

这有什么意义

如果使用键盘按左箭头,则击键将分派给具有焦点的组件。因此,如果焦点在文本字段上,左箭头将把插入符号向后移动一个字符

如果你点击一个按钮,焦点现在在按钮上,如果你把左箭头指向按钮,什么也不会发生

也许您正在尝试使用左箭头键来制作某种动画。如果是这样,则需要创建一个
操作
。然后,您需要添加代码,以便单击按钮或按左箭头键可以调用此操作

有关此方法的基本概念,请阅读。以下章节介绍:

  • 如何使用动作
  • 如何使用密钥绑定
  • 有关此方法的工作示例,您可以查看。
    MotionWithKeyBindings.java
    code使用键盘或按钮制作动画

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

    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();
                }
            });
        }
    }
    
    import java.awt.*;
    导入java.awt.event.*;
    导入javax.swing.*;
    导入javax.swing.border.*;
    公共类计算器面板扩展了JPanel
    {
    专用JTextField显示;
    公共计算器面板()
    {
    Action numberAction=new AbstractAction()
    {
    @凌驾
    已执行的公共无效操作(操作事件e)
    {
    //display.setCaretPosition(display.getDocument().getLength());
    display.replaceSelection(例如getActionCommand());
    }
    };
    setLayout(新的BorderLayout());
    display=新的JTextField();
    display.setEditable(false);
    display.setHorizontalAlignment(JTextField.RIGHT);
    添加(显示,BorderLayout.NORTH);
    JPanel buttonPanel=新的JPanel();
    setLayout(新的GridLayout(0,5));
    添加(按钮面板、边框布局、中心);
    对于(int i=0;i<10;i++)
    {
    String text=String.valueOf(i);
    JButton按钮=新JButton(文本);
    addActionListener(numberAction);
    button.setOrder(新行边框(颜色为黑色));
    按钮。设置首选尺寸(新尺寸(50,50));
    按钮面板。添加(按钮);
    InputMap InputMap=button.getInputMap(JComponent.WHEN_IN_FOCUSED_窗口);
    inputMap.put(击键.getKeyStroke(文本),文本);
    inputMap.put(KeyStroke.getKeyStroke(“NUMPAD”+text),text);
    button.getActionMap().put(文本、数字操作);
    }
    }
    私有静态void createAndShowUI()
    {
    //UIManager.put(“按钮边距”,新插图(10,10,10,10));
    JFrame=新的JFrame(“计算器面板”);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(新计算器面板());
    frame.pack();
    frame.setLocationRelativeTo(空);
    frame.setVisible(true);
    }
    公共静态void main(字符串[]args)
    {
    invokeLater(新的Runnable()
    {
    公开募捐
    {
    createAndShowUI();
    }
    });
    }
    }
    
    单击按钮,值将显示在文本字段中

    我点击它,它不断按下我的左箭头键,直到我释放鼠标,然后它停止

    这有什么意义

    如果使用键盘按左箭头,则击键将分派给具有焦点的组件。因此,如果焦点在文本字段上,左箭头将把插入符号向后移动一个字符

    如果你点击一个按钮,焦点现在在按钮上,如果你把左箭头指向按钮,什么也不会发生

    也许您正在尝试使用左箭头键来制作某种动画。如果是这样,则需要创建一个
    操作
    。然后,您需要添加代码,以便单击按钮或按左箭头键可以调用此操作

    有关此方法的基本概念,请阅读。以下章节介绍:

  • 如何使用动作
  • 如何使用密钥绑定
  • 有关此方法的工作示例,您可以查看。
    MotionWithKeyBindings.java
    code使用键盘或按钮制作动画

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

    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();
                }
            });
        }
    }
    
    import java.awt.*;
    导入java.awt.event.*;
    导入javax.swing*