2012-11-21 22 views
2
import javax.swing.*; 

import java.awt.*; 
import java.awt.event.*; 
import java.util.ArrayList; 

public class Game extends JPanel{ 
private static JFrame primary= new JFrame("Game"); 
private JButton x1; 

public Game(){ 
    x1= new JButton("YES"); 
    x1.addActionListener(new LevelChoice(1)); 
    add(x1); 
} 

public static void setScreen(JPanel jp){ 


    //primary.removeAll(); 
    //System.out.println("hi"); 
    //primary.revalidate(); 
    //primary.repaint(); 

} 

public static void main(String[] args){ 

    primary.setPreferredSize(new Dimension(1000, 700)); 
    /*primary.add(new LevelHUD("xxxxxxxxxxxxxxxxxxxx" + 
      "xoooooooooooooooooox" + 
      "xoooooooooooooooooox" + 
      "xooomoooooooooooooox" + 
      "xoooooooooxoooooooox" + 
      "xoooooooooxooooomoox" + 
      "xoooommoooxxxxooooox" + 
      "xoooomooooooooooooox" + 
      "xoooomooomooooooooox" + 
      "xooomooooolooomoooox" + 
      "xoooomcoooooooooooox" + 
      "xooomococoooooooooox" + 
      "xooomocoooloooooooox" + 
      "xgoomocoooooooooooox" + 
      "xcooooogooooooooooox" + 
      "xocooooocoooooooooox" + 
      "xoococooolooogooooox" + 
      "xoooooooooooooooooox" + 
      "xxxxxxxxxxxxxxxxxxxx"));*/ 

    primary.add(new Game()); 



    primary.setResizable(false); 
    primary.setVisible(true); 
    primary.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    primary.pack(); 
} 

private class LevelChoice implements ActionListener{ 

    private int level; 

    public LevelChoice(int i){ 
     level=i; 
    } 

    public void actionPerformed(ActionEvent e) { 
     //Game.setScreen(new LevelHUD(gamelevel1)); 
     primary.add(new LevelHUD("xxxxxxxxxxxxxxxxxxxx" + 
       "xoooooooooooooooooox" + 
       "xoooooooooooooooooox" + 
       "xooomoooooooooooooox" + 
       "xoooooooooxoooooooox" + 
       "xoooooooooxooooomoox" + 
       "xoooommoooxxxxooooox" + 
       "xoooomooooooooooooox" + 
       "xoooomooomooooooooox" + 
       "xooomooooolooomoooox" + 
       "xoooomcoooooooooooox" + 
       "xooomococoooooooooox" + 
       "xooomocoooloooooooox" + 
       "xgoomocoooooooooooox" + 
       "xcooooogooooooooooox" + 
       "xocooooocoooooooooox" + 
       "xoococooolooogooooox" + 
       "xoooooooooooooooooox" + 
       "xxxxxxxxxxxxxxxxxxxx")); 
     revalidate(); 

    } 

} 


} 

好了,所以我有大約12類遊戲的ATM,但我不會去與我的但─遊戲細節涉及運動玩家通過按鍵聽衆使用箭頭鍵。這是我的主要課程 - 現在發生的是這個。我試圖讓一個人可以選擇他們想要玩的級別 - 級別A或級別B.然後添加一個包含該遊戲級別的JPanel供用戶玩 - 如果我將遊戲級別添加到主要的方法,一切工作正常!通過按鈕操作監聽器添加遊戲的一個JPanel(使用的KeyListener)禁用的KeyListener

當我點擊按鈕然後添加JPanel時,玩家不能移動,這就是它 - 所有關卡中的怪物都可以很好地工作。

有什麼想法? ButtonListener是否會覆蓋KeyListener或什麼?順便說一下,setFocusable在遊戲關卡面板類中已經設置爲true,所以我懷疑這是一個問題

回答

0

KeyListeners要求他們正在監聽的組件,有焦點,當你點擊一個按鈕,焦點變化和鍵不再「發送」給您的組件。

你有兩種選擇。更改您的代碼中使用Key Bindings(首選)或要接收按鍵事件

與例如更新演示的核心問題

簡單的例子在面板上調用requestFocusInWindow。焦點必須返回到組件KeyListener,它不足以集中其子組件或父組件。

你應該確保的重點要求的是美國東部時間內進行,否則有可能,它可能被偷

public class TestKeyListener { 

    public static void main(String[] args) { 
     new TestKeyListener(); 
    } 

    public TestKeyListener() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       KeyPane keyPane = new KeyPane(); 
       frame.add(keyPane); 
       frame.add(new ButtonPane(keyPane), BorderLayout.SOUTH); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class KeyPane extends JPanel { 

     private JLabel keyed; 
     public KeyPane() { 

      setLayout(new GridBagLayout()); 
      keyed = new JLabel("..."); 
      add(keyed); 
      setFocusable(true); 
      requestFocusInWindow(); 

      addKeyListener(new KeyAdapter() { 

       @Override 
       public void keyPressed(KeyEvent e) { 
        keyed.setText(KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers()).toString()); 
       } 

      }); 

      addFocusListener(new FocusListener() { 

       @Override 
       public void focusGained(FocusEvent e) { 
        keyed.setText("Focus gained"); 
       } 

       @Override 
       public void focusLost(FocusEvent e) { 
        keyed.setText("Focus lost"); 
       } 

      }); 

     } 

    } 

    public class ButtonPane extends JPanel { 

     public ButtonPane(final Component focus) { 
      setLayout(new GridBagLayout()); 
      JButton bad = new JButton("Steal Focus"); 
      JButton good = new JButton("Return Focus"); 
      good.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        focus.requestFocusInWindow(); 
       } 
      }); 

      add(good); 
      add(bad); 
     } 

    } 

} 
+0

LevelHUD lhud =新LevelHUD(「XXXXXXXXXXXXXXXXXXXX」); \t \t \t \t \t \t primary.add(lhud); \t \t \t primary.revalidate(); \t \t \t lhud.requestFocusInWindow(); 好吧,我試過了,但它沒有工作... – Skorpius

+0

真的嗎?爲我工作正常,檢查更新。記住點擊一個按鈕可以給按鈕提供焦點。 – MadProgrammer