在 Java 摆动窗口中控制形状移动的问题



我正在尝试制作一个程序,您可以使用箭头键在java摆动窗口中移动圆圈。 键绑定工作正常,但显示圆圈总是有问题。代码如下:

public class ShapesMove extends JFrame{
    public static int x = 40;
    public static int y = 40;
    public static void main(String[] args){
        final JFrame frame = new JFrame("Movement of 2d Shapes");
        frame.setSize(400,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel content = (JPanel) frame.getContentPane();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        Action actionRight = new AbstractAction(){
            public void actionPerformed(ActionEvent actionRightEvent){
                x++;
            }
        };
        Action actionLeft = new AbstractAction(){
            public void actionPerformed(ActionEvent actionLeftEvent){
                x--;
            }
        };
        Action actionUp = new AbstractAction(){
            public void actionPerformed(ActionEvent actionUpEvent){
                y++;
            }
        };
        Action actionDown = new AbstractAction(){
            public void actionPerformed(ActionEvent actionDownEvent){
                y--;
            }
        };
        KeyStroke right = KeyStroke.getKeyStroke("RIGHT");
        KeyStroke left = KeyStroke.getKeyStroke("LEFT");
        KeyStroke up = KeyStroke.getKeyStroke("UP");
        KeyStroke down = KeyStroke.getKeyStroke("DOWN");
        InputMap inputMap = content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(right, "RIGHT");
        inputMap.put(left, "LEFT");
        inputMap.put(up, "UP");
        inputMap.put(down, "DOWN");
        content.getActionMap().put("RIGHT", actionRight);
        content.getActionMap().put("LEFT", actionLeft);
        content.getActionMap().put("UP", actionUp);
        content.getActionMap().put("DOWN", actionDown);
    }
    public void draw(Graphics g){
        g.drawOval(x, y, 60, 60);
    }
}

我没有包括导入行,因为我知道我有所有正确的模块。编译总是很好,但是当我运行它时,圆圈不会显示。我在它自己的单独 fie 中尝试了相同的代码,当我运行它时,圆圈出现了,所以我在这里做错了什么?

您需要覆盖paintComponent才能进行绘制。 在添加到JFrame的组件上执行此操作,而不是在框架本身上执行此操作,因为JFrame是一个具有 contentPane 的容器,这将使事情变得更加复杂,并且对于进一步修改的灵活性更低。

更改了您的代码,它工作正常。重写paintComponent方法并在其中调用draw方法。并将JFrame更改为JPanel.

    public class ShapesMove extends JPanel{
    public static int x = 40;
    public static int y = 40;
    public static void main(String[] args){
        final JFrame frame = new JFrame("Movement of 2d Shapes");
        frame.setSize(400,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final ShapesMove m = new ShapesMove();
        frame.getContentPane().add(m);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        Action actionRight = new AbstractAction(){
            public void actionPerformed(ActionEvent actionRightEvent){
                x++;
                m.repaint();
            }
        };
        Action actionLeft = new AbstractAction(){
            public void actionPerformed(ActionEvent actionLeftEvent){
                x--;
                m.repaint();
            }
        };
        Action actionUp = new AbstractAction(){
            public void actionPerformed(ActionEvent actionUpEvent){
                y++;
                m.repaint();
            }
        };
        Action actionDown = new AbstractAction(){
            public void actionPerformed(ActionEvent actionDownEvent){
                y--;
                m.repaint();
            }
        };
        KeyStroke right = KeyStroke.getKeyStroke("RIGHT");
        KeyStroke left = KeyStroke.getKeyStroke("LEFT");
        KeyStroke up = KeyStroke.getKeyStroke("UP");
        KeyStroke down = KeyStroke.getKeyStroke("DOWN");
        InputMap inputMap = m.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(right, "RIGHT");
        inputMap.put(left, "LEFT");
        inputMap.put(up, "UP");
        inputMap.put(down, "DOWN");
        m.getActionMap().put("RIGHT", actionRight);
        m.getActionMap().put("LEFT", actionLeft);
        m.getActionMap().put("UP", actionUp);
        m.getActionMap().put("DOWN", actionDown);
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawOval(x, y, 60, 60);
    }
  }

最新更新