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

  • Home
  • SEARCH
  • 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 8969071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:32:31+00:00 2026-06-15T17:32:31+00:00

From previous experience, everyone will say Don’t use those setXxxSize methods! Use a layout

  • 0

From previous experience, everyone will say “Don’t use those setXxxSize methods! Use a layout manager!” However, what should I do in this case?

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

public class Game {
    public static void main(String[] args) {
        new Game();
    }
    public Game() {
        JFrame f = new JFrame("Hi this is a game");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new GamePanel());
        f.pack(); 
        f.setVisible(true);
    }
    private class GamePanel extends JPanel {
        private GamePanel() {
            setPreferredSize(600, 600);
        }
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D)g;
            //paint stuff
        }
    }
}

This is just some example code I threw together real quick. What should I do when I need to make the game area, say, 600 by 600? Is it okay to use setPreferredSize here? I don’t know of any layout manager that can just take a single component and give it a size, and even if there is, isn’t it a lot easier to just use this instead of having to use an entire layout manager?

By the way, f.setSize(600, 600) doesn’t work here because it takes into account the frame decorations (title bar, border things).

So the question is if I only have one component that needs to be a specific size, what do I do?

  • 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-15T17:32:32+00:00Added an answer on June 15, 2026 at 5:32 pm

    Why do this and not just setPreferredSize()?

    As Game is a JPanel that is

    • Used solely for drawing,

    • Contains a default layout but no components and

    • Has a standard UI delegate,

    the difference is negligible. There are still reasons to override getPreferredSize()

    • The intent is clear to other developers; demos and prototypes, like zombies, never go away.

    • Any one or all of those conditions may change as the class evolves.

    • It’s an idiomatic reminder not to fall into this trap.

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

Sidebar

Related Questions

From previous experience I had been under the impression that it's perfectly legal (though
From my previous question about timezones , I decided to use the Ruby gem
From my previous experience, I've almost always had problems with linking files with my
Migrating from PHP to EJB , I have previous experience of PHP , C++
https://i.stack.imgur.com/ZfB0c.jpg Hi everyone, I've been learning MySQL (exclusively, no experience with PHP yet) from
From previous post, I learnt that for there are two ways, at least, to
Note: This question has broadened in scope from previous revisions. I have tried to
Is there a way to cache pages from previous visitors and then share that
I am trying to figure out this error from previous 3 hours. By searching,
Given <field name=frame.time_delta_displayed showname=Time delta from previous displayed frame: 0.000008000 seconds size=0 pos=0 show=0.000008000/>

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.