【问题标题】:the circle drawn by graphic class doesnot move图形类画的圆不动
【发布时间】:2019-11-29 23:03:31
【问题描述】:

我想通过箭头移动一个圆圈,但是当尝试这样做时它不会移动我认为 KeyReleased 代码的问题 我使 vx 的值发生变化,因此 x 值也发生变化,并且与 y 相同,这使圆圈移动我试图在 vx 和 vy 上增加而不是为它们分配值,但它仍然是同样的问题

 public class Class2
 extends JPanel
 implements ActionListener, KeyListener

{
int x = 0, vx = 0, y = 0, vy = 0;
Timer t = new Timer(5, this);

public Class2()
{
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.red);
g.fillOval(x, y, 50, 50);
}

@Override
 public void actionPerformed(ActionEvent e)
 {
  x += vx;
  y += vy;
  repaint();
  }

  @Override
  public void keyReleased(KeyEvent e)
 {
  int c = e.getKeyCode();
if (c == KeyEvent.VK_LEFT)
{
  vx = -1;
  vy = 0;
}
if (c == KeyEvent.VK_RIGHT)
{
  vx = 1;
  vy = 0;
}
if (c == KeyEvent.VK_UP)
{
  vx = 0;
  vy = 1;
}
if (c == KeyEvent.VK_DOWN)
{
  vx = 0;
  vy = -1;
}
}

【问题讨论】:

  • 你怎么知道值被更新了?我怀疑您已经违反了 KeyListener 的众所周知且记录在案的焦点相关问题,这通常使其成为此类操作的糟糕选择 - 您应该改用键绑定 API

标签: java swing actionlistener keylistener


【解决方案1】:

问题的可能原因是使用了KeyListener。当组件不需要键盘焦点并且可用输入数量有限时,这通常是一个糟糕的选择

Key bindings API 修复了这些问题,让您可以更好地控制组件何时应接收键盘事件,例如...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new Class2());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Class2
            extends JPanel
            implements ActionListener {

        int x = 0, vx = 0, y = 0, vy = 0;
        Timer t = new Timer(5, this);

        public Class2() {
            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();

            am.put("left", new LeftAction());
            am.put("right", new RightAction());
            am.put("up", new UpAction());
            am.put("down", new DownAction());

            // Stop movement when key is released
            am.put("stopHorizontal", new StopHorizontalAction());
            am.put("stopVertical", new StopVerticalAction());

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "left");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "right");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "up");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "down");

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true), "stopHorizontal");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), "stopHorizontal");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), "stopVertical");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "stopVertical");

            t.start();
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.red);
            g.fillOval(x, y, 50, 50);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            x += vx;
            y += vy;
            repaint();
        }

        protected class StopHorizontalAction extends AbstractAction {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                vx = 0;
            }
        }

        protected class StopVerticalAction extends AbstractAction {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                vy = 0;
            }
        }

        protected class MovementAction extends AbstractAction {

            private Integer xDelta;
            private Integer yDelta;

            public MovementAction(Integer xDelta, Integer yDelta) {
                this.xDelta = xDelta;
                this.yDelta = yDelta;
            }

            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (xDelta != null) {
                    vx += xDelta;
                }
                if (yDelta != null) {
                    vy += yDelta;
                }
            }

        }

        protected class LeftAction extends MovementAction {

            public LeftAction() {
                super(-1, null);
            }

        }

        protected class RightAction extends MovementAction {

            public RightAction() {
                super(1, null);
            }

        }

        protected class UpAction extends MovementAction {

            public UpAction() {
                super(null, -1);
            }

        }

        protected class DownAction extends MovementAction {

            public DownAction() {
                super(null, 1);
            }

        }

    }
}

【讨论】:

    猜你喜欢
    • 2014-11-28
    • 2017-06-14
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多