2016-04-06 5 views
-2

私は、クラス内のアクションの1つが抽象的で、他のクラスのアクションがなぜそうでないのかを理解するのに苦労しています。なぜこのアクションは抽象的ではありませんか?

ソースコード1:(コンパイルエラー: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:(うまくコンパイルコード)

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

答えて

1

最初の例では、入力を持っているのでエラー:

actionPerformed 
+0

今私は大きなばかげた馬鹿のように感じますが、うまくいきました。ありがとう!私はそれができるようになるとすぐに受け入れます。 –

関連する問題