1

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();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tom T
  • 59
  • 1
  • 1
  • 12
  • 1
    `Toolkit#getScreenSize` is not your best choice for determining the screen's size, as it doesn't take into account things like the dock or taskbar, which could force you window under those element. You have a number of options, you could use something like [this](http://stackoverflow.com/questions/27031807/how-to-set-present-screensize-in-java-swing/27031968#27031968) to calculate the safe/viewable area, you could use [Full-Screen Exclusive Mode API](https://docs.oracle.com/javase/tutorial/extra/fullscreen/) – MadProgrammer Oct 30 '15 at 23:07
  • 1
    or you could use [`JFrame#setExtendedState`](https://docs.oracle.com/javase/8/docs/api/java/awt/Frame.html#setExtendedState-int-) and simply maximise the window – MadProgrammer Oct 30 '15 at 23:08

2 Answers2

3
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?

BrendanDodd
  • 237
  • 2
  • 10
  • aha good point, thank you i cant believe i missed that, sat there scratching my head for a good half an hour :P thank you your a life saver – Tom T Oct 30 '15 at 22:56
3

What you are doing is assigning frame to a new MainWindow that contains all your components. But then, you re-assign frame to a new JFrame, and a JFrame by default has no components.

Wesley
  • 31
  • 3
  • thank you for the fast response, i hadn't realized i was resetting it more silly human errors on my part aha but thank you for answering – Tom T Oct 30 '15 at 22:59