为什么这个动作不是抽象的? [英] Why isn't this action abstract?

查看:80
本文介绍了为什么这个动作不是抽象的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解为什么一个类中的一个Action是抽象的而另一个类中的Action不是抽象的。

I am having a hard time understanding why one of the Actions in a class is abstract and the Action in the other class not.

源代码1 :(错误编译时: https://gyazo.com/cd3c21a8562589451814903febaf89fe

Source code 1: (Error when compiling: https://gyazo.com/cd3c21a8562589451814903febaf89fe)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Play extends JFrame{

Engine drawPanel = new Engine();
private int x = 0;
private int y = 0;

public int getX(){
    return x;
}
public int getY(){
    return y;
}

public Play(){
    Action rightAction = new AbstractAction(){
        public void actionPreformed(ActionEvent e){
            x+=10;
            drawPanel.repaint();
        }
    };
    Action leftAction = new AbstractAction(){
        public void actionPreformed(ActionEvent e){
            x-=10;
            drawPanel.repaint();
        }
    };

        InputMap inputMap = drawPanel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = drawPanel.getActionMap();

    inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "rightAction");
    actionMap.put("rightAction", rightAction);
    inputMap.put(KeyStroke.getKeyStroke("LEFT"), "leftAction");
    actionMap.put("leftAction", leftAction);

    add(drawPanel);
    pack();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(640, 480);
    setTitle("Game");
    setLocationRelativeTo(null);
    setVisible(true);
}

public static void main(String[] args){

    EventQueue.invokeLater(new Runnable(){
        @Override
        public void run(){
            new Play();
        }
    });
}
}

源代码2 :(编译的代码很好)

Source code 2:(The code that compiles just fine)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class KeyBidings extends JFrame {
int x = 0;
int y = 0;

DrawPanel drawPanel = new DrawPanel();

public KeyBidings(){
    Action rightAction = new AbstractAction(){
        public void actionPerformed(ActionEvent e) {
            x +=10;
            drawPanel.repaint();
        }
    };

        InputMap inputMap = drawPanel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = drawPanel.getActionMap();

    inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "rightAction");
    actionMap.put("rightAction", rightAction);

    add(drawPanel);

    pack();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
}

private class DrawPanel extends JPanel {


    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.GRAY);
                g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.GREEN);
        g.fillRect(x, y, 50, 50);
    }

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

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable(){
        public void run(){
            new KeyBidings();
        }
    });
}
}


推荐答案

因为在第一个示例中,您输入错误:

Because in the first example you have a typing error:

actionPreformed

宁可

actionPerformed

这篇关于为什么这个动作不是抽象的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆