2014-10-04 22 views
0

我創建的JFrame窗口在所謂的核心,然後在主創造這樣的窗口中的一個類:的JFrame - 從KeyListener的類訪問我的窗口

Core window = new Core("GAME1", 0, 0, true, true); 

但後來我決定使用的KeyListener,我創建了另一個類所謂Core_ControlsL: 我的問題是,在這裏:

else if(keyCode == 113) 
     { 
     GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this.window); 
     } 

我無法弄清楚如何這樣,當我按下F2它進入全屏再次訪問窗口。

import javax.swing.UnsupportedLookAndFeelException; 

這是我main.java:

public class Main 
{ 
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException 
    { 
     Core window = new Core("GAME1", 0, 0, true, true); 
    } 
} 

這是Core.java:

import java.awt.GraphicsEnvironment; 
import java.awt.Window; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JFrame; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.WindowConstants; 

public class Core extends JFrame implements ActionListener 
{ 
    private final String Game_Title; 
    private int Window_Width; 
    private int Window_Height; 
    private boolean isVisible; 
    private boolean isResizeable; 

    public Core(String Game_Title, int Window_Width, int Window_Height, boolean isVisible, boolean isResizeable) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException 
    { 
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 

     this.Game_Title = Game_Title; 
     this.Window_Width = Window_Height; 
     this.Window_Height = Window_Height; 
     this.isResizeable = isResizeable; 
     this.isVisible = isVisible; 

     //Create JFrame 
     JFrame window = new JFrame(Game_Title); 
     window.setSize(Window_Width, Window_Height); 
     window.setResizable(isResizeable); 
     window.setExtendedState(JFrame.MAXIMIZED_BOTH); 
     window.setVisible(isVisible); 
     window.addKeyListener(new Core_Controls()); 
     window.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
     GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(window); // make window full screen 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) 
    { 

    } 
} 

而且Core_Controls.java:

import java.awt.GraphicsEnvironment; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

public class Core_Controls implements KeyListener 
{ 
    @Override 
    public void keyTyped(KeyEvent e) 
    { 
    } 

    @Override 
    public void keyPressed(KeyEvent e) 
    { 
     int keyCode; 
     keyCode = e.getKeyCode(); 
     System.out.println(keyCode); // LLLLLL 
     if(keyCode == 112) 
     { 
     GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(null); 
     } 
     else if(keyCode == 113) 
     { 
     GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(null); 
     } 
    } 

    @Override 
    public void keyReleased(KeyEvent e) 
    { 
    } 
} 
+0

仍試圖找出這一個.... – 2014-10-07 21:12:23

回答

1

傳聆聽者窗口:

Core window = new Core("GAME1", 0, 0, true, true); 
Core_ControlsL listener = new Core_ControlsL(window); 

當您處理它時,請重命名您的類,以便重新使用標準Java命名約定。並使用完整的單詞而不是不可讀的縮寫。你會在2周內感謝你自己,重新閱讀你自己的代碼。

+0

修改我的代碼,這樣你就可以清楚地看到發生了什麼事情,我開始學習Java今年UNI和我還是新有了它,任何代碼優化是頗受歡迎。 這不是我想要構建整個遊戲,而只是練習。 – 2014-10-04 12:02:18

0
  1. 不要使用 「幻數」。閱讀代碼時,「112」和「113」對任何其他人都沒有任何意義。
  2. 更好的是,不要使用KeyListener。

Swing設計用於Key Bindings。查看Key Bindings的一些基本信息,以及How to Use Key Bindings上Swing教程的鏈接以及帶鍵綁定的工作示例的鏈接。