1

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

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

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

4

2 回答 2

2

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

于 2013-11-04T18:25:46.060 回答
1

更改了您的代码,它可以正常工作。覆盖paintComponent方法并在其中调用您的draw方法。并更改JFrameJPanel.

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

  }
于 2013-11-04T18:29:09.740 回答