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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:39:10+00:00 2026-05-12T16:39:10+00:00

I am writing a simple application in Java that does some particle simulation on

  • 0

I am writing a simple application in Java that does some particle simulation on a bunch of sheep (don’t ask). For this I want a window with a JPanel for graphics (which will be resizable with a simple combobox that contains some standard resolutions) and some other elements like buttons to start and pause the simulation etc.

My question: I’m using the JFrame.pack method to pack everything nicely together using a borderLayout. But for some reason the JPanel is packed wrong, it seems like the packing ignores it, so the window is resized to fit the size of only the two buttons that I have now. What am I doing wrong?

This is the code so far (bit of a newbie, so no comments on my dumbness if there is any ;)):

public class Window {
 public Sheepness sheepness;

 public ButtonPanel buttonPanel;
 public PaintPanel paintPanel;
 public JFrame frame;

 public Window(Sheepness sheepness, int width, int height) {
  this.sheepness = sheepness;

  frame = new JFrame("Sheepness simulation");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //frame.setSize(width, height);

  BorderLayout frameLayout = new BorderLayout();
  JPanel background = new JPanel(frameLayout);
  background.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

  buttonPanel = new ButtonPanel(this);
  background.add(BorderLayout.SOUTH, buttonPanel.buttonBox);

  paintPanel = new PaintPanel(this);
  paintPanel.setSize(600, 600);
  background.add(BorderLayout.CENTER, paintPanel);

  frame.getContentPane().add(background);
  frame.pack();
  frame.setResizable(false);
  frame.setVisible(true);
 }
}

public class PaintPanel extends JPanel {
 public Window window;

 public PaintPanel(Window window) {
  this.window = window;
 }

 @Override
 public void paintComponent(Graphics g) {
  g.setColor(Color.blue);
  g.fillRect(0, 0, 300, 200);
 }
}

public class ButtonPanel {
 public Window window;
 public Box buttonBox;

 public JButton startButton;
 public JButton resetButton;

 public ButtonPanel(Window window) {
  this.window = window;

  buttonBox = new Box(BoxLayout.X_AXIS);

  startButton = new JButton("Start");
  startButton.addActionListener(new startButtonListener());
  buttonBox.add(startButton);

  resetButton = new JButton("Reset");
  resetButton.addActionListener(new resetButtonListener());
  buttonBox.add(resetButton);
 }
}
  • 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-12T16:39:10+00:00Added an answer on May 12, 2026 at 4:39 pm

    Try:

    paintPanel.setPreferredSize(600, 600);
    

    As Window.pack() sizes to the preferred sizes of its subcomponents and JPanel gets its preferred size from its child components (in your case there are none).

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

Sidebar

Ask A Question

Stats

  • Questions 197k
  • Answers 197k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you using a tool like fiddler to see what… May 12, 2026 at 7:21 pm
  • Editorial Team
    Editorial Team added an answer use array_unique($array) in PHP you can also iterate over array… May 12, 2026 at 7:21 pm
  • Editorial Team
    Editorial Team added an answer Django doesn't currently support multiple databases. However you can just… May 12, 2026 at 7:21 pm

Related Questions

I am learning OpenGL and just started getting into lighting. I enable lighting and
I have a large tree of Java Objects in my Desktop Application and am
My small utility application asks the user for an output directory via a GUI
So my 2009 new years resolution is to learn Java. I recently acquired Java
I was hoping to implement a simple XMPP server in Java. What I need

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.