javaswingawtcontentpane

Swing & AWT: ContentPane items not showing in JFrame


I'm creating a basic Java Swing app but I am having problems with my content pane as it is not showing my objects.

This is my main class this will be calling up other classes in future and is just calling up the Frame.java:

public class Main 
{

    public static void main(String[] args)
    {
       System.out.println("HI");

       Frame frameObject = new Frame();
       frameObject.main();
    }
}

This is the frame class where i create my frame:

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

public class Frame
{
    public static void main()
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame frame = new MainFrame("Warlock of Firetop Mountain");

                //Implementing Toolkit to allow computer to get dimensions of screen and assign them to two int values
                Toolkit tk = Toolkit.getDefaultToolkit();
                int xsize = (int) tk.getScreenSize().getWidth();
                int ysize = (int) tk.getScreenSize().getHeight();

                frame = new JFrame("Warlock of Firetop Mountain");
                frame.setTitle("Warlock of Firetop Mountain");
                frame.setSize(new Dimension(xsize, ysize));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(true);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

This is my mainFrame.java where i store my components:

public class MainFrame extends JFrame
{
    public MainFrame(String title)
    {
        super(title);

        setLayout(new BorderLayout());

        //Components - Buttons
        JButton saveButton =new JButton("Save");

        JButton loadButton =new JButton("Load");

        JButton optionsButton = new JButton("Options");

        JButton inventoryButton =new JButton("Inventory");

        Container buttonsContainer = getContentPane();
        buttonsContainer.add(saveButton, BorderLayout.LINE_START);
        buttonsContainer.add(loadButton, BorderLayout.CENTER);
        buttonsContainer.add(optionsButton,BorderLayout.CENTER);
        buttonsContainer.add(inventoryButton,BorderLayout.AFTER_LINE_ENDS);

        //Components - Enter Button
        JButton enterButton = new JButton("Enter");

        // /Components - ComboBox
        JComboBox pageTurner = new JComboBox();

        //Components = JTextArea
        JTextArea currentText = new JTextArea();
    }
}

Solution

  • frame = new JFrame("Warlock of Firetop Mountain");
    

    Why are you resetting frame with a new instance of JFrame? Didn't you already set it with MainFrame?