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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:19:40+00:00 2026-05-27T01:19:40+00:00

I am trying to place two JButton s next to each other in the

  • 0

I am trying to place two JButtons next to each other in the center of a JFrame, that won’t re-size the buttons when the JFrame is re-sized.

To do this I placed the two buttons in a panel with a FlowLayout, that is then placed in a panel with a center BorderLayout.

However the following code won’t display the chosen panel in the center of the BorderLayout.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class test extends JFrame {

    private JPanel panel1 = new JPanel();
    private JPanel panel2 = new JPanel();
    private JButton button1 = new JButton("one");
    private JButton button2 = new JButton("two");

    public test() {
        panel1.setLayout(new FlowLayout());
        panel1.add(button1);
        panel1.add(button2);

        panel2.setLayout(new BorderLayout());
        panel2.add(panel1, BorderLayout.CENTER);

        this.add(panel2);
    }

    public static void main(String[] args) {
        test frame = new test();
        frame.setVisible(true);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
  • 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-27T01:19:41+00:00Added an answer on May 27, 2026 at 1:19 am

    Set GridBagLayout to panel1.

     panel1.setLayout(new GridBagLayout());
    

    EDIT:

    @trashgod : I have to figure out how the default constraints do it.

    Because of the field GridBagLayout.defaultConstraints :

    This field holds a gridbag constraints instance containing the default
    values, so if a component does not have gridbag constraints associated
    with it, then the component will be assigned a copy of the
    defaultConstraints.

    In normal practice, one must have to create GridBagConstraints object and set fields to specify the constraints on each object.

    To quote the tutorial:

    The preferred approach to set constraints on a
    component is to use the Container.add variant, passing it a
    GridBagConstraints object

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

Sidebar

Related Questions

I am trying to place two divs next to each-other on the same line,
Possible Duplicate: Trying to get tables next to each other horizontal I have two
I'm trying to create two buttons that using the hover event, hide some divs
I am trying to position two <div> s within each other, but it seems
I'm trying to place 3 divs within a larger div such that the center
I am trying to place a TableRow with two Buttons at the bottom of
I'm trying to place a nice border around an image that's 250x250, using only
I'm trying to pull the two components out of a timeval struct and place
I've been all over the place for the last two days trying to find
I'm trying to write a method that will take in two Queues (pre-sorted Linked

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.