-1

I'm trying to create a GUI, and I want to place elements in certain places. I made the layout of my panel null, so I could do this. However, Nothing will appear when the panel is null.
Here's the code:

public class OverView extends JFrame {

    //height and width of screen
    Toolkit tk = Toolkit.getDefaultToolkit();
    int x = ((int) tk.getScreenSize().getWidth());//length of screen
    int y = ((int) tk.getScreenSize().getHeight());//height

    //components
    private JLabel title;
    private JLabel description;
    private JPanel panel;
    private ArrayList<JButton> farms;

    //farm variables
    public ArrayList<Farm> owned;


    public OverView(ArrayList<Farm> owned) {
        super("The Lolipop Farm - Overview");
        setSize(700, 700);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);

        //initialize variables
        this.owned = owned;
        panel = new JPanel();
        panel.setLayout(null);
        title = new JLabel("<html>Your Farms - The Lolipop Farm"
                + "<br> <font size=1000> <i> An Eph Production </i> </font></html>");

        //set background color, color, and font of JComponents
        title.setFont(new Font("serif", Font.BOLD, 25));
        title.setBackground(Color.GRAY);
        title.setOpaque(true);

        //set size and location of the components
        title.setSize(350, 120);
        title.setLocation(x / 2, 600);

        //add to panel
        panel.add(title);

        //add panel to the screen
        add(panel);         

    }

}

Why isn't the panel showing anything when the layout's null?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    *"I want to place elements in certain places"* Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 29 '15 at 16:23
  • Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Aug 29 '15 at 17:27

2 Answers2

2

As Overview is a Frame, I think you must call the method

setVisible(true);

according to https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html in order to make it visible . Now, if that doesn't work, I wonder if you have created an instance of the Overview class somewhere else in your code, or in the Main method. If you haven't, then there is no object that can show the panel inside of your class so your program won't show anything.

nonameable
  • 426
  • 3
  • 11
  • *"Now, if that doesn't work.."* It doesn't. *"If you haven't, then there is no object that can show the panel inside of your class so your program won't show anything."* Even after the frame is set visible, there is no panel, for one simple reason that I feel it is best not to reveal., – Andrew Thompson Aug 29 '15 at 17:25
  • I have set the frame visible, and the instance of OverView is created in another class. – Scott Wayne Aug 29 '15 at 18:28
  • *"I have set the frame visible.."* That is best shown in a [mcve]/ As an aside, that code above does not compile, even with the relevant imports, also include the `main(String[])` needed to run it and show the frame on screen. – Andrew Thompson Aug 30 '15 at 07:22
0

Your problem is with the code

setLayout(null);

This will set the layout of the JFrame to null since you are extending (inheriting it). You must have a layout for a JFrame although you can do without layout for JPanel. Just remove that line and it will be fine.

EDIT: And of course you need to call setVisible(true) like the other guy said.