Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to access JFrame from another class

2e4b0d10-e3bb-4b94-ba2a-ed2a96f2563fOct 9 2014 — edited Oct 9 2014

This is my main.java:

import javax.swing.UnsupportedLookAndFeelException;

public class Main

{

   public static void main(String[] args)

         throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException

   {

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

   }

}

And this is my Core.java:

import java.awt.GraphicsEnvironment;

import java.awt.Window;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import javax.swing.JFrame;

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

   { 

      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.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

      window.addKeyListener(new Key_Listener()); // Adding the Key_Listener to the window

      GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(window); // make the window full screen

   }

   @Override

   public void actionPerformed(ActionEvent e)

   {

      //For Buttons and other interface stuff

   }

  

   // KEY LISTENER:

   public class Key_Listener implements KeyListener

   {

      // 112 - F1

      // 113 - F2

      // 114 - F3

      // 115 - F4

      // 27 - ESC

      private char character;

      private int KeyPressed;

      @Override

      public void keyTyped(KeyEvent e)

      {

        

      }

      @Override

      public void keyPressed(KeyEvent e)

      {

         this.character = e.getKeyChar();

         this.KeyPressed = e.getKeyCode();

         System.out.println(character + "::" + KeyPressed);

         if(this.KeyPressed == 112)

         {

            GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(null);

         }

         else if (this.KeyPressed == 113)

         {

            GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(null);

         }

         else if (this.KeyPressed == 114)

         {

            System.out.println("F3");

         }

      }

      public int returnPressedKey()

      {

         return this.KeyPressed;

      }

      @Override

      public void keyReleased(KeyEvent e)

      {

        

      }    

   }

}

In Core.Java at line 80 how can I access window (

JFrame window = new JFrame(Game_Title); // <- this window

) ?

Also should I use throws or try and catch ?

This post has been answered by TPD-Opitz on Oct 9 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 6 2014
Added on Oct 9 2014
5 comments
2,859 views