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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:10:01+00:00 2026-06-11T09:10:01+00:00

I have this code that loads 5 images and puts them into the frame

  • 0

I have this code that loads 5 images and puts them into the frame using FlowLayout:

public class Main
{
    private static final int verticalGap=50;
    private static final int horizontalGap=30;
    private static final int width= 800;
    private static final int height= 800;
    public static void main(String[] args)
    {
        FlowLayout layout=new FlowLayout(FlowLayout.LEADING,horizontalGap,verticalGap);
        JButton button= new JButton("Discard");
        ImagePanel[] panels= new ImagePanel[5];
        Deck deck= new Deck();
        JFrame frame= new JFrame("Poker");
        frame.setSize(width, height);
        frame.setLayout(layout);
        frame.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        deck.mix();
        for(int i=0; i<5; i++)
        {
            panels[i]= new ImagePanel();
            panels[i].setImage(deck.getCard(i));
            frame.getContentPane().add(panels[i]);
        }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

The code loads 5 cards and spot them correctly.
But the problem is that now I want to place a button to the frame.This button should be placed approximately in the center of the screen, but if I add it to the pane, the button is placed near the other panels, using the horizontal gap that the flow layout has set.
How do I place it in an absolute position without altering the position of the panels (so I want 5 panels to be added using flow layout, and one button to be added in an absolute position).

  • 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-11T09:10:03+00:00Added an answer on June 11, 2026 at 9:10 am

    You can’t mix absolute-layout with a LayoutManager.

    In this case:

    1. I would wrap the 5 cards in a separate JPanel (using your FlowLayout).
    2. I would put that panel in the content pane.
    3. and use another JPanel for the button which I would add to the SOUTH of the content pane (by default the content pane uses BorderLayout). In this panel, I would simply use a FlowLayout with the alignement set to CENTER.
    4. Move your GUI init code to the EDT by using SwingUtilities.invokeLater (always run GUI things on the EDT!)

    Here is the code corresponding to that solution (but I could not test it because I don’t have your other classes).

    import java.awt.BorderLayout;
    import java.awt.ComponentOrientation;
    import java.awt.FlowLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Main {
        private static final int verticalGap = 50;
        private static final int horizontalGap = 30;
        private static final int width = 800;
        private static final int height = 800;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Main().initUI();
                }
            });
        }
    
        private void initUI() {
            FlowLayout layout = new FlowLayout(FlowLayout.LEADING, horizontalGap, verticalGap);
            JButton button = new JButton("Discard");
            ImagePanel[] panels = new ImagePanel[5];
            Deck deck = new Deck();
            JFrame frame = new JFrame("Poker");
            frame.setSize(width, height);
            frame.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
            deck.mix();
            JPanel deckPanel = new JPanel(layout);
            for (int i = 0; i < 5; i++) {
                panels[i] = new ImagePanel();
                panels[i].setImage(deck.getCard(i));
                deckPanel.add(panels[i]);
            }
            frame.getContentPane().add(deckPanel);
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            buttonPanel.add(button);
            frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();// Sets the frame size to its preferred size.
            // You can also call setSize() instead
            frame.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that runs but never stops. class A { public static
I have an app that loads multiple thumbnail images into a UIScrollVIew. This is
I have this code that suppose to place the marker by the latlng public
I have this code that fetches some text from a page using BeautifulSoup soup=
i have this bit of nasty code :P that updates a div using AJAX,
I have this AS3 slideshow code, that loads an XML with a list of
I have this code that I've edited: http://pastebin.com/vrqHek6S I've put in comments where I
I have this code that reads from XML file. It gets five strings (groupId,
I have this code that where I would normally use one line: if (tableView
I have this code that works in a unit test but doesn't work when

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.