请帮帮我们。我无法运行KeyListener。但我已经添加了你需要的一切(我想)。我真的不知道这段代码有什么问题。我添加了一个Key Listener,实现了一个关键的监听器并包含了所有类,所以我忘记了什么? :/
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
/******Key classes are included*********/
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;
/********Key listener is Implemented********/
public class Game extends JPanel implements KeyListener,Runnable
{
JPanel m_mainPanel;
Thread moveBall = new Thread();
Timer t = new Timer(5, this);
int radius = 50;
int posx = 400;
int posy = 400;
int RectPosX = 10;
int RectPosY = 400;
int velocityX = 0;
int velocityY = 0;
//Konstruktor
public Game(JPanel mainPanel) {
m_mainPanel = mainPanel;
setLayout(new GridLayout(3, 1));
JPanel north_panel = new JPanel();
north_panel.setLayout(new FlowLayout(FlowLayout.LEFT));
/**********I didn't forget this********/
this.addKeyListener(this);
/*******Focus is set**********/
setFocusable(true);
setFocusTraversalKeysEnabled(false);
add(north_panel);
}
//Painting rectangle
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(RectPosX, RectPosY, radius, radius);
}
//the key event wich isn't called
@Override
public void keyPressed(KeyEvent e)
{
System.out.println("Key pressed");
if(e.getKeyCode() == KeyEvent.VK_UP)
{
velocityY = 10;
}
if(e.getKeyCode() == KeyEvent.VK_DOWN)
{
velocityY = -10;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void run()
{
while(true)
{
repaint();
}
}
}
答案 0 :(得分:-1)
以下是MCVE的一个小例子:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel{
public Main(){
addKeyListener(new KeyListener(){
@Override
public void keyTyped(KeyEvent e) {
System.out.println("Typed: " + e.getKeyChar());
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("Pressed: " + e.getKeyChar());
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("Released: " + e.getKeyChar());
}
});
}
public static void main(String[] args){
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Main());
frame.setSize(300, 300);
frame.setVisible(true);
}
}
这个程序表现出同样的问题:从不调用关键监听器!这是因为焦点位于JFrame上,而不是JPanel。
要更改此设置,您必须使JPanel 可聚焦。您只需调用setFocusable()方法即可完成此操作。现在你的JPanel将成为焦点:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel{
public Main(){
addKeyListener(new KeyListener(){
@Override
public void keyTyped(KeyEvent e) {
System.out.println("Typed: " + e.getKeyChar());
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("Pressed: " + e.getKeyChar());
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("Released: " + e.getKeyChar());
}
});
setFocusable(true);
}
public static void main(String[] args){
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Main());
frame.setSize(300, 300);
frame.setVisible(true);
}
}
更复杂的程序可能需要requestFocusInWindow()函数。或者您可能希望改为使用KeyBindings。但是你的主要问题是由焦点问题引起的。