2015-01-07 75 views
1

我創建的JFrame和一些矩形它使用g.fillRect中,例如:添加的KeyEvent的NumLock鍵鍵盤上的按鍵在JFrame中

Rectangle rec1 = new Rectangle(0, 10, 50, 50); 
    g.fillRect((int) rec1.getX(), (int) rec1.getY(), (int) rec1.getWidth(), 
      (int) rec1.getHeight()); 

現在我想知道當有人按下幾個數字鎖定的鑰匙,我已經使用這個代碼的嘗試:

public void keyPressed(KeyEvent e) { 
    if (e.getKeyCode() == KeyEvent.VK_UP) { 
     y = y - 50; 
    } 

這個代碼不工作,我不知道爲什麼,我需要的KeyListener每個numlock密鑰1-9 .. 我該怎麼辦? THX

全碼:

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Rectangle; 
import java.awt.event.KeyEvent; 
import java.util.Timer; 
import java.util.TimerTask; 

import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 

public class Game extends JComponent { 
    private static int y = 0; 

    public static void main(String args[]) { 
     JFrame mainFrame = new JFrame("BlueTAP Game"); 
     mainFrame.getContentPane().add(new Game()); 
     mainFrame.setSize(205, 225); 
     mainFrame.setVisible(true); 
     mainFrame.setFocusable(true); 
     final JFrame parent = new JFrame(); 
     JOptionPane 
       .showMessageDialog(
         parent, 
         "Shown only one time! - In this game, the buttons are arranged as the keyboard numpad buttons. you are supposed to click on the right keyboard button place by the grey button place.", 
         "Game Instructions", JOptionPane.PLAIN_MESSAGE); 
     final Timer timer = new Timer(); 
     timer.scheduleAtFixedRate(new TimerTask() { 
      public void run() { 
       System.out.println("H" + y); 
      } 
     }, 0, 1000); 
    } 

    public void paint(Graphics g) { 

     Rectangle rec1 = new Rectangle(10, 10, 50, 50); 
     g.fillRect((int) rec1.getX(), (int) rec1.getY(), (int) rec1.getWidth(), 
       (int) rec1.getHeight()); 
     Rectangle rec2 = new Rectangle(70, 10, 50, 50); 
     g.fillRect((int) rec2.getX(), (int) rec2.getY(), (int) rec2.getWidth(), 
       (int) rec2.getHeight()); 
     Rectangle rec3 = new Rectangle(130, 10, 50, 50); 
     g.fillRect((int) rec3.getX(), (int) rec3.getY(), (int) rec3.getWidth(), 
       (int) rec3.getHeight()); 
     Rectangle rec4 = new Rectangle(10, 70, 50, 50); 
     g.fillRect((int) rec4.getX(), (int) rec4.getY(), (int) rec4.getWidth(), 
       (int) rec4.getHeight()); 
     Rectangle rec5 = new Rectangle(70, 70, 50, 50); 
     g.fillRect((int) rec5.getX(), (int) rec5.getY(), (int) rec5.getWidth(), 
       (int) rec5.getHeight()); 
     Rectangle rec6 = new Rectangle(130, 70, 50, 50); 
     g.fillRect((int) rec6.getX(), (int) rec6.getY(), (int) rec6.getWidth(), 
       (int) rec6.getHeight()); 
     Rectangle rec7 = new Rectangle(10, 130, 50, 50); 
     g.fillRect((int) rec7.getX(), (int) rec7.getY(), (int) rec7.getWidth(), 
       (int) rec7.getHeight()); 
     Rectangle rec8 = new Rectangle(70, 130, 50, 50); 
     g.fillRect((int) rec8.getX(), (int) rec8.getY(), (int) rec8.getWidth(), 
       (int) rec8.getHeight()); 
     g.setColor(Color.LIGHT_GRAY); 
     Rectangle rec9 = new Rectangle(130, 130, 50, 50); 
     g.fillRect((int) rec9.getX(), (int) rec9.getY(), (int) rec9.getWidth(), 
       (int) rec9.getHeight()); 
    } 

    public void keyPressed(KeyEvent e) { 
     if (e.getKeyCode() == KeyEvent.VK_NUMPAD0) { 
      y = y - 50; 
     } 
    } 
} 
+0

沒有'KeyListener'在你的代碼。您只是添加了一個不覆蓋或實現某些內容的方法。沒有理由爲什麼AWT/Swing應該調用這種方法。請參閱[「如何編寫密鑰監聽器」](http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html) – Holger

+0

@Holger嘿,我試圖使用KeyListener,但我無法做到因爲我正在使用這一行:\t \t mainFrame.addKeyListener(this);但你不能在一個靜態的上下文中使用它(在公共靜態無效的主...) – user3731180

+0

@Holger Thx,我明白了:) – user3731180

回答

2

您需要使用數字鍵盤鍵值:

VK_NUMPAD0 
VK_NUMPAD1 
VK_NUMPAD2 

...

VK_NUMPAD9 
+0

仍然無法正常工作:(THX無論如何,上傳完整的代碼.. – user3731180

+1

@ user3731180您需要將KeyListener添加到每個組件中,這就是爲什麼我建議您使用其他方式:請參閱[本教程](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html )一個替代方法.. –

+0

@newbiedoodle但是,你是什麼意思,我需要添加keylistener到每個組件?我只想讓計算機注意到當我按下一個鍵,所以我只需要將其添加到JFrame不是? – user3731180