【问题标题】:how to move an object in a JPanel using the arrow keys如何使用箭头键在 JPanel 中移动对象
【发布时间】:2015-07-15 22:06:06
【问题描述】:

我正在尝试使用 Windowbuilder 创建一个小程序,它只是在 JPanel 中绘制一个红色矩形(称为 car1)并通过按箭头键移动它;为此,我与箭头关联了一个更改 x 位置的方法,调用了 repaint 方法,但矩形根本不移动 - 因此我可能会用 KeyEvent 和/或 repaint 搞砸一些东西。

每次按正确的箭头键移动并刷新面板时,我应该怎么做才能使矩形?

public class Car extends JPanel {
    int x;
    int y;

    public Car(int x,int y){
        this.x=x;
        this.y=y;
    }


    public void paint(Graphics g) {
        g.setColor(Color.RED);
        g.fillRect(x, y, 20, 20);
    }

    public void move_right(){
        x=x+20;
    }

    public void move_left(){
        x=x-20;
    }

}



public class Form extends JFrame {

    //private JPanel contentPane;
    Car car1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Form frame = new Form();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Form() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 727, 550);
        getContentPane().setLayout(null);
        car1 = new Car(350, 480);
        car1.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
                int key = e.getKeyCode();
                if (key == KeyEvent.VK_KP_LEFT) {
                    car1.move_left();
                    car1.repaint();
                }
                if (key == KeyEvent.VK_KP_RIGHT) {
                    car1.move_right();
                    car1.repaint();
                }
            }
        });
        car1.setBounds(0, 0, 700, 500);
        car1.setBackground(new Color(255, 255, 255));
        getContentPane().add(car1);
    }

}

【问题讨论】:

    标签: java swing keylistener


    【解决方案1】:

    至少有3个问题:

    1. 使用KeyListenerKeyListener 以仅响应发生在可聚焦且具有键盘焦点的组件上的关键事件而闻名。 JPanel 默认情况下是不可聚焦的,因此它不能接收键盘焦点。更好的解决方案是使用键绑定 API,它允许在触发绑定之前定义组件必须具有的焦点级别,并允许您为多个键重用 Action,从而减少代码重复
    2. 覆盖paint。强烈建议在执行自定义绘画时覆盖paintComponent 而不是绘画。您也未能维护油漆链,这将导致无穷无尽的奇怪而美妙的油漆工件。 Painting in AWT and SwingPerforming Custom Painting 了解更多详情
    3. 使用null 布局。避免使用null 布局,像素完美的布局是现代 ui 设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起使用,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正

    例如...

    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.ActionMap;
    import javax.swing.InputMap;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private int xPos;
    
            public TestPane() {
                Action leftAction = new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        xPos -= 2;
                        if (xPos < 0) {
                            xPos = 0;
                        }
                        repaint();
                    }
                };
                Action rightAction = new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        xPos += 2;
                        if (xPos + 10 > getWidth()) {
                            xPos = getWidth() - 10;
                        }
                        repaint();
                    }
                };
    
                bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), leftAction);
                bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.left", KeyStroke.getKeyStroke(KeyEvent.VK_KP_LEFT, 0), leftAction);
                bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.left", KeyStroke.getKeyStroke(KeyEvent.VK_4, 0), leftAction);
                bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.left", KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), leftAction);
    
                bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.right", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), rightAction);
                bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.right", KeyStroke.getKeyStroke(KeyEvent.VK_KP_RIGHT, 0), rightAction);
                bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.right", KeyStroke.getKeyStroke(KeyEvent.VK_6, 0), rightAction);
                bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.right", KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), rightAction);
            }
    
            protected void bindKeyStroke(int condition, String name, KeyStroke keyStroke, Action action) {
                InputMap im = getInputMap(condition);
                ActionMap am = getActionMap();
    
                im.put(keyStroke, name);
                am.put(name, action);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                int yPos = (getHeight() - 10) / 2;
                g2d.drawRect(xPos, yPos, 10, 10);
                g2d.dispose();
            }
    
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      这是一个解决方案:

      addKeyListener(new KeyAdapter() {
          public void keyPressed(KeyEvent e) {
              int key = e.getKeyCode();
              if (key == KeyEvent.VK_LEFT) {
                  car1.move_left();
                  car1.repaint();
              }
              if (key == KeyEvent.VK_RIGHT) {
                  car1.move_right();
                  car1.repaint();
              }
          }
      });
      

      哪里出错了:

      1. addKeyListener:将关键监听器添加到框架,而不是面板
      2. VK_KP_:改用VK_前缀
      3. keyTyped:改用keyPressed

      您必须解决的下一步是删除以前的矩形

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-23
        相关资源
        最近更新 更多