Java:如何使用箭头键进行按键操作

6

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

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

下面是原始代码:

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

我必须将它改为像下面这样的内容,但是运行时没有反应: KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT); 你可以试试这个: KeyStroke.getKeyStroke("RIGHT");

我认为registerKeyboardAction()已经过时了一段时间。 - Catalina Island
2个回答

7

按下向下箭头启动程序,首先观看字符串。以下是一个示例程序:

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);
    }
}

5
你可以使用 KeyStroke.getKeyStroke("DOWN");"UP""LEFT""RIGHT" 来实现你想要的功能。
详细信息请参见javadoc

我忘记在我的帖子中包括这种情况。没有错误,但仍然无法正常工作。 :( - hqt
@hqt tick(RIGHT);是什么意思?因为它在我的代码中有效,为了更快地获得帮助,请发布一个[SSCCE](http://sscce.org/)。 - mKorbel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接