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 7090151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:00:39+00:00 2026-05-28T08:00:39+00:00

Ok so I am trying to add a JPanel to a JFrame as so:

  • 0

Ok so I am trying to add a JPanel to a JFrame as so:

            gameClasses[2] = new a2();
            gameClasses[2].setSize(100, 100);
            menu.add(gameClasses[2]);
            menu.setVisible(true);

a2() is a separate class that acts as a JPanel which I use the paintComponent to paint images to it. “menu” is the JFrame. My problem is when I call “gameClasses[2].setSize(100, 100);” it does not resize the JPanel but it stays the same size. Does anyone know what I am doing wrong or how this is supposed to be done because no one else seems to have any issues with this on the internet. Thanks.

EDIT: Here is the code related to menu and a2:

            menu.setSize(swidth / 2 + swidth / 5, sheight / 2 + sheight / 5);
            menu.setLocation((swidth - menu.getWidth()) / 2, (sheight -     menu.getHeight()) / 3);
            menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            menu.setResizable(true);
            menu.remove(main);
            menu.add(gameClasses[0] = new a3());
            menu.add(gameClasses[1] = new a4());
            gameClasses[2] = new a2();
            gameClasses[2].setSize(100, 100);
            gameClasses[2].validate();
            menu.add(gameClasses[2]);
            menu.setVisible(true);

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class a2 extends JPanel {

public static int size = 48;
public static Image grsX = Toolkit.getDefaultToolkit().getImage("tiles/grsX.png");
public static Image grsY = Toolkit.getDefaultToolkit().getImage("tiles/grsY.png");
public static Image grsX1 = Toolkit.getDefaultToolkit().getImage("tiles/grsX1.png");
public static Image grsY1 = Toolkit.getDefaultToolkit().getImage("tiles/grsY1.png");

public a2() {
    System.out.println("a2 loaded...");
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    //draw interface
    for(int y = 0; y < a6.ay; y++) {
        for(int x = 0; x < a6.ax; x++) {
            g.drawImage(a5.Tile_Img.get(a5.ID_Tile.get(a6.area[x][y])), x * size, y * size, size, size, this);
            if(x > 0) {
                if(a6.area[x - 1][y].equals("00") && a6.area[x][y].equals("01")) {
                    g.drawImage(grsX, x * size, y * size, size, size, this);
                }
            }
            if(x < a6.ax - 1) {
                if(a6.area[x + 1][y].equals("00") && a6.area[x][y].equals("01")) {
                    g.drawImage(grsX1, x * size, y * size, size, size, this);
                }
            }
            if(y > 0) {
                if(a6.area[x][y - 1].equals("00") && a6.area[x][y].equals("01")) {
                    g.drawImage(grsY, x * size, y * size, size, size, this);
                }
            }
            if(y < a6.ay - 1) {
                if(a6.area[x][y + 1].equals("00") && a6.area[x][y].equals("01")) {
                    g.drawImage(grsY1, x * size, y * size, size, size, this);
                }
            }
        }
    }
    repaint();
}

}

a3 and a4 are a KeyListener class and a MouseListener class that both extend JPanel

  • 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-05-28T08:00:39+00:00Added an answer on May 28, 2026 at 8:00 am
    • A layout is more likely to respect the preferred size than the size.
    • A call to pack() on the frame will make it become the minimum size needed to display the components inside. Call it after everything is added.
    • Don’t call setLayout(null) (mentioned in comment as ‘non-relevant’ code). Use layouts.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am trying to add 2 JPanel's to a JFrame that take the
I've a JFrame class and two JPanel classes. At first, I want to add
Ok, I am trying to add one row of multiple images to a JPanel,
Trying to add a class object into a List using reflection, but when invoking
Trying to add a dynamic class value to one of my columns in CGridView:
Im trying to add an element to a database and then return a new
i'm trying to add a jtable component to my jPanel but i am unable
I am trying to view a JApplet within a JFrame. Class: Paint public void
I am trying to add a JPanel (well, several) to a JLayeredPane. However, when
Ok so i am trying to get a 3 JPanel JFrame where right and

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.