有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing Java:使用带有箭头键的按键

我有一些代码需要修改。在代码中,原始作者使用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");


共 (2) 个答案

  1. # 1 楼答案

    您应该能够使用KeyStroke.getKeyStroke("DOWN");"UP""LEFT""RIGHT"来做您想做的事情

    有关更多详细信息,请参见javadoc

  2. # 2 楼答案

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

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