Java swing计时器仅工作一次,然后keyEvents快速连续触发 - 按住键 [英] Java swing timer only works once then keyEvents fire in rapid succession - holding key down
问题描述
所以我把它设置为 KeyEvents
和计时器的测试。第一次按下右箭头键时,事件将等待5秒,就像定时器设置为,然后打印 KeyPressed
。但是,在第一个 println
之后, KeyPressed
将快速连续打印,就像 KeyEvents
当我按住键时它正在收集。我不希望所有按住右箭头键的额外按键导致。我想按住右箭头键,每5秒钟只收到 println
。任何帮助是极大的赞赏。
So I've set this up as a test for KeyEvents
and timers. The first time the right arrow key is pressed the event will wait 5 seconds like the timer is setup to, then print KeyPressed
. However, after the first println
, KeyPressed
will be printed in rapid succession like a long queue of KeyEvents
it was collecting up while I held the key down.I don't want all the extra key presses that holding the right arrow key causes. I want to hold the right arrow key down and only receive a println
every 5 seconds. Any help is greatly appreciated.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class GameBoard extends JPanel
{
public Ninja ninja;
public GameBoard()
{
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
ninja = new Ninja();
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(ninja.getImage(), 20,20,null);
}
private class TAdapter extends KeyAdapter
{
private Timer timer;
@Override
public void keyPressed(KeyEvent e)
{
timer = new Timer(5000, new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
System.out.println("KeyPressed");
}
});
timer.start();
}
@Override
public void keyReleased(KeyEvent e)
{
ninja.keyReleased(e);
repaint();
}
}
}
推荐答案
当按下该键时,操作系统将为中风生成一个重复事件。
When the key is held down, the OS will generate a repeating event for the stroke.
通常,你需要某种旗帜这表示 keyPressed
事件已被处理过。
Normally, you would need some kind of flag that would indicate that the keyPressed
event has already been handled or not.
根据您的示例,您可以使用计时器
。例如,当触发 keyPressed
时,您将检查计时器
是否为空或正在运行...
Based on your example, you could use the Timer
. For example, when keyPressed
is triggered, you would check to see of the Timer
is null or is running...
if (timer == null || !timer.isRunning()) {...
现在,在您的 keyReleased
事件中,您可能需要停止计时器,因此,下次 keyPressed
被触发时,您可以重新启动计时器。
Now, in your keyReleased
event, you could need to stop the timer, so that the next time keyPressed
is triggered, you can restart the timer.
这假设您只需要只有在按下键时才能运行计时器。
This assumes that you only want the timer to run only while the key is pressed.
作为一般建议,你应该使用键绑定而不是 KeyListener
因为它可以让你更好地控制焦点触发关键事件的级别
As a general suggestion, you should be using Key Bindings instead of KeyListener
as it will provide you better control over the focus level which triggers the key events
使用键绑定更新示例
这是基于您的代码似乎正在做什么......
This is based on what your code appears to be doing...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
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.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class WalkCycle {
public static void main(String[] args) {
new WalkCycle();
}
public WalkCycle() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private List<BufferedImage> walkCycle;
private int frame;
private Timer timer;
public TestPane() {
setBackground(Color.WHITE);
walkCycle = new ArrayList<>(10);
try {
walkCycle.add(ImageIO.read(getClass().getResource("/Walk01.png")));
walkCycle.add(ImageIO.read(getClass().getResource("/Walk02.png")));
walkCycle.add(ImageIO.read(getClass().getResource("/Walk03.png")));
walkCycle.add(ImageIO.read(getClass().getResource("/Walk04.png")));
walkCycle.add(ImageIO.read(getClass().getResource("/Walk05.png")));
walkCycle.add(ImageIO.read(getClass().getResource("/Walk06.png")));
walkCycle.add(ImageIO.read(getClass().getResource("/Walk07.png")));
walkCycle.add(ImageIO.read(getClass().getResource("/Walk08.png")));
walkCycle.add(ImageIO.read(getClass().getResource("/Walk09.png")));
walkCycle.add(ImageIO.read(getClass().getResource("/Walk10.png")));
Timer timer = new Timer(80, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame++;
if (frame >= walkCycle.size()) {
frame = 0;
}
System.out.println(frame);
repaint();
}
});
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "right-down");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), "right-up");
am.put("right-down", new TimerAction(timer, true));
am.put("right-up", new TimerAction(timer, false));
} catch (IOException exp) {
exp.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics g2d = (Graphics2D) g.create();
BufferedImage img = walkCycle.get(frame);
int x = (getWidth() - img.getWidth()) / 2;
int y = (getHeight() - img.getHeight()) / 2;
g2d.drawImage(img, x, y, this);
g2d.dispose();
}
}
public class TimerAction extends AbstractAction {
private Timer timer;
private boolean start;
public TimerAction(Timer timer, boolean start) {
this.timer = timer;
this.start = start;
}
@Override
public void actionPerformed(ActionEvent e) {
if (start && !timer.isRunning()) {
System.out.println("Start");
timer.start();
} else if (!start && timer.isRunning()) {
System.out.println("stop");
timer.stop();
}
}
}
}
就个人而言,我会有一个计时器
,它总是在滴答作响,这更新了视图。然后视图将与模型一起检查应该更新和呈现的内容以及键绑定将更新模型的状态,但这只是我。
Personally, I would have a single Timer
which was always ticking, which updated the view. The view would then check with the model about what should be updated and rendered and the key bindings would update the state of the model, but that's just me.
这篇关于Java swing计时器仅工作一次,然后keyEvents快速连续触发 - 按住键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!