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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:00:53+00:00 2026-06-10T05:00:53+00:00

Been struggling for this for some time. My method looks like this: public Frame(){

  • 0

Been struggling for this for some time. My method looks like this:

public Frame(){
            JFrame window = new JFrame();
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setSize(800, 600);

            JPanel panel = new JPanel(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
              gbc.gridheight =3;
              gbc.gridwidth = 3;


            JButton upButt = new JButton();//Buttons.upButton();
              gbc.gridx = 1;
              gbc.gridy = 0;
              panel.add(upButt, gbc);

            JButton downButt = new JButton();
              gbc.gridx = 1;
              gbc.gridy = 2;
              panel.add(downButt, gbc);

            JButton leftButt = new JButton();//Buttons.leftButton();
              gbc.gridx=0;
              gbc.gridy = 1;
              panel.add(leftButt, gbc);

            JButton rightButt = new JButton();//Buttons.rightButton();
              gbc.gridx =2;
              gbc.gridy = 1;
              panel.add(rightButt, gbc);

            window.add(panel);
            window.setVisible(true);

           }

From my understanding – after reading and rereading the Java doc. – this should give me 4 buttons in a cross shape. This however isn’t the case and the buttons are stacked on top of one another in the center of the window.
What am I missing?

  • 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-10T05:00:54+00:00Added an answer on June 10, 2026 at 5:00 am

    Why are you using gridheight and gridwidth of 3? That’s a bit strange to say the least.

    For my money, I’d simplify things and use a simple GridLayout:

    import java.awt.GridLayout;
    import javax.swing.*;
    
    public class Foo003 {
       public static void main(String[] args) {
          JButton upButton = new JButton("Up");
          JButton downButton = new JButton("Down");
          JButton leftButton = new JButton("Left");
          JButton rightButton = new JButton("Right");
          JComponent[][] components = { 
                { new JLabel(), upButton, new JLabel() },
                { leftButton, new JLabel(), rightButton },
                { new JLabel(), downButton, new JLabel() } };
    
          JPanel panel = new JPanel(new GridLayout(components.length,
                components[0].length, 8, 8));
          for (int i = 0; i < components.length; i++) {
             for (int j = 0; j < components[i].length; j++) {
                panel.add(components[i][j]);
             }
          }
    
          int eb = 15;
          panel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
    
          JFrame frame = new JFrame("Grid e.g.");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(panel);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    }
    

    enter image description here

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

Sidebar

Related Questions

Been struggling with this for quite some time so any help is appreciated. Spent
I have been struggling with this for quite some time having been accustomed to
I've been struggling with this error for quite some time: Redis::ProtocolError: Got 'i' as
Hi iv been struggling with this error for some time and really cant figure
I've been struggling with this for some time now. I'm just trying to get
I have been struggling with this for some time now scanning the net for
I've been struggling with this for some time now and I just can't find
I've been struggling with this for quite some time but i just can't figure
I've been struggling with this specific set of tables for some time. There are
I've been struggling through this for some time and I've finally got to the

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.