Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  JFrame, JPanel, JLabel issues  (Read 422 times)
0 Members and 1 Guest are viewing this topic.
Offline Skwizz

Junior Newbie





« Posted 2013-05-24 04:42:09 »

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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
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");
   }
}
Offline GabrielBailey74
« Reply #1 - Posted 2013-05-24 05:02:09 »

(You just had the order of things wrong)

It should go like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
   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)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
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..
}

Offline Skwizz

Junior Newbie





« Reply #2 - Posted 2013-05-24 05:18:55 »

That didn't work, I'm afraid. JPanel still isn't being added to the frame.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 65K
« Reply #3 - Posted 2013-05-24 05:45:14 »

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.

Offline Skwizz

Junior Newbie





« Reply #4 - Posted 2013-05-24 06:42:44 »

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?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (23 views)
2014-08-19 09:29:53

Tekkerue (22 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (57 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (36 views)
2014-08-06 19:49:38

BurntPizza (66 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!