[J2SE] KEY FOCUS
在menu時還可以用key來控制選單
進入某個選項要執行GamePanel時
GamePanel卻要先切換視窗到別的地方
再切回來才能讓我用key控制
爬文大部分人都說是focus問題
可是我試著自己去抓focus還是不成功
希望有高手能幫忙一下
感覺是觀念錯了...
另外 if那一句話在執行時
compile會出現下列文字:
Exception in thread "main" java.lang.NullPointerException at if的那一行
public static void main(String[] args)
{
Menu menu = new Menu();
frame.add( menu , BorderLayout.CENTER );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(new Dimension(window_width, window_height));
frame.setVisible(true);
while(true)
{
if( menu.getflag() )
{
frame.remove( menu );
menu = null;
System.gc();
GamePanel game = new GamePanel();
game.requestFocusInWindow();
game.grabFocus() ;
game.requestFocus();
frame.add(game,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(window_width, window_height));
frame.setVisible(true);
}
}
}
GamePanel的constructor
public GamePanel(Key key)
{
loadImage( imgname , bf );// 讀取圖片
this.setFocusable(true); // 取得焦點, 否則KeyListener會無作用
addKeyListener(new GameInput()); // Add it to the JPanel
setdirect(key);
new Thread(this).start();
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.122.153
※ 編輯: yeahhey 來自: 140.113.122.153 (06/28 17:28)
→
06/28 19:28, , 1F
06/28 19:28, 1F