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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:34:10+00:00 2026-06-17T04:34:10+00:00

I have been working on a small project that is supposed to simulate a

  • 0

I have been working on a small project that is supposed to simulate a gambling game. Unfortunately, I ran into some odd issues while working with BoxLayout. To the best of my knowledge, LayoutManagers usually honor any component’s preferred size. However, in the below code, BoxLayout does not.

Here is my code thus far:

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



public class Main 
{
    public static void main(String[] args)
    {
      JFrame.setDefaultLookAndFeelDecorated(true);
      JFrame frame = new JFrame("Suit-Up");
      frame.setContentPane(makeGUI());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(900,450);
      frame.setLocationRelativeTo(null);
      frame.setResizable(false);
      frame.setVisible(true);
    }

    public static JPanel makeGUI()
    {
      JPanel main = new JPanel();
      main.setMinimumSize(new Dimension(900,450));
      main.setBackground(Color.red);

      JPanel infoPanel = new JPanel();
      infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.LINE_AXIS));
      infoPanel.setPreferredSize(new Dimension(900,60));
      infoPanel.setBackground(Color.green);
      main.add(infoPanel);

      JPanel infoText = new JPanel();
      infoText.setLayout(new BoxLayout(infoText, BoxLayout.PAGE_AXIS));
      infoPanel.add(infoText);

      JPanel moneyText = new JPanel();
      moneyText.setLayout(new BoxLayout(moneyText, BoxLayout.LINE_AXIS));
      infoText.add(moneyText);

      JPanel lastGameText = new JPanel();
      lastGameText.setLayout(new BoxLayout(lastGameText, BoxLayout.LINE_AXIS));
      infoText.add(lastGameText);

      JButton playAgain = new JButton("Play Again ($20)");
      playAgain.setPreferredSize(new Dimension(200,60));
      infoPanel.add(playAgain);

      JButton finish = new JButton("End Session");
      finish.setPreferredSize(new Dimension(200,60));
      infoPanel.add(finish);

      JPanel cardPanel = new JPanel();
      cardPanel.setLayout(new BoxLayout(cardPanel, BoxLayout.LINE_AXIS));
      main.add(cardPanel);

      return main;
    }
}

Despite specifying preferred sizes for both JButtons, they do not change their sizes. I have tried setMaximumSize() and setMinimumSize() as well, but neither had any effect.

Am I overlooking something obvious, or is this a limitation of BoxLayout?

  • 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-17T04:34:11+00:00Added an answer on June 17, 2026 at 4:34 am

    “To the best of my knowledge, LayoutManagers usually honor any component’s preferred size” – That’s actually not true. preferred/min/max size are just “hints” that layout managers MAY use to determine how best to layout there contents. Layout managers are allowed to simply ignore them if they want to.

    From the JavaDocs

    BoxLayout attempts to arrange components at their preferred widths
    (for horizontal layout) or heights (for vertical layout). For a
    horizontal layout, if not all the components are the same height,
    BoxLayout attempts to make all the components as high as the highest
    component. If that’s not possible for a particular component, then
    BoxLayout aligns that component vertically, according to the
    component’s Y alignment. By default, a component has a Y alignment of
    0.5, which means that the vertical center of the component should have the same Y coordinate as the vertical centers of other components with
    0.5 Y alignment.

    Similarly, for a vertical layout, BoxLayout attempts to make all
    components in the column as wide as the widest component. If that
    fails, it aligns them horizontally according to their X alignments.
    For PAGE_AXIS layout, horizontal alignment is done based on the
    leading edge of the component. In other words, an X alignment value of
    0.0 means the left edge of a component if the container’s ComponentOrientation is left to right and it means the right edge of
    the component otherwise.

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

Sidebar

Related Questions

I have a small c++ project I've been working on in XCode, that I'd
I have a small open source OSX project I've been working on that I'd
I have been working around one small project which needs a similar GUI as
I have been working on a small project on recent delived ASP.NET MVC 3
I have been working with a small team on a small project about fiction
I have this small project I have been working on. I have a MySQL
I have been working on a C++ project that uses SDL, and wonder if
Have been working on a small project, and have gotten stuck at a part
I've been working on a small project that consists of a GUI with a
I have a normal VS2008 project that I've been working on for awhile that

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.