- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在创建一个正方形的动画时遇到问题,当按下其中一个箭头键时,该动画将以一定的速度移动。我已经实现了 keyPressed 和 actionPerformed 方法,并且还在构造函数末尾启动了计时器的滴答声。但是方 block 仍然不会移动。我做错了什么?
下面是 JPanel。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class A3JPanel extends JPanel implements KeyListener, ActionListener{
public static final int JFRAME_AREA_WIDTH = A3Constants.JFRAME_AREA_WIDTH;
public static final int JFRAME_AREA_HEIGHT = A3Constants.JFRAME_AREA_HEIGHT;;
public static final Rectangle HOME_AREA = A3Constants.HOME_AREA;
public static final Rectangle LEO_LEFT_AREA = A3Constants.LEO_LEFT_AREA;
public static final Rectangle ALL_WALLS_AREA = A3Constants.ALL_WALLS_AREA;
public static final Rectangle EXITING_SLIDES_AREA = A3Constants.EXITING_SLIDES_AREA;
public static final Color NICE_GRAY_COLOUR = A3Constants.NICE_GRAY_COLOUR;
public static final Color GAME_SCREEN_COLOUR = A3Constants.GAME_SCREEN_COLOUR;
public static final Color SLIDES_AREA_COLOUR = A3Constants.SLIDES_AREA_COLOUR;
public static final Color LEO_AREA_COLOUR = A3Constants.LEO_AREA_COLOUR;
public static final Color HOME_AREA_COLOUR = A3Constants.HOME_AREA_COLOUR;
public static final Color WALL_COLOUR = A3Constants.WALL_COLOUR;
public static final int MAX_WALLS = A3Constants.MAX_WALLS;
public static final Font TINY_FONT = A3Constants.TINY_FONT;
public static final Font LARGE_FONT = A3Constants.LARGE_FONT;
public static final Font HUGE_FONT = A3Constants.HUGE_FONT;
public static final int LARGE_FONT_SIZE = A3Constants.LARGE_FONT_SIZE;
public static final int HUGE_FONT_SIZE = A3Constants.HUGE_FONT_SIZE;
public static final Point TICKS_POSITION = A3Constants.TICKS_POSITION;
public static final Point WINNER_LOSER_INFO_POSITION = A3Constants.WINNER_LOSER_INFO_POSITION;
public static final Point INFORMATION_POSITION1 = A3Constants.INFORMATION_POSITION1;
public static final Point INFORMATION_POSITION2 = A3Constants.INFORMATION_POSITION2;
public static final Point INFORMATION_POSITION3 = A3Constants.INFORMATION_POSITION3;
public static final int TICKS_ALLOWED = A3Constants.TICKS_ALLOWED;
public static final int UP = A3Constants.UP;
public static final int DOWN = A3Constants.DOWN;
public static final int LEFT = A3Constants.LEFT;
public static final int RIGHT = A3Constants.RIGHT;
private CoolCat coolCat;
private Timer t;
public A3JPanel() {
setBackground(Color.GREEN);
coolCat = new CoolCat(A3Constants.LEO_START_AREA.x, A3Constants.LEO_START_AREA.y);
addKeyListener(this);
t = new Timer(30,this);
t.start();
//t.addActionListener(this);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
drawGameArea(g);
coolCat.draw(g);
}
private void drawGameArea(Graphics g){
g.setColor(A3Constants.LEO_AREA_COLOUR);
g.fillRect(A3Constants.LEO_LEFT_AREA.x, A3Constants.LEO_LEFT_AREA.y, A3Constants.LEO_LEFT_AREA.width, A3Constants.LEO_LEFT_AREA.height);
g.setColor(A3Constants.WALL_COLOUR);
g.fillRect(A3Constants.ALL_WALLS_AREA.x, A3Constants.ALL_WALLS_AREA.y, A3Constants.ALL_WALLS_AREA.width, A3Constants.ALL_WALLS_AREA.height);
g.setColor(A3Constants.HOME_AREA_COLOUR);
g.fillRect(A3Constants.HOME_AREA.x, A3Constants.HOME_AREA.y, A3Constants.HOME_AREA.width, A3Constants.HOME_AREA.height);
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_UP){
coolCat.setDirection(A3Constants.UP);
}
if(e.getKeyCode() == KeyEvent.VK_DOWN){
coolCat.setDirection(A3Constants.DOWN);
}
if(e.getKeyCode() == KeyEvent.VK_LEFT){
coolCat.setDirection(A3Constants.LEFT);
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
coolCat.setDirection(A3Constants.RIGHT);
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void actionPerformed(ActionEvent e){
coolCat.move();
repaint();
}
}
下面是另一个类。
import java.awt.*;
public class CoolCat {
public static final Rectangle LEO_START_AREA = A3Constants.LEO_START_AREA;
public static final int GAME_SCREEN_AREA_TOP = A3Constants.GAME_SCREEN_AREA_TOP;
public static final int GAME_SCREEN_AREA_BOTTOM = A3Constants.GAME_SCREEN_AREA_BOTTOM;
public static final int GAME_SCREEN_AREA_LEFT = A3Constants.GAME_SCREEN_AREA_LEFT;
public static final int GAME_SCREEN_AREA_RIGHT = A3Constants.GAME_SCREEN_AREA_RIGHT;
public static final int UP = A3Constants.UP;
public static final int DOWN = A3Constants.DOWN;
public static final int LEFT = A3Constants.LEFT;
public static final int RIGHT = A3Constants.RIGHT;
private Rectangle area;
private int speed;
private int direction;
public CoolCat(int x, int y){
this.speed = speed;
speed = (int)(Math.random() * (8 - 4 + 1)) + 4;
area = new Rectangle(A3Constants.LEO_START_AREA.x, A3Constants.LEO_START_AREA.y);
direction = RIGHT;
}
public Rectangle getArea(){
area = new Rectangle(LEO_START_AREA.x, LEO_START_AREA.y, LEO_START_AREA.width, LEO_START_AREA.height);
return area;
}
public void setDirection(int direction){
this.direction = direction;
}
//public boolean hasReachedHome(Rectangle zhomeArea){}
public void move(){
if(direction == A3Constants.UP){
area.y -= speed;
} else if(direction == A3Constants.DOWN){
area.y += speed;
} else if(direction == A3Constants.LEFT){
area.x -= speed;
} else if(direction == A3Constants.RIGHT){
area.x += speed;
}
}
public void draw(Graphics g){
g.setColor(Color.RED);
g.fillOval(A3Constants.LEO_START_AREA.x, A3Constants.LEO_START_AREA.y, A3Constants.LEO_START_AREA.width, A3Constants.LEO_START_AREA.height);
}
}
最佳答案
您正在向 JPanel 添加 KeyListener,该组件默认情况下无法接受焦点,而关键监听器需要焦点才能工作。
一个简单的解决方案是允许您的 JPanel 接受焦点,然后请求将焦点授予它。
更好的解决方案是避免在 Swing 应用程序中使用 KeyListener,而是使用键绑定(bind)(正如之前在类似问题中多次讨论的那样)。例如,请查看:How to make an image move while listening to a keypress in Java.
关于java - Java 中的定时器和按键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19568003/
我对 JavaScript 上的 onkeypress 事件有疑问。 是否可以仅检测 Cntl 键或 Alt 键?此时,如果同时按下 Cntl 和 m,则 onkeypress 事件可以触发单击。是否
我想知道按键中允许使用什么样的字符。它是否也生成一个符号下划线(_)?我总是得到一个带有 - 字母的按键。 最佳答案 按键使用修改过的 Base64 字母表: -0123456789ABCDEFGHI
我有以下格式的(格式错误的DITA)XML: BLARG BLARG Definition BLARG2 BLARG2 Definition BLARG3 BLARG3 Definition
我有两个单独的对象数组,如果特定键值匹配,我需要将它们合并。分析数据后可能更有意义: 数组 1 let categories = [ { id: 5, slug: 'category-5',
由于我是新手,所以我的低效编码给大家带来了巨大的麻烦,对此我提前表示歉意。 我正在尝试在 HTML5 Canvas 上使用 Javascript 制作一个非常基本的游戏,但我似乎找不到一种简单的方法来
我试图找出将多维数组中的对象 id 属性映射到共享相同 id 的另一个数组中的对象值的最佳方法。 举个例子,我有一个像这样的genre_ids数组: 0: {id: 1, name: 'sci-fi'
我有一个游戏,当按下一个键时,您可以通过将子弹添加到 Controller 类来发射子弹。这是代码来自 KeyPressed(); else if (key == KeyEvent.VK_Q && !
我想比较两个字典的长度以及每个字典中每个键、值对的长度。如果在查找时没有匹配项,我还需要能够打印出来。 我当前的代码似乎传递了长度标准,但在尝试匹配元素时失败: assert_that(len(mod
我正在寻找一种跨平台(Win 和 MacOS)方法来检测 C# 中 OpenGL 应用程序的按键。 以下有效,但仅适用于字母数字字符。 protected override void OnKeyPre
我正在 try catch 按键事件(向上和向下翻页),但根本没有收到任何按键事件。这是相关代码:构造函数: private MainLayout() { imageView = new Im
$(el).bind('blur keypress', function(event){ if(event.type == 'keypress' && event.keyCode != 13) r
我有这段代码: while (SDL_PollEvent(&event)) { if (event.type == SDL_KEYDOWN) { switch(event.key.keys
我正在使用下面的代码: $(document).keypress(function (e) { if (e.which === 68 || e.which === 100) {
我正在用 html 和 javascript 制作游戏。只是为了澄清,这不是重复或任何东西。没有什么可以给我我需要的答案。另外,在解释之前,我想说我对按键监听器没有任何问题,我的游戏知道何时按下按键以
我正在尝试用 Javascript 制作游戏,但目前我已经陷入停滞。我正在尝试检测按键并检查它们是否不断按下以移动 Angular 色。这是我正在使用的代码: var THREE; var keys;
我得到了多维数组。我想从每个子数组中删除/取消设置索引为 1 的值。我的数组 $data。 Array ( [3463] => Array ( [0]
我正在设计一个基于网络的会计软件。例如,每当用户按 N 键时,我想打开“新会计凭证”。并在他/她按下 S 键时打开“设置”。 我看到了一些基于 JavaScript 和 jQuery 的脚本。但它们并
阅读此主题后: Keypress events stopped working outside of "input" elements in Meteor after update to 0.5.2
所以,当我按下按钮 1“1 PLAY/STOP”时,按钮变成绿色,当我再次按下它时它会去除颜色。 如果我按下 button2“2 PLAY/STOP”,同样的事情会发生。 如果两个按钮之一播放而我按下
非常直接的问题。 只想按一个键盘键。像输入一样,使用 pywin auto。我不想在任何应用程序窗口的上下文中按下它。 只是键盘键的原始按键,如 a 或 enter 或退格。 最佳答案 只需使用 #
我是一名优秀的程序员,十分优秀!