Dr java keylistener 帮助对象在释放键后移动
Dr java keylistener help object move after key is released
我正在尝试创建一个程序,在该程序中,除非用户按下 space,否则对象会不断从屏幕上掉落,在这种情况下,它会向上移动。到目前为止,当我 运行 程序时,我已经得到了最初开始下降的对象。当我按下 space 时,对象向上移动(就像它应该的那样)。但是,当我松开按键时,物体停止移动。我希望它在释放键时继续下降。
我考虑过使用键绑定而不是键侦听器,但我遇到了麻烦。我是一名想主修计算机科学的高中生,所以请原谅我对高级编码了解不多terms/methods。然而,我是我 class 中最好的人之一,也是一个渴望解决这个问题的快速学习者。这是我的代码:
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
}
如果使用键绑定更容易,那么个人化且易于遵循的示例将是惊人的。谢谢。
所以,改变...
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
解决核心问题
键绑定示例...
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;
}
}
}
我正在尝试创建一个程序,在该程序中,除非用户按下 space,否则对象会不断从屏幕上掉落,在这种情况下,它会向上移动。到目前为止,当我 运行 程序时,我已经得到了最初开始下降的对象。当我按下 space 时,对象向上移动(就像它应该的那样)。但是,当我松开按键时,物体停止移动。我希望它在释放键时继续下降。
我考虑过使用键绑定而不是键侦听器,但我遇到了麻烦。我是一名想主修计算机科学的高中生,所以请原谅我对高级编码了解不多terms/methods。然而,我是我 class 中最好的人之一,也是一个渴望解决这个问题的快速学习者。这是我的代码:
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
}
如果使用键绑定更容易,那么个人化且易于遵循的示例将是惊人的。谢谢。
所以,改变...
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
键绑定示例...
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;
}
}
}