JFrame, JPanel, JLabel issues

I seem to be having some issues. The main class, Main.java, loads the JPanel from World.java, which loads JLabels from Ineo.java. SO far, the only thing that works is Main.java when it opens a new window. The JPanel isn’t being added to the JFrame for some reason, as I’m getting a blank window which should at least be black due to the background color, if the JPanel was being added to the JFrame.

Main.java

package poquest;

import javax.swing.JFrame;

public class Main extends JFrame {
	private static final long serialVersionUID = 1L;
	
	int w = 32*40;
	int h = 32*25;
	
	static JFrame jf;
	
	public Main(){
		setTitle("PoQuest");
		setSize(w, h);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		
		add(World.jp);
	}
	
	public static void main(String[] args) {
		new Main();
	}
}

World.java

package poquest;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JPanel;

import poquest.world.Ineo;

public class World extends JPanel {
	private static final long serialVersionUID = 1L;
	
	public static JPanel jp = new JPanel();
	
	public World() {
		jp.setLayout(new GridLayout(Ineo.gridW, Ineo.gridH));
		jp.setBackground(Color.BLACK);
		
		for (int i = 0; i < Ineo.jlNum; i++) jp.add(Ineo.jl[i]);
	}
}

Ineo.java

package poquest.world;

import javax.swing.JLabel;

//import poquest.world.Tiles;

public class Ineo {
	public static JLabel jl[];
	
	public static int gridW = 2;
	public static int gridH = 2;
	
	public static int jlNum = 4;
	
	public Ineo() {
		jl[1] = new JLabel("flower1");
		jl[2] = new JLabel("flower2");
		jl[3] = new JLabel("flower3");
		jl[4] = new JLabel("treeTiny");
	}
}

(You just had the order of things wrong)

It should go like:


	public Main() {
		setTitle("PoQuest");
		setSize(w, h);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		add(World.jp);
		pack(); // Pack the frame after adding all the components.
		setVisible(true); // Finally, set the frame visible.
		setLocationRelativeTo(null); // Will center the frame after setVisible(true) is called.
	}

How it’s ‘usually’ done:

  1. Create the frame (Title, size, resizing etc…)
  2. Add components to the frame (JPanel, JButton etc…)
  3. Pack the frame.
  4. Set the frame visible.
  5. (Set the location relative to null after visible for it to center)

By the way, it’s a good practice to call super(“TITLE”) in the constructor of a class that extends the JFrame.

It would also be a good idea to create a Instance of the World.Java class instead of just adding the JPanel ‘jp’ staticly.
(Prevent future errors like variables not getting instantiated)


public Main() {
// prior to creating the JFrame..
	World world_instance = new World();

// initialize the JFrame...

// add the jpanel to the JFrame
	add(world_instance.jp);

// pack the JFrame, setVisible etc..
}

That didn’t work, I’m afraid. JPanel still isn’t being added to the frame.

Add it to the content pane of the JFrame.
Generally only use static fields for very good reason. Learn to get along without static first.
It is good practice to no inherit from JFrame and but to use one.

So would I have the frame in Main, content pane in World, then the JPanel in Ineo?

Since I can stack multiple JPanels on top of each other for a “layered” effect (Which will be used in the future), then consolidate them into one JPanel in World and add the mega panel to the content pane, then ad that to the JFrame in Main?