Reputation: 23
I am creating JFrame window In one class called Core and then in Main creating the window like this:
Core window = new Core("GAME1", 0, 0, true, true);
But then I decided to use keylistener and I created another class called Core_ControlsL: My problem is that in here:
else if(keyCode == 113)
{
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this.window);
}
I cannot figure out how to access window so that when I press F2 it goes into full screen again.
import javax.swing.UnsupportedLookAndFeelException;
This is my 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);
}
}
This is the 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)
{
}
}
And the 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)
{
}
}
Upvotes: 0
Views: 128
Reputation: 324157
Swing was designed to be used with Key Bindings
. Check out Key Bindings for some basic information as well as a link to the Swing tutorial on How to Use Key Bindings
and links to working examples with key bindings.
Upvotes: 0
Reputation: 692073
Pass the window to the listener:
Core window = new Core("GAME1", 0, 0, true, true);
Core_ControlsL listener = new Core_ControlsL(window);
While you're at it, rename your class so that it reaspects the standard Java naming conventions. And use complete words instead of unreadable abbreviations. You'll thank yourself in 2 weeks when you'll re-read your own code.
Upvotes: 1