Java:使用带箭头键的按键 [英] Java: Use keystroke with arrow key

查看:460
本文介绍了Java:使用带箭头键的按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些我需要修改的代码。在代码中,原作者使用 KeyStroke.getKeyStroke 来获取用户输入。例如,在这段代码中,他使用 a 而不是左箭头。

I have some code that I need to modify. In the code, the original author uses KeyStroke.getKeyStroke to take user input. In this code, for example, he uses a instead of left arrow.

我想改变这个,但是我不知道怎么做。

I want to change this, but I don't know how.

以下是原始代码:

registerKeyboardAction(
        new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tick(RIGHT);
            }
        }, "right", KeyStroke.getKeyStroke('d'), WHEN_IN_FOCUSED_WINDOW
);

我必须将其更改为类似的东西,但运行时,它不起作用:
KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT);

I have to change it to something like this, but when run, it doesn't work: KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT);

KeyStroke.getKeyStroke( 正确);

推荐答案

DOWN启动程序 ARROW KEY ,首先观察字符串。下面看一下这个示例程序:

Do start the program by pressing the DOWN ARROW KEY, to watch the string first. Here have a look at this example program :

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

public class KeyBindingExample
{
    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("Key Binding Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        DrawingPanel contentPane = new DrawingPanel();
        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        contentPane.requestFocusInWindow();
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new KeyBindingExample().createAndDisplayGUI();
            }
        });
    }
}

class DrawingPanel extends JPanel
{
    private int x;
    private int y;
    private String[] commands = {
                                    "UP",
                                    "DOWN",
                                    "LEFT",
                                    "RIGHT"
                                };                      

    private ActionListener panelAction = new ActionListener()
    {   
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            String command = (String) ae.getActionCommand();
            if (command.equals(commands[0]))
                y -= 1;             
            else if (command.equals(commands[1]))
                y += 1;
            else if (command.equals(commands[2]))
                x -= 1;
            else if (command.equals(commands[3]))
                x += 1;

            repaint();  
        }
    };

    public DrawingPanel()
    {
        x = 0;
        y = 0;

        for (int i = 0; i < commands.length; i++)       
            registerKeyboardAction(panelAction,
                            commands[i],
                            KeyStroke.getKeyStroke(commands[i]),
                            JComponent.WHEN_IN_FOCUSED_WINDOW);
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(500, 300));
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        String displayText = "X : " + x + " and Y : " + y;
        System.out.println(displayText);
        g.drawString(displayText, x, y);
    }
}

这篇关于Java:使用带箭头键的按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆