0

在过去的几个月里,我一直在使用 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();
   }     
} 
}  
4

1 回答 1

2
private static final int KEY_0 = 48;

不要使用魔法值。没有人知道48是什么。我假设 KEY_0 代表 KeyEvent.VK_0,所以请使用该变量,不要自己编造。

我最近决定改用 KeyBinds。问题是,我不知道如何将它们添加到 AWT 框架

您不能将绑定添加到框架。键绑定仅适用于 Swing 组件。不要混合 AWT 和 Swing。只需将 JFrame 与 JPanel 一起使用,即可轻松使用 Key Bindings。

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 没有焦点。更简单的方法是使用其他 InputMap 之一。阅读有关如何使用键绑定的 Swing 教程以了解有关这些其他 InputMap 的更多信息。

于 2013-06-20T00:05:05.367 回答