1

I honestly have no idea what I'm doing wrong.

Sometimes, the key events register and sometimes they don't.
And by that I mean sometimes when I run Frogger2 I get registering events, and sometimes nothing.

It seems completely random, when the key events register or not. Usually if I don't test anything for a while and run Frogger2 the events register and the moment I close it and rerun the exact same program, I get no events.

Please help.

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回答的问题(非常感兴趣的答案)。 对 Key Bindings 感兴趣的是: 说当你按下++程序做一些操作时,非常有趣并且强烈推荐使用 Key Bindings,也可以看到我在堆栈上的第一个问题是关于这个主题的。 这是您的代码,我做了一些更改:


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 回答