gpt4 book ai didi

Java - keyPressed 事件未被调用

转载 作者:行者123 更新时间:2023-12-01 10:57:47 25 4
gpt4 key购买 nike

我正在用java为自己制作一个俄罗斯方 block 克隆,作为一个学习项目。然而,我现在陷入了获取左右移动棋子的输入部分。我不确定问题是否出在我的方法中,或者问题出在未调用的 keyPressed 方法中,我在调试中没有成功,因为它忽略了该方法。这是我的代码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.*;

public class MainClass implements KeyListener{
public static Painter painter = new Painter();
public static LShape tetr = new LShape(150,0);

public static void main(String[] args) throws InterruptedException {
JFrame window = new JFrame("Tetris");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300, 560);
window.setBackground(Color.BLUE);

window.getContentPane().add(painter);
window.setVisible(true);

ArrayList<Block> blocks = new ArrayList<Block>();
ArrayList<Block> staticBlocks = new ArrayList<Block>();

while(true){
tetr = new LShape(150, 0);
for (int i = 0; i < tetr.iterations; i++) {
blocks = new ArrayList<Block>(Arrays.asList(tetr.getBlocks()));
blocks.addAll(staticBlocks);
painter.setBlocks(blocks);
tetr.changeY(5);
Thread.sleep(35);
painter.repaint();
}
staticBlocks.addAll(blocks);
}
}

public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

public void keyPressed(KeyEvent e) { //I am not sure if this is even called
switch(e.getKeyCode()){
case KeyEvent.VK_RIGHT:
tetr.changeX(20);
break;
case KeyEvent.VK_LEFT:
tetr.changeX(-20);
}
}

public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}
}

最佳答案

MainClass 未在任何地方使用。

创建一个实例并向窗口对象(JFrame 实例)添加一个键监听器,该监听器位于代码中的 MainClass 中。使用这个window.addKeyListener(new MainClass());

public class MainClass implements KeyListener {

public static Painter painter = new Painter();
public static LShape tetr = new LShape(150, 0);

public static void main(String[] args) throws InterruptedException {
JFrame window = new JFrame("Tetris");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300, 560);
window.setBackground(Color.BLUE);
window.addKeyListener(new MainClass());
// window.getContentPane().add((PopupMenu) painter);
window.setVisible(true);

ArrayList<Block> blocks = new ArrayList<Block>();
ArrayList<Block> staticBlocks = new ArrayList<Block>();

while (true) {
tetr = new LShape(150, 0);
for (int i = 0; i < tetr.iterations; i++) {
blocks = new ArrayList<Block>(Arrays.asList(tetr.getBlocks()));
blocks.addAll(staticBlocks);
painter.setBlocks(blocks);
tetr.changeY(5);
Thread.sleep(35);
painter.repaint();
}
staticBlocks.addAll(blocks);
}
}

public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

public void keyPressed(KeyEvent e) { //I am not sure if this is even called
System.out.println(e.getKeyCode());
switch (e.getKeyCode()) {
case KeyEvent.VK_RIGHT:
// tetr.changeX(20);
break;
case KeyEvent.VK_LEFT:
// tetr.changeX(-20);
}
}

public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}
}

关于Java - keyPressed 事件未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33525090/

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