在過去的幾個月中,我一直在使用Java.Awt.Frame爲鍵盤輸入創建一個屏幕和keyListeners。然而,我的程序已經變得複雜了,因此我最近決定使用KeyBinds。問題是,我無法弄清楚如何將它們添加到AWT框架。我試圖創建一個JPanel並添加到框架(使用add()),但是,這似乎沒有做任何事情。任何輸入將不勝感激;如果需要,請參閱我的代碼。如何將KeyBindings添加到AWT框架?
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();
}
}
}
謝謝你,我消除了魔術數字(它們在鍵盤綁定中是不必要的),我將Jpanel移動到新創建的fltsm構造函數中,並使用pnl.WHEN_IN_FOCUSED_WINDOW作爲getInputMap()的參數。 –
重點是主要問題。 –