JFrame - 从keylistener类访问我的窗口

时间:2014-10-04 11:46:00

标签: java jframe

我正在创建JFrame窗口在一个名为Core的类中,然后在Main中创建如下窗口:

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)
   {
   }
}

2 个答案:

答案 0 :(得分:1)

将窗口传递给侦听器:

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

当你在它的时候,重命名你的类,以便它重新考虑标准的Java命名约定。并使用完整的单词而不是不可读的缩写。当你重新阅读自己的代码时,你会在两周内感谢自己。

答案 1 :(得分:0)

  1. 不要使用"魔术数字"。 " 112"和" 113"对任何阅读该代码的人都没有任何意义。
  2. 更好的是,不要使用KeyListener。
  3. Swing旨在与Key Bindings一起使用。查看Key Bindings以获取一些基本信息,以及How to Use Key Bindings上Swing教程的链接以及带有键绑定的工作示例的链接。