2013-06-19 86 views
0

在過去的幾個月中,我一直在使用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(); 
    }  
} 
} 

回答

2
private static final int KEY_0 = 48; 

不要使用魔法值。沒有人知道什麼是48。我假設KEY_0代表KeyEvent.VK_0,因此請使用該變量並且不要自行構建。

我最近決定改用KeyBinds。問題是,我想不出如何將它們添加到AWT框架

您不能將綁定添加到框架。鍵綁定僅適用於Swing組件。不要混用AWT和Swing。只需使用帶有JPanel的JFrame,就可以輕鬆使用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沒有焦點。更簡單的方法是使用其他InputMaps之一。請閱讀有關How to Use Key Bindings的Swing教程以瞭解有關這些其他InputMaps的更多信息。

+0

謝謝你,我消除了魔術數字(它們在鍵盤綁定中是不必要的),我將Jpanel移動到新創建的fltsm構造函數中,並使用pnl.WHEN_IN_FOCUSED_WINDOW作爲getInputMap()的參數。 –

+0

重點是主要問題。 –