Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8404071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:30:16+00:00 2026-06-09T22:30:16+00:00

I need to pass a JPanel extending class to the main class. Here is

  • 0

I need to pass a JPanel extending class to the main class.

Here is what I have so far:
Main class

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

public class main {

    private gamePanel gamepanel = new gamePanel();

    public JPanel createContentPane(){
        // We create a bottom JPanel to place everything on.
        JPanel totalGUI = new JPanel();

        //We set the Layout Manager to null so we can manually place
        // the Panels.
        totalGUI.setLayout(null);

        //Now we create a new panel and add it to the bottom JPanel.

        totalGUI.add(gamepanel);



        totalGUI.setOpaque(true);
        return totalGUI;
    }

    private static void createAndShowGUI(){
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("[=] There's a JPanel in here! [=]");

        //Create and set up the content pane.
        main demo = new main();
        frame.setContentPane(demo.createContentPane());

        //The other bits and pieces that make your program a bit more stable.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700,500);
        frame.setVisible(true);

    }
    public static void main(String[] args) {
        //Schedule a jog for the event-dispatching thread:
        //creating and showing this application's GUI.
        System.out.println(gamepanel);
        SwingUtilities.invokeLater(new Runnable() {
            public void run(){
                createAndShowGUI();
            }
        });
    }
}

The gamePanel class

public class gamePanel extends JPanel implements Commons {

    private Dimension d;
    private ArrayList snowmens;
    private coreFunctions functions = new coreFunctions();
    private int snowmenX = 150;
    private int snowmenY = 5;
    private final String snowmenpix = "snowman.png";
    private JPanel background;

public gamePanel() {



    add(new JLabel("TEST"));
    setFocusable(true);


    setDoubleBuffered(true);
}


@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLUE);
    g.fillRect(0, 0, 700, 700);



}
}

I can’t figure out why the blue background and the label is not being shown…

EDIT:

Here are more details:

Ok so I am trying to make a little 2D game. For that I need to create some snowmen on the gamePanel class and display it via the main class. To start it off, the createContentPane creates a background panel, the root panel if you want. The createandshowgui creates a JFrame.

The gamepanel class is in fact a JPanel which has 1 panel as of now, which is the background panel. For now, I only want it to have a blue background.

I tried putting it like this because I saw some examples and they were pretty similar to what I have, but for some reason, I can’t get it to work….

Thank you,
Ara

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:30:17+00:00Added an answer on June 9, 2026 at 10:30 pm

    You should use LayoutManager instead of setLayout(null); and if only one component is being added no need for it either (that component will stretch to fill unless others are added)

    see here for tutorial on LayoutManagers:

    • http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
    • http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

    If all you are trying to do is have a blue background (on which you can add components) then simply override paintComponent() of your gamePanel and paint the background blue:

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLUE);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
        }
    

    then you can add your JLabel as if it was a normal Panel as the background is now being painted blue and not set by/as a component.

    If you have an image look into g.drawImage(Image image,int x,int y,ImageObserver iO)

    EDIT

    DROP THE CALL TO:

    setLayout(null);

    and it will work

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to pass an x/y around. I was just using java.awt.Point. I do
I need to pass a View's class name as CommandParameter. How to do this?
I need to pass an instance variable (self.rank) to be used by a class
I need to pass a lot of variables to a PHP class from a
I have a problem: In my project, I need pass an integer value from
I have quite specific issue. I have application where I need to import data
I have two Java class hierarchies that share a common ancestor and implement a
I need to pass an extra parameter :mobilejs => true from jQuery to a
I need to pass argument to JNLP dynamically for which I tried using a
I need to pass Cursor object to another activity, what is the best way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.