Home > Enterprise >  How to use 1 JFrame for all the classes?
How to use 1 JFrame for all the classes?

Time:12-15

Good day. I would like to ask how can I use this 1 class that contain the details of my main frame to be used as the frame of all class that I will make? I want this to be the frame of all my classes. Thank you in advance.

package ThinkNotOfficial;

import javax.swing.*;
import java.awt.*;

public class MainFrame{

    // Global Variables
    JFrame mainFrame = new JFrame("Base Frame (global)");
    ImageIcon logo = new ImageIcon("Logo.png");

    MainFrame(){
        mainFrame.setSize(720, 720);
        mainFrame.setResizable(false);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setIconImage(logo.getImage());
        mainFrame.getContentPane().setBackground(new Color(255,255,255));
        mainFrame.setLocationRelativeTo(null);

        mainFrame.setVisible(true);
    }
}

CodePudding user response:

You should create properties class. And each other class for JFrame window


public class FrameProperties {
    JFrame mainFrame = new JFrame("Base Frame (global)");
    ImageIcon logo = new ImageIcon("Logo.png");

    public void Properties(){
        mainFrame.setSize(720, 720);
        mainFrame.setResizable(false);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setIconImage(logo.getImage());
        mainFrame.getContentPane().setBackground(new Color(255,255,255));
        mainFrame.setLocationRelativeTo(null);

        mainFrame.setVisible(true);
    }

}

And then you can create your Frame classes and add your method to it


public class MainFrame extends JFrame{

    // Global Variables


   MainFrame(){
       FrameProperties frameProperties = new FrameProperties();
       frameProperties.Properties();

    }



public class SecondFrame extends JFrame {

   SecondFrame(){
       FrameProperties frameProperties = new FrameProperties();
       frameProperties.Properties();

    }
}
   public static void main(String[] args) {
        new MainFrame();
        new SecondFrame();
    }
  • Related