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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:02:18+00:00 2026-06-14T19:02:18+00:00

thank you for taking your time in reading this. I hope it is not

  • 0

thank you for taking your time in reading this. I hope it is not confusing, and if any other information is needed, please let me know!

Issue:
I am making a Snake like game and I’ve created a JFrame, and then added the JPanel. Well, if I do not set the size of it in the JFrame it will size to about 100 x 20. When I set the size of it, it stays at the dimensions set. However, if I pack() it, it will shrink back to the 100 x 20 or so, when I have defined the size in the JPanel. I’ve set the bounds as well, with no luck. I have use FlowLayout(), and BorderLayout() with no success either. Now my game runs correctly and have no issues with it, except for the frame. It will cut off the sides, unless i do some gymnastics, which I do not wish to do.

Objective:
I would like to be able to have the Frame size correctly to my width and height specified, and have the panel moved down a bit to add space for text for the game so it is not blocking anything.

Basically I’m confused and flustered that I have spent a few days on this, and I have not been successful with anything so far.

JFrame Class:

public class main extends JFrame {

 /**
  * @param args the command line arguments
  */

 public main(){
     Board game = new Board();

     setLayout(new BorderLayout(0, 50));
     setSize(game.getGameWidth(), game.getGameHeight());
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setLocationRelativeTo(null);
     setTitle("BlockRunner");
     getContentPane().add(game);

     setResizable(false);
     setVisible(true);
 }
}

My Board class:

public class Board extends JPanel implements ActionListener, KeyListener{
 private int width = 600;
 private int height = 400;
 Board(){
     addKeyListener(this);
     setFocusable(true);
     setBackground(Color.LIGHT_GRAY);
     setSize(width, height);
     InitGame();
 }
}

I am willing to try anything someone is willing to put out there for me.
Thank you for your time!

EDIT 1: (Due to a user’s answer.)
using setPerferredSize(), setMinmumSize() and setMaximumSize() does not affect the outcome of this.

  • 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-14T19:02:20+00:00Added an answer on June 14, 2026 at 7:02 pm

    You need to use a combination of getPreferred/Minimum/MaximumSize and a layout that will actually respect it.

    Here, I’ve used a GridBagLayout set up so that the board will always stay the same size, but the cell will use the maximum amount of same.

    If you want to keep the text at the bottom of the board (instead of the frame), remove the weighty constraint (and possibly the weightx) as well

    enter image description hereenter image description here

    public class TestBoardGame {
    
        public static void main(String[] args) {
            new TestBoardGame();
        }
    
        public TestBoardGame() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new GridBagLayout());
    
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.gridx = 0;
                    gbc.gridy = 0;
                    gbc.weightx = 1;
                    gbc.weighty = 1;
                    frame.add(new BoardGamePane(), gbc);
    
                    gbc.gridx = 0;
                    gbc.gridy = 1;
                    gbc.weightx = 1;
                    gbc.fill= GridBagConstraints.HORIZONTAL;
                    JLabel text = new JLabel("Area for text");
                    text.setHorizontalAlignment(JLabel.CENTER);
                    frame.add(text, gbc);
    
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            });
        }
    
        public class BoardGamePane extends JPanel {
    
            public BoardGamePane() {
    
                setBorder(new LineBorder(Color.RED));
                setBackground(Color.ORANGE);
    
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }
    
            @Override
            public Dimension getMinimumSize() {
                return getPreferredSize();
            }
    
            @Override
            public Dimension getMaximumSize() {
                return getPreferredSize();
            }
    
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

thank you for taking your time to read this message. I hope you are
thank you for taking the time and interest reading this and hopefully helping me
First off, let me thank you if you're taking the time to look at
Hi there and thank you for taking the time to look into this. I'm
First off thank you in advance for taking time to help me with this,
Thank you for taking the time to read this. I looked over Stack Overflow
Thank you for taking the time to read this and I will appreciate every
Thank you for taking the time to read this and I will appreciate every
First of all thanks for taking your time to help me with this. Sorry
thank you for taking your time Problem I have a problem with searching records

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.