gpt4 book ai didi

java - Dr java keylistener 帮助对象在按键释放后移动

转载 作者:行者123 更新时间:2023-12-02 03:58:15 33 4
gpt4 key购买 nike

我正在尝试创建一个程序,其中一个对象会不断地从屏幕上掉下来,除非用户按下空格键,在这种情况下它会向上移动。到目前为止,当我运行程序时,我已经让物体开始下落。当我按下空格键时,对象会向上移动(就像它应该的那样)。但是,当我松开按键时,物体停止移动。我希望松开 key 时它继续下降。

我考虑过使用按键绑定(bind)而不是按键监听器,但我遇到了麻烦。我是一名高中生,希望主修计算机科学,所以请原谅我不了解太多高级编码术语/方法。然而,我是类最好的学生之一,也是一个渴望解决这个问题的快速学习者。这是我的代码:

public class htestnew extends JPanel implements ActionListener, KeyListener {
Timer t = new Timer(5, this);
int x = 20, y = 20, vely = 1;

public htestnew() {
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

ImageIcon background = new ImageIcon("OBJECTEXAMPLE1");
background.paintIcon(this, g, 0, 0);
}

public void actionPerformed(ActionEvent e) {
if (y < 0)
{
vely = 0;
y = 0;
}

if (y > 305) //-70
{
vely = 0;
y = 305;
}

y += vely;
repaint();
}

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

if (code == KeyEvent.VK_SPACE) {
vely = -1;
}
}

public void keyTyped(KeyEvent e) {
}

public void keyReleased(KeyEvent e) {
vely = 0;
//I have tried setting this value to 1 but it does not work
}

如果使用键绑定(bind)更容易,那么个人且易于遵循的示例将是令人惊奇的。谢谢。

最佳答案

所以,改变...

public void keyReleased(KeyEvent e) {
vely=0;
//I have tried setting this value to 1 but it does not work
}

到...

public void keyReleased(KeyEvent e) {
vely=1;
//I have tried setting this value to 1 but it does not work
}

基本上让它工作了(在我添加了一些实际绘制的东西之后)

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test extends JPanel implements ActionListener, KeyListener {

public static void main(String[] args) {
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 Test());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

Timer t = new Timer(5, this);
int x = 20, y = 20, vely = 1;

public Test() {
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(x, y, 5, 5);
}

public void actionPerformed(ActionEvent e) {

if (y < 0) {
vely = 0;
y = 0;
}

if (y > 305) //-70
{
vely = 0;
y = 305;
}

y += vely;
repaint();
}

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

if (code == KeyEvent.VK_SPACE) {
vely = -1;
}

}

public void keyTyped(KeyEvent e) {
}

public void keyReleased(KeyEvent e) {
vely = 1;
//I have tried setting this value to 1 but it does not work
}
}

我强烈建议您查看How to Use Key Bindings而不是使用KeyListener,它将用KeyListener解决核心问题

按键绑定(bind)示例...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
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.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test extends JPanel implements ActionListener {

public static void main(String[] args) {
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 Test());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

Timer t = new Timer(5, this);
int x = 20, y = 20, vely = 1;

public Test() {
t.start();

addKeyBinding("space.pressed", KeyEvent.VK_SPACE, true, new ChangeYAction(-1));
addKeyBinding("space.released", KeyEvent.VK_SPACE, false, new ChangeYAction(1));
}

protected void addKeyBinding(String name, int virtualKey, boolean pressed, Action action) {
addKeyBinding(name, KeyStroke.getKeyStroke(virtualKey, 0, !pressed), action);
}

protected void addKeyBinding(String name, KeyStroke keyStroke, Action action) {
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();

im.put(keyStroke, name);
am.put(name, action);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(x, y, 5, 5);
}

public void actionPerformed(ActionEvent e) {

if (y < 0) {
vely = 0;
y = 0;
}

if (y > 305) //-70
{
vely = 0;
y = 305;
}

y += vely;
repaint();
}

public class ChangeYAction extends AbstractAction {

private int changeYTo;

public ChangeYAction(int changeYTo) {
this.changeYTo = changeYTo;
}

@Override
public void actionPerformed(ActionEvent e) {
vely = changeYTo;
}

}

}

关于java - Dr java keylistener 帮助对象在按键释放后移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35215689/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com