2011-06-18 189 views
0

我希望能夠註冊觸發布爾變量的按鍵事件。我已經在我的主類中完成了,但是現在我正試圖將它放到類中,而且它似乎並沒有工作。鍵監聽器事件沒有觸發?

這裏是英雄職業:

public class Hero extends Main { 
private boolean downPressed; 
private boolean leftPressed; 
private boolean rightPressed; 

public void init() { 
} 

public void paint(Graphics g, int x_pos, int y_pos) { 

    if (isDownPressed() && isLeftPressed()) { 
     this.sprite = hero225; 
    } else if (isDownPressed() && isRightPressed()) { 
     this.sprite = hero135; 
    } else if (isUpPressed() && isRightPressed()) { 
     this.sprite = hero45; 
    } else if (isUpPressed() && isLeftPressed()) { 
     this.sprite = hero315; 
    } else if (isLeftPressed() == true) { 
     this.sprite = hero270; 
    } else if (isRightPressed() == true) { 
     this.sprite = hero90; 
    } else if (isUpPressed() == true) { 
     this.sprite = hero; 
     System.out.println("Succsess"); 
    } else if (isDownPressed() == true) { 
     this.sprite = hero180; 
    } 

    // this.sprite will contain value set on last "movement" 
    g.drawImage(this.sprite, x_pos, y_pos, this); 
} 

public void keyPressed(KeyEvent e) { 
    if (e.getKeyCode() == KeyEvent.VK_LEFT) { 
     setLeftPressed(true); 
     System.out.println("keyPressed"); 
    } 
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) { 
     setRightPressed(true); 

    } 
    if (e.getKeyCode() == KeyEvent.VK_UP) { 
     setUpPressed(true); 
    } 
    if (e.getKeyCode() == KeyEvent.VK_DOWN) { 
     setDownPressed(true); 
    } 

} 

public void keyReleased(KeyEvent e) { 

    if (e.getKeyCode() == KeyEvent.VK_LEFT) { 
     setLeftPressed(false); 
    } 
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) { 

     setRightPressed(false); 
    } 
    if (e.getKeyCode() == KeyEvent.VK_UP) { 
     setUpPressed(false); 

    } 
    if (e.getKeyCode() == KeyEvent.VK_DOWN) { 
     setDownPressed(false); 
    } 
} 

public void keyTyped(KeyEvent e) { 

} 

public void mouseClicked(MouseEvent e) { 
    System.out.println("HIT!"); 

} 

public void mouseEntered(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 

public void mouseExited(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 

public void mousePressed(MouseEvent e) { 
    boolean isButtonPressed = true; 

} 

public void mouseReleased(MouseEvent e) { 
    boolean isButtonPressed = false; 

} 


public void setDownPressed(boolean downPressed) { 
    this.downPressed = downPressed; 
} 

public boolean isDownPressed() { 
    return downPressed; 
} 

public void setLeftPressed(boolean leftPressed) { 
    this.leftPressed = leftPressed; 
} 

public boolean isLeftPressed() { 
    return leftPressed; 
} 

public void setRightPressed(boolean rightPressed) { 
    this.rightPressed = rightPressed; 
} 

public boolean isRightPressed() { 
    return rightPressed; 
} 

public void setUpPressed(boolean upPressed) { 
    this.upPressed = upPressed; 
} 

public boolean isUpPressed() { 
    return upPressed; 
} 

在此可以調用它的級別類:

public class Level extends Main { 
Hero hero = new Hero(); 

public void paint(KeyEvent e, Graphics g, double x_pos, double x_pos2) { 
    repaint(); 
} 

,這裏是在調用主類的塗料功能:

public void paint(Graphics g) { 
    Level level = new Level(); 
    level.paint(e, g, x_pos, y_pos); 

造成這種情況的問題似乎並不明顯。

回答

2

你應該實現java.awt.event.KeyListener接口要監聽事件類(ES),在這種情況下英雄,所以:

public class Hero extends Main implements java.awt.event.KeyListener 

,然後註冊在某些方法的事件,也許init或別的地方使用:

addKeyListener(this); 
//rest of your code 

或者,如果你要聽的只是一些事件,你可以而不是實現接口KeyListener的,註冊一個適配器:

addKeyListener(new java.awt.KeyAdapter() 
{ 
    public void keyPressed(java.awt.KeyEvent e) 
    { 
     //handle just this event 
    } 
} 
); 
//rest of of your code 
+0

我得到這個錯誤,當我按照第二步:方法addEventListener(英雄)是未定義的類型英雄 – Derek

+0

我編輯了我的答案,所以你可以測試它 –

+0

我設法讓它在英雄類工作(Woop!)但它在Level類中不起作用。我所做的是把一個System.out.println(「keyPressed」);在if(isDownPressed()&& isLeftPressed()){。但是當我加載了Level並按下左鍵時:控制檯中沒有任何事情發生: – Derek

1

聽起來像焦點問題:只有活動組件纔會獲得KeyEvents。

有幾種方法可以解決這個問題,但我發現this教程涵蓋了很好的想法。但是,如果你想要一個快速和骯髒的(骯髒的?不知道,簡單和簡單不壞,我會想;只是有點臃腫的大型項目)解決方案,你可以實現一個監聽器爲每個組件,並轉發事件一些處理它的普通類。

+0

+1「爲了觸發鍵盤事件,一個組件必須具有鍵盤焦點。「 - [如何編寫關鍵偵聽器](http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html) – trashgod