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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:51:16+00:00 2026-05-21T23:51:16+00:00

I’m developing Snake game using Java. Board’s (where all action takes it place) width

  • 0

I’m developing Snake game using Java. Board’s (where all action takes it place) width and height should be fixed (640 pixels * 480 pixels).

Structure:

  • Main class that extends from JFrame class,
  • Board class that extends from JPanel class;

In Main class I have something like…

setSize( 1024, 768 );

…and in Board class I have…

setSize( BOARDS_WIDTH, BOARDS_HEIGHT );

Problem is that main and only windows seems to be like 1024 * 768, but panel inside it – not like 640 * 480. I have setBackground() as well and background is filled more than 640 * 480.

Is there any way to make Board’s width and height like 640 * 480, but main windows width and height – as much as Board’s width and height are correct?

Right now it looks like this…

Width and height are ~1000 * ~750, not 640 * 480.

Edit:

All works… almost.

Main class:

add( new Board(), BorderLayout.CENTER );
pack();
setResizable( false );
setLocationRelativeTo( null );
setVisible( true );
setDefaultCloseOperation( EXIT_ON_CLOSE );
setTitle( "Snake (by daGrevis)" );

Board class:

setPreferredSize( new Dimension( 640, 480 ) );

Gray background is 642 * 482! Like there would be border that wraps all by 1 pixel!

  • 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-21T23:51:16+00:00Added an answer on May 21, 2026 at 11:51 pm

    please, something went xxx*x, and that’s not true at all, check that

    JButton Size - java.awt.Dimension[width=400,height=40]
    JPanel Size - java.awt.Dimension[width=640,height=480]
    JFrame Size - java.awt.Dimension[width=646,height=505]
    

    code (basic stuff from Trail: Creating a GUI With JFC/Swing , and yet I still satisfied that that would be outdated )

    EDIT: forget setDefaultCloseOperation()

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class FrameSize {
    
        private JFrame frm = new JFrame();
        private JPanel pnl = new JPanel();
        private JButton btn = new JButton("Get ScreenSize for JComponents");
    
        public FrameSize() {
            btn.setPreferredSize(new Dimension(400, 40));
            btn.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("JButton Size - " + btn.getSize());
                    System.out.println("JPanel Size - " + pnl.getSize());
                    System.out.println("JFrame Size - " + frm.getSize());
                }
            });
            pnl.setPreferredSize(new Dimension(640, 480));
            pnl.add(btn, BorderLayout.SOUTH);
            frm.add(pnl, BorderLayout.CENTER);
            frm.setLocation(150, 100);
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // EDIT
            frm.setResizable(false);
            frm.pack();
            frm.setVisible(true);
        }
    
        public static void main(String[] args) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    FrameSize fS = new FrameSize();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.