gpt4 book ai didi

java - 键盘不输入任何东西java

转载 作者:行者123 更新时间:2023-11-29 05:24:02 24 4
gpt4 key购买 nike

我一直在尝试这段代码并对其进行编辑,但它不起作用。假设得到一个键盘输入,将其存储在变量“c”中,然后进行比较到键盘上的键。然后假设根据按下的键移动一个小方 block 。键盘似乎无法识别。有人可以帮帮我吗?请注意我 14 岁,所以请不要说它是多么简单和容易,我还在学习,你能不能也用我可能理解的方式来写它。提前谢谢你

'package package1;

import java.awt.Color;
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.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Class1 extends JPanel implements KeyListener, ActionListener{

Timer timer = new Timer(5, this);

//variables
int cox = 0;
int spx = 0; //cox = coordinates x, spx = speedx
int coy = 0;
int spy = 0; //coy = coordinates y, spy = speedy

public void paintComponent(Graphics g)
{
timer.start();

super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(cox, coy, 50, 50);

addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}

public void actionPerformed(ActionEvent e)
{
cox = cox + spx;
coy = coy + spy;
repaint();
}

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

if(c == KeyEvent.VK_LEFT)
{
spx = -1;
spy = 0;
}
if(c == KeyEvent.VK_UP)
{
spx = 0;
spy = -1;
}
if(c == KeyEvent.VK_RIGHT)
{
spx = 1;
spy = 0;
}
if(c == KeyEvent.VK_DOWN)
{
spx = 0;
spy = 1;
}

}

public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
spx = 0;
spy = 0;
}

public static void main(String args[])
{
Class1 t = new Class1();
JFrame frame = new JFrame("window");
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(t);
}
}

最佳答案

你的代码有问题:

  • KeyListener 是非常低级的构造,通常应避免使用 大多数 Swing 应用程序中的键绑定(bind)。这样做还可以更轻松地解决困扰 KeyListener 的焦点问题,这些问题会导致您当前的 KeyListener 什么都不做。如果您在这个站点上阅读了大多数带有 Swing 和 KeyListeners 标签的问题,您会一次又一次地看到这个建议——因为它是真实的,而且有效。
  • 请理解 paintComponent(Graphics g) 方法用于绘制。它经常被调用,通常不受您的控制,因为操作系统可以诱导它被调用。
  • paintComponent 方法对 GUI 的感知响应能力有重大影响,因为它控制绘制 GUI 和 GUI 可能包含的任何动画。如果它因任何原因而变慢,您的 GUI 将看起来很慢。
  • 出于这个原因,此方法应该用于绘图,并且只用于绘图,绘图。具体来说,
    • 不要从此方法中启动您的 Swing 计时器,
    • 不要从此方法中添加 KeyListener(除非您想向 GUI 添加 KeyListener 20 次,这将导致极其不可预测的行为),
    • 不要在此方法中更改任何对象的状态。

例如:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;

@SuppressWarnings("serial")
public class Class1B extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
private static final int ANIMATION_DELAY = 15;
private static final int RECT_WIDTH = 15;
private static final Color RECT_COLOR = Color.red;
private EnumMap<Direction, Boolean> dirMap = new EnumMap<>(Direction.class);
private Map<Integer, Direction> keyToDir = new HashMap<>();
private Timer animationTimer;
public int rectX;
public int rectY;


public Class1B() {
for (Direction dir : Direction.values()) {
dirMap.put(dir, Boolean.FALSE);
}
keyToDir.put(KeyEvent.VK_UP, Direction.UP);
keyToDir.put(KeyEvent.VK_DOWN, Direction.DOWN);
keyToDir.put(KeyEvent.VK_LEFT, Direction.LEFT);
keyToDir.put(KeyEvent.VK_RIGHT, Direction.RIGHT);
setKeyBindings();

animationTimer = new Timer(ANIMATION_DELAY, new AnimationListener());
animationTimer.start();
}

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

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(RECT_COLOR);
g.fillRect(rectX, rectY, RECT_WIDTH, RECT_WIDTH);
}

private void setKeyBindings() {
int condition = WHEN_IN_FOCUSED_WINDOW;
final InputMap inputMap = getInputMap(condition);
final ActionMap actionMap = getActionMap();
boolean[] keyPressed = { true, false };
for (Integer keyCode : keyToDir.keySet()) {
Direction dir = keyToDir.get(keyCode);
for (boolean onKeyPress : keyPressed) {
boolean onKeyRelease = !onKeyPress;
KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, 0,
onKeyRelease);
Object key = keyStroke.toString();
inputMap.put(keyStroke, key);
actionMap.put(key, new KeyBindingsAction(dir, onKeyPress));
}
}
}

private class KeyBindingsAction extends AbstractAction {
private Direction dir;
boolean pressed;

public KeyBindingsAction(Direction dir, boolean pressed) {
this.dir = dir;
this.pressed = pressed;
}

@Override
public void actionPerformed(ActionEvent evt) {
dirMap.put(dir, pressed);
}
}

private class AnimationListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent evt) {
boolean repaint = false;
for (Direction dir : Direction.values()) {
if (dirMap.get(dir)) {
rectX += dir.getIncrX();
rectY += dir.getIncrY();
repaint = true;
}
}
if (repaint) {
repaint();
}
}
}

private static void createAndShowGui() {
Class1B mainPanel = new Class1B();

JFrame frame = new JFrame("Class1B");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

enum Direction {
UP(0, -1), DOWN(0, 1), LEFT(-1, 0), RIGHT(1, 0);
private int incrX;
private int incrY;

private Direction(int incrX, int incrY) {
this.incrX = incrX;
this.incrY = incrY;
}

public int getIncrX() {
return incrX;
}

public int getIncrY() {
return incrY;
}
}

关于java - 键盘不输入任何东西java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23434550/

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