[英]I am a beginner programmer and I don't know how parameters work in Java. So how do they work?
[英]i need working input for java. scanner doesn't work so neither buffer. on idea what should i do
我从理论上应该可以工作但没有成功的帖子中复制了代码。
我复制的代码:
public class Keyboard {
private static final Map<Integer, Boolean> pressedKeys = new HashMap<>();
static {
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(event -> {
synchronized (Keyboard.class) {
if (event.getID() == KeyEvent.KEY_PRESSED) pressedKeys.put(event.getKeyCode(), true);
else if (event.getID() == KeyEvent.KEY_RELEASED) pressedKeys.put(event.getKeyCode(), false);
return false;
}
});
}
public static boolean isKeyPressed(int keyCode) { // Any key code from the KeyEvent class
return pressedKeys.getOrDefault(keyCode, false);
}
}
它给出了两个错误:
无法在内部类型 jWindowComponent.Keyboard 中定义 static 初始化程序
isKeyPressed方法不能声明static; static 方法只能在 static 或顶级类型中声明
我尝试了所有可以解决的问题,但没有成功。 我想为我正在工作的小项目制作专门的 UI。 我需要一些方法来更改图形渲染器中间渲染的参数,并且需要这样的东西:
if (Keyboard.isKeyPressed(KeyEvent.VK_A)) {
x=0;
y=0;
}
当我按下键时,它会放大或提高分辨率或移动某个方向。 我有一个想法,但我不知道如何让它响应按键。 这是我第一次使用终端以外的任何类型的输入,我真的很想在终端之外进行。 我尝试了我发现的所有东西,我要么是盲人,要么我不知道如何让它发挥作用。 我尝试了这篇文章中的所有内容,但没有任何效果。
无论如何,在我的代码的其他部分可能有一些奇怪的恶作剧:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
public class jWindowComponent extends Canvas {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 1000; // Additional pixels needed for the frame
private static final int HEIGHT = 1000; // Additional pixels needed for the frame
@Override
public void paint(Graphics g) {
super.paint(g);
long percent = 0;
long percheck = 0;
for (int r = 0; r <= 2; r++) {
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
if (Keyboard.isKeyPressed(KeyEvent.VK_A)) {
x=0;
y=0;
}
if (x==y) {
g.setColor(Color.BLACK);
} else {
g.setColor(Color.WHITE);
}
g.fillRect(x, y, 1, 1);
}
//percent = (long) (y * 100 / HEIGHT);
//if (percent == percheck) {
// // block of code to be executed if the condition is true
//} else {
// System.out.print(String.format("\033[%dA", 1));
// System.out.print("\033[2K");
// System.out.print(percent);
// System.out.println("%");
//}
//
//percheck = percent;
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("ColouringPixels - Lesson 9");
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.add(new jWindowComponent());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class Keyboard {
private static final Map<Integer, Boolean> pressedKeys = new HashMap<>();
static {
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(event -> {
synchronized (Keyboard.class) {
if (event.getID() == KeyEvent.KEY_PRESSED) pressedKeys.put(event.getKeyCode(), true);
else if (event.getID() == KeyEvent.KEY_RELEASED) pressedKeys.put(event.getKeyCode(), false);
return false;
}
});
}
public static boolean isKeyPressed(int keyCode) { // Any key code from the KeyEvent class
return pressedKeys.getOrDefault(keyCode, false);
}
}
}
我使用这段代码来学习新的东西(就像我学会了如何使用 jframe 来渲染 window 以及更多在这段代码中不存在的东西)。 我对 java 比较陌生,所以 90% 的时间我都不知道自己在做什么(如您所见)。 可能是该解决方案在互联网上的某个地方,但我没有找到它。
我会感谢任何可以解决这个问题的东西。
使用static
修饰符定义您的Keyboard
class 或将其移动到另一个文件。 另外我假设您使用的是 Java 8 左右,其中不支持内部类中的 static 声明。
您可以使用KeyListener
来检测按键。
这是一个简单的实现:
import java.awt.Canvas;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class JWindowComponent extends Canvas implements KeyListener {
public JWindowComponent() {
// Since this class implements KeyListener, you can just add the KeyListener methods in
// this class
// instead of using an anonymous class
addKeyListener(this);
}
public static void main(String[] args) {
JFrame frame = new JFrame("KeyListener Example");
frame.setSize(500, 500);
frame.setResizable(false);
frame.add(new JWindowComponent());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed: " + e.getKeyChar());
}
// These two methods are required, but they can be empty
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
}
虽然 window 具有焦点,但您应该能够按任意键并触发事件。
希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.