1

正直なところ、何が間違っているのかわかりません。

キー イベントが登録される場合と登録されない場合があります。
つまり、Frogger2 を実行すると、イベントが登録されることもあれば、何も登録されないこともあります。

キーイベントが登録されるかどうかは、完全にランダムに見えます。通常、しばらく何もテストせずに Frogger2 を実行すると、イベントが登録され、それを閉じてまったく同じプログラムを再実行すると、イベントが発生しません。

助けてください。

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class Frogger2 extends JPanel {

public static JFrame frame;
public static Frogger2 F;

public Frogger2() {}

public Dimension getPreferredSize() { return new Dimension(500,500); }
public void paintComponent(Graphics g) { super.paintComponent(g); }


private int fdir;

public void moveLeft() {
    travel(2);
}
public void moveRight() {
    travel(0);
}
public void moveUp() {
    travel(3);
}
public void moveDown() {
    travel(1);
}
private void travel(int ddd) {
    System.out.println(ddd);
}


private boolean step() {
    System.out.println("FDIR: "+fdir);
    return true;
}

public void start() {
    fdir = 2;
    while(true) {
        boolean a = step();
        if (!a) break;
        try {
            Thread.sleep(25);
        } catch(Exception e) {}
    }
}

public static void main(String[] args) {
    frame = new JFrame("Frogger");

    F = new Frogger2();
    F.setDoubleBuffered(true);
    frame.add(F);
    frame.pack();
    frame.setVisible(true);
    frame.setResizable(false);
    JPanel abc = (JPanel)frame.getContentPane();
    abc.getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "left");
    abc.getActionMap().put("left", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {F.moveLeft();}
    });
    abc.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "right");
    abc.getActionMap().put("right", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {F.moveRight();}
    });
    abc.getInputMap().put(KeyStroke.getKeyStroke("UP"), "up");
    abc.getActionMap().put("up", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {F.moveUp();}
    });
    abc.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "down");
    abc.getActionMap().put("down", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {F.moveDown();}
    });

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    F.start();

}

}
4

1 に答える 1

5

この問題は通常、パネルにフォーカスがあるときに発生します。キーを押すたびに、パネルにフォーカスがあることを確認する必要があります。@camickrがコメントで言及しているように、キー
バインド の概要については Java チュートリアルを参照してください。@mKorbelが回答した質問 も参照してください(非常に興味深い回答)。 キーバインディングで興味深いのは、 + + を押すとプログラムが何らかの操作を行うとします。これは非常に興味深いことであり、キーバインディングを使用することを強くお勧めします。スタックに関する 私の最初の質問もこのテーマに関するものでした。 これは私がいくつかの変更を加えたあなたのコードです:


CtrlShiftSpace

  • KeyListenerキーを押すたびにフォーカスがパネルに置かれます。
  • MouseListenerマウスがパネルに入るか、マウスがパネルをクリックすると、再びフォーカスがパネルに置かれます。
  • FocusListener:パネルがフォーカス可能かどうかを確認します。

コードは次のとおりです。

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class MovingJPanel {

    public static void main(String[] args) {
    JFrame frame = new JFrame("Frogger");

    frame.pack();
    frame.setVisible(true);
    frame.setResizable(true);
    frame.setLocationRelativeTo(null);
    frame.setSize(400,400);
    JPanel abc = new JPanel();
    abc.setBackground(Color.CYAN);
    abc.getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "left");
    abc.getActionMap().put("left", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("LEFT");}
    });
    abc.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "right");
    abc.getActionMap().put("right", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("ROIGHT");}
    });
    abc.getInputMap().put(KeyStroke.getKeyStroke("UP"), "up");
    abc.getActionMap().put("up", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("UP");}
    });
    abc.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "down");
    abc.getActionMap().put("down", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("DOWN");}
    });

    abc.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent e) {
            System.out.println("Focus Gained");
        }
        @Override
        public void focusLost(FocusEvent e) {
            System.out.println("Focus Lost");
        }  
});
    abc.addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
            JPanel p = (JPanel)e.getSource();
            p.requestFocus();
        } 
});

    abc.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseEntered(MouseEvent e) {
           JPanel p = (JPanel)e.getSource();
            p.requestFocus();
        }

});
    frame.getContentPane().add(abc,"Center");
    frame.getContentPane().add(new JButton("Click Me"),"North");
    abc.requestFocusInWindow();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}
于 2013-06-02T18:07:34.690 に答える