【问题标题】:KeyPress won't work [duplicate]按键不起作用[重复]
【发布时间】:2014-01-18 14:12:51
【问题描述】:

由于某种原因,我的程序没有检测到我何时按下一个键,即使它应该没问题。

这是我的代码:

import javax.swing.*;

public class Frame {
    public static void main(String args[]) {

    Second s = new Second();
    JFrame f = new JFrame();
    f.add(s);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setTitle("Bouncing Ball");
    f.setSize(600, 400);
}

}  

这是第二课:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

 import javax.swing.*;

public class Second extends JPanel implements ActionListener, KeyListener {
Timer t = new Timer(5, this);
double x = 0, y = 0, velX =0 , velY = 0;

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    Rectangle2D circle = new Rectangle2D.Double(x, y, 40, 40);
    g2.fill(circle);
    t.start();
}

public void actionPerformed(ActionEvent e) {

    x += velX;
    y += velY;

    repaint();
}

public void up() {
    velY = -1.5;
    velX = 0;
}

public void down() {
    velY = 1.5;
    velX = 0;
}

public void keyPressed(KeyEvent e) {
    int KeyCode = e.getKeyCode();

    if (KeyCode == KeyEvent.VK_Z) {
        up();
    }

    if (KeyCode == KeyEvent.VK_S) {
        down();
    }
}

public void keyTyped(KeyEvent e){

}

public void keyReleased(KeyEvent e){

}

}

我该如何解决这个问题?

【问题讨论】:

  • 您是否尝试过在 keyPressed 函数中调试和设置断点?
  • 对于 Swing,通常在基于 AWT 的较低级别 KeyListener 上使用键绑定。有关如何使用它们的详细信息,请参阅How to Use Key Bindings

标签: java swing awt keylistener keyevent


【解决方案1】:
  • 计时器似乎不起作用的原因是,velXvelY 等于 0,因此它不会增加任何内容。如果你给它们一个值,它就会动画。

  • 钥匙不起作用的原因是

    1. 因为您还没有将KeyListener 注册到面板。
    2. 您需要setfocusable(true)
    3. 您需要在up() down() 方法中调用repaint() keyPressed() 方法中。
    4. 您需要在up()down() 方法中增加/减少y 的值。

添加以下构造函数,将repaint() 添加到keyPressed() 并正确递增/递减,它可以正常工作

public Second(){
    setFocusable(true);
    addKeyListener(this);
}

添加上面的构造函数。以及 keyPressed 中的重绘

public void keyPressed(KeyEvent e) {
    int KeyCode = e.getKeyCode();

    if (KeyCode == KeyEvent.VK_Z) {
        up();
        repaint();
    }

    if (KeyCode == KeyEvent.VK_S) {
        down();
        repaint();
    }
}

递增/递减

public void up() {
    y -= 10;
}

public void down() {
    y += 10;
}

虽然这可能有效,但建议使用键绑定。

How to use Key Bindings | The complete Creating a GUI with Swing trail

【讨论】:

    【解决方案2】:

    出于测试目的,向 JFrame 添加一个侦听器并查看您是否在其中得到任何响应。如果你这样做了,那么这意味着 JFrame 没有将事件传递给 Second,可能是因为 Second 没有焦点。

    您也可以尝试拨打requestFocusInWindow()http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html

    当用户点击一个组件,或者当用户在组件之间切换,或者以其他方式与组件交互时,组件通常会获得焦点。组件也可以通过编程方式获得焦点,例如当其包含的框架或对话框可见时。这段代码 sn-p 展示了如何在每次窗口获得焦点时赋予特定组件焦点:

    //Make textField get the focus whenever frame is activated.
    > f.addWindowFocusListener(new WindowAdapter() {
    >     public void windowGainedFocus(WindowEvent e) {
    >         s.requestFocusInWindow();
    >     } });
    

    我还建议使用比sf 更具描述性的变量,以及比Second 更具描述性的类名。

    【讨论】:

      猜你喜欢
      • 2016-01-04
      • 2022-06-15
      • 2013-10-10
      • 2020-08-02
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多