在过去的几个月里,我一直在使用Java.Awt.Frame为键盘输入创建一个屏幕和keyListeners。但是,我的程序变得很复杂,因此我最近决定使用KeyBinds。问题是,我无法弄清楚如何将它们添加到AWT框架。我试图创建一个JPanel并将其添加到框架(使用add()),但是,这似乎没有做任何事情。任何输入将不胜感激;如果需要,请参阅我的代码。
import java.awt.*;
import BreezyGUI.*;
import java.io.* ;
import java.util.Scanner;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.lang.Math;
import java.awt.image.BufferedImage;
import java.awt.Container;
import javax.swing.JComponent;
import javax.swing.Action;
public class fltsm extends GBFrame implements KeyListener
{
private static int fps = 0;
private static int score = 0;
private final int TIMER_DELAY = 100; //milliseconds
private static final int KEY_DOWN = 40; // KEYCODES
private static final int KEY_UP = 38;
private static final int KEY_RIGHT = 39;
private static final int KEY_LEFT = 37;
private static final int KEY_SPACE = 32;
private static final int KEY_0 = 48;
private static final int KEY_1 = 49;
private static final int KEY_2 = 50;
private static final int KEY_3 = 51;
private static final int KEY_4 = 52;
private static final int KEY_5 = 53;
private static final int KEY_6 = 54;
private static final int KEY_ENTER = 10;
private static final int SCREEN_LENGTH = 1000;
private static final int SCREEN_HEIGHT = 600;
private static String keyWord;
private static String commandString = ""; // When the user types in alphabetic keys, they are added to this String and matched to a list if executable commands.
boolean timerStarted = false;
boolean keyListenerAdded = false;
boolean calledByEvent = true;
boolean KeyPressed = false;
boolean drawFPS = false;
int kCode = 0;
KeyEvent event = null; // helps clear out unwanted keyListeners(see object Listener class below)
private static ArrayList<Missile> miss = new ArrayList<Missile>();
private static ArrayList<Missile> Co = new ArrayList<Missile>();
World w1 = new World(SCREEN_LENGTH,SCREEN_HEIGHT);
Plane flyer = new Plane();
Timer t;
public void paint(Graphics g)
{
if (drawFPS == true)
fps++;
if (Plane.getPlanes().indexOf(flyer) == - 1) // Until I find a better solution...
{
Plane.addPlane(flyer);
JPanel panel = new JPanel();
add(panel);
panel.getInputMap().put(KeyStroke.getKeyStroke("1", "doSomething");
panel.getActionMap().put("doSomething", new anAction());
}
addKeyListener (this);
Plane.processPlanes(g);
PlaneMissile.processMissiles(g, w1, calledByEvent);
BotMissile.processMissiles(g, w1, calledByEvent, flyer);
TrackingBot.processBots(g, w1, flyer);
DeathLaser.processLasers(g, flyer);
ScanningLaser.processLasers(g, flyer);
EMP.processEMPs(g, flyer);
Wall.processWalls(g, w1, flyer);
drawStatBox(g, flyer);
}
public static void main (String[ ] args)
{
//normalizeGraphics();
Frame frm = new fltsm( );
frm.setSize (SCREEN_LENGTH, SCREEN_HEIGHT);
frm.setTitle("Flight Simulator");
frm.setVisible(true);
}
public void keyPressed(KeyEvent event)
{
kCode = event.getKeyCode();
keyWord = String.valueOf(kCode);
if (timerStarted == false)
{
ObjectListener x = new ObjectListener ();
t = new Timer(TIMER_DELAY, x);
t.start();
timerStarted = true;
}
}
public void keyTyped (KeyEvent event )
{
removeKeyListener(this); // removes unwanted keyListeners (this method cannot be called in the ObjectListener class)
}
public void keyReleased ( KeyEvent event)
{
}
public KeyEvent getEvent ()
{
return event;
}
class ObjectListener implements ActionListener // Called to process objects that move on the screen
{ // regardless of whether a key is pressed.
public void actionPerformed(ActionEvent event)
{
keyTyped(getEvent()); // This line clears out the unwanted keylisteners, which would otherwise build up everytime the paint() method is called.
repaint();
calledByEvent = false;
}
}
class an implements AbstractAction // Called to process objects that move on the screen
{ // regardless of whether a key is pressed.
public void actionPerformed(ActionEvent event)
{
TrackingBot.botMania();
}
}
}
答案 0 :(得分:2)
private static final int KEY_0 = 48;
不要使用魔法值。没有人知道48是什么。我假设KEY_0代表KeyEvent.VK_0,所以使用该变量并不构成你自己的变量。
我最近决定使用KeyBinds。问题是,我无法弄清楚如何将它们添加到AWT框架
您无法向Frame添加绑定。键绑定仅适用于Swing组件。不要混合AWT和Swing。只需将JFrame与JPanel一起使用,即可轻松使用Key Binding。
public void paint(Graphics g)
不要覆盖顶级容器的paint()方法。自定义绘制是通过覆盖JPanel(或JComponent)的paintComponent()方法完成的。然后将面板添加到框架中。
JPanel panel = new JPanel();
add(panel);
不要在绘画方法中为框架创建和添加组件。绘画方法只应该绘画。
panel.getInputMap().put(KeyStroke.getKeyStroke("1", "doSomething");
panel.getActionMap().put("doSomething", new anAction());
默认的InputMap仅支持组件具有焦点的KeyBindings。默认情况下,JPanel没有焦点。更简单的方法是使用其他InputMaps之一。阅读How to Use Key Bindings上的Swing教程,了解有关这些其他InputMaps的更多信息。