不确定如何将计时器放入我的 keylistener 代码中
Not sure how to put timer into my keylistener code
基本上,我有一个按键侦听器代码(我知道按键绑定,谢谢,但我的项目不需要它),它会根据您按下的内容重新调整 window 框架的大小.但是,当您按住 w 时,它会像这样 w [暂停] wwwwwwww 并且每当您重新调整 window 的大小时它都会很明显。有人可以帮助我成功地将计时器放入我的代码中吗?我已经排除了导入语句,但它们都在那里。
public class KeyFrame extends JFrame implements KeyListener
{
public boolean t = true;
private final HashSet<Integer> pressed = new HashSet<Integer>();
private Timer timer;
public KeyFrame(String name) {
super(name);
}
public void keyTyped(KeyEvent e) {
return;
}
public void keyPressed(KeyEvent e) {
pressed.add(e.getKeyCode());
new Timer(10, new ActionListener(){public void actionPerformed(ActionEvent arg0){}}).start();
if (pressed.size() >= 1) {
for(int code : pressed) {
if(code == KeyEvent.VK_D){
this.setSize(getWidth()+5, getHeight());}
if(code == KeyEvent.VK_A)
this.setSize(getWidth()-5, getHeight());
if(code == KeyEvent.VK_S)
this.setSize(getWidth(), getHeight()+5);
if(code == KeyEvent.VK_W)
this.setSize(getWidth(), getHeight()-5);
}
}
}
public void keyReleased(KeyEvent e) {
pressed.remove(e.getKeyCode());
}
}
However, when you hold down w, it goes like this w [pause] wwwwwwww
这是大多数OS
下的标准操作
不要在 KeyPressed
中启动 Timer
,让 Timer
运行 独立于 KeyListener
您可能还想将代码检查放入 ActionListener
而不是 keyPressed
方法
public class KeyFrame extends JFrame implements KeyListener
{
public boolean t = true;
private final HashSet<Integer> pressed = new HashSet<Integer>();
private Timer timer;
public KeyFrame(String name) {
super(name);
new Timer(10, new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(pressed.contains(KeyEvent.VK_D)) {
setSize(getWidth()+5, getHeight());
} else if(pressed.contains(KeyEvent.VK_A)) {
setSize(getWidth()-5, getHeight());
} else if(pressed.contains(KeyEvent.VK_S)) {
setSize(getWidth(), getHeight()+5);
} else if(pressed.contains(KeyEvent.VK_W)) {
setSize(getWidth(), getHeight()-5);
}
}
}).start();
}
public void keyTyped(KeyEvent e) {
return;
}
public void keyPressed(KeyEvent e) {
pressed.add(e.getKeyCode());
}
public void keyReleased(KeyEvent e) {
pressed.remove(e.getKeyCode());
}
}
您还会发现键绑定将解决与 KeyListener
相关的焦点相关问题,这就是我们通常推荐它的原因
I am aware of keybindings thank you but I don't need that for my project
那么你应该使用它们,真的没有任何借口不使用
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
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 Delta xDelta;
private Delta yDelta;
public TestPane() {
xDelta = new Delta();
yDelta = new Delta();
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.up.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), new DeltaAction(yDelta, -5));
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.up.released", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), new DeltaAction(yDelta));
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.down.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), new DeltaAction(yDelta, 5));
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.down.released", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), new DeltaAction(yDelta));
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.left.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), new DeltaAction(xDelta, -5));
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.left.released", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true), new DeltaAction(xDelta));
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.right.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), new DeltaAction(xDelta, 5));
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.right.released", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), new DeltaAction(xDelta));
Timer timer = new Timer(250, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Window window = SwingUtilities.getWindowAncestor(TestPane.this);
Dimension size = window.getSize();
size.width += xDelta.getValue();
size.height += yDelta.getValue();
window.setSize(size);
}
});
timer.start();
}
public void bindKeyStrokeTo(int condition, String name, KeyStroke keyStroke, Action action) {
InputMap im = getInputMap(condition);
ActionMap am = getActionMap();
im.put(keyStroke, name);
am.put(name, action);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public class Delta {
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
public class DeltaAction extends AbstractAction {
private Delta delta;
private int value;
public DeltaAction(Delta delta, int value) {
this.delta = delta;
this.value = value;
}
public DeltaAction(Delta delta) {
this(delta, 0);
}
@Override
public void actionPerformed(ActionEvent e) {
delta.setValue(value);
}
}
}
基本上,我有一个按键侦听器代码(我知道按键绑定,谢谢,但我的项目不需要它),它会根据您按下的内容重新调整 window 框架的大小.但是,当您按住 w 时,它会像这样 w [暂停] wwwwwwww 并且每当您重新调整 window 的大小时它都会很明显。有人可以帮助我成功地将计时器放入我的代码中吗?我已经排除了导入语句,但它们都在那里。
public class KeyFrame extends JFrame implements KeyListener
{
public boolean t = true;
private final HashSet<Integer> pressed = new HashSet<Integer>();
private Timer timer;
public KeyFrame(String name) {
super(name);
}
public void keyTyped(KeyEvent e) {
return;
}
public void keyPressed(KeyEvent e) {
pressed.add(e.getKeyCode());
new Timer(10, new ActionListener(){public void actionPerformed(ActionEvent arg0){}}).start();
if (pressed.size() >= 1) {
for(int code : pressed) {
if(code == KeyEvent.VK_D){
this.setSize(getWidth()+5, getHeight());}
if(code == KeyEvent.VK_A)
this.setSize(getWidth()-5, getHeight());
if(code == KeyEvent.VK_S)
this.setSize(getWidth(), getHeight()+5);
if(code == KeyEvent.VK_W)
this.setSize(getWidth(), getHeight()-5);
}
}
}
public void keyReleased(KeyEvent e) {
pressed.remove(e.getKeyCode());
}
}
However, when you hold down w, it goes like this w [pause] wwwwwwww
这是大多数OS
下的标准操作不要在 KeyPressed
中启动 Timer
,让 Timer
运行 独立于 KeyListener
您可能还想将代码检查放入 ActionListener
而不是 keyPressed
方法
public class KeyFrame extends JFrame implements KeyListener
{
public boolean t = true;
private final HashSet<Integer> pressed = new HashSet<Integer>();
private Timer timer;
public KeyFrame(String name) {
super(name);
new Timer(10, new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(pressed.contains(KeyEvent.VK_D)) {
setSize(getWidth()+5, getHeight());
} else if(pressed.contains(KeyEvent.VK_A)) {
setSize(getWidth()-5, getHeight());
} else if(pressed.contains(KeyEvent.VK_S)) {
setSize(getWidth(), getHeight()+5);
} else if(pressed.contains(KeyEvent.VK_W)) {
setSize(getWidth(), getHeight()-5);
}
}
}).start();
}
public void keyTyped(KeyEvent e) {
return;
}
public void keyPressed(KeyEvent e) {
pressed.add(e.getKeyCode());
}
public void keyReleased(KeyEvent e) {
pressed.remove(e.getKeyCode());
}
}
您还会发现键绑定将解决与 KeyListener
相关的焦点相关问题,这就是我们通常推荐它的原因
I am aware of keybindings thank you but I don't need that for my project
那么你应该使用它们,真的没有任何借口不使用
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
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 Delta xDelta;
private Delta yDelta;
public TestPane() {
xDelta = new Delta();
yDelta = new Delta();
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.up.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), new DeltaAction(yDelta, -5));
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.up.released", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), new DeltaAction(yDelta));
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.down.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), new DeltaAction(yDelta, 5));
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.down.released", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), new DeltaAction(yDelta));
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.left.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), new DeltaAction(xDelta, -5));
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.left.released", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true), new DeltaAction(xDelta));
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.right.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), new DeltaAction(xDelta, 5));
bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "delta.right.released", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), new DeltaAction(xDelta));
Timer timer = new Timer(250, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Window window = SwingUtilities.getWindowAncestor(TestPane.this);
Dimension size = window.getSize();
size.width += xDelta.getValue();
size.height += yDelta.getValue();
window.setSize(size);
}
});
timer.start();
}
public void bindKeyStrokeTo(int condition, String name, KeyStroke keyStroke, Action action) {
InputMap im = getInputMap(condition);
ActionMap am = getActionMap();
im.put(keyStroke, name);
am.put(name, action);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public class Delta {
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
public class DeltaAction extends AbstractAction {
private Delta delta;
private int value;
public DeltaAction(Delta delta, int value) {
this.delta = delta;
this.value = value;
}
public DeltaAction(Delta delta) {
this(delta, 0);
}
@Override
public void actionPerformed(ActionEvent e) {
delta.setValue(value);
}
}
}