Skip to content
Advertisement

How to make an app using swing that only uses 1 JFrame?

I am making an app or (something.exe). I want to make a class that will serve as my main frame. That is because I don’t want to create new frame for each class that I will make. I want my class MainMenu to have the Frame of MainFrame where I can put buttons and etc.

package ThinkNotOfficial;

public class Main {

    public static void main(String[] args) {

        MainFrame mainFrame = new MainFrame();

    }
}
package ThinkNotOfficial;

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

public class MainFrame extends JFrame{

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

    MainFrame (){
        mainFrame.setSize(500, 500);
        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.setLayout(null);

        mainFrame.setVisible(true);

        // ------------ PROGRAM FIELD -----------



    }
}
package ThinkNotOfficial;

import javax.swing.*;

public class MainMenu{

    // ------------ Global Variables ------------
    JButton play = new JButton("PLAY");

    MainMenu(){
        // ------------ PLAY button ------------
        play.setSize(100,50);

        // ------------ Adding parts ------------

    }
}

Advertisement

Answer

Make use of

You can also make use of CardLayout to help facilitate the switching between different views, for example

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MainPane extends JPanel {

        private CardLayout cardLayout;
        private MenuPane menuPane;
        private GamePane gamePane;

        public MainPane() {
            cardLayout = new CardLayout();
            setLayout(cardLayout);

            menuPane = new MenuPane(new MenuPane.Observer() {
                @Override
                public void didStartGame(MenuPane source) {
                    cardLayout.show(MainPane.this, "game");
                }
            });
            gamePane = new GamePane(new GamePane.Observer() {
                @Override
                public void gameDidEnd(GamePane source, int score) {
                    cardLayout.show(MainPane.this, "menu");
                }
            });

            add(menuPane, "menu");
            add(gamePane, "game");
        }

    }

    public class MenuPane extends JPanel {

        public interface Observer {

            public void didStartGame(MenuPane source);
        }

        public MenuPane(Observer observer) {
            setLayout(new GridBagLayout());
            JPanel contentPane = new JPanel(new GridLayout(-1, 1));
            JButton startButton = new JButton("Start");
            startButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    observer.didStartGame(MenuPane.this);
                }
            });

            contentPane.add(startButton);
            add(contentPane);
        }

    }

    public class GamePane extends JPanel {

        public interface Observer {
            public void gameDidEnd(GamePane source, int score);
        }

        public GamePane(Observer observer) {
            setLayout(new GridBagLayout());
            add(new JLabel("All your game is belong to us"));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }
}

JTabbedPane is also a good way for allowing the user to switch between different views, see How to Use Tabbed Panes for more details

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement