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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:01:12+00:00 2026-05-22T13:01:12+00:00

I want to have a 3×3 grid layout structured like this: _ _ _

  • 0

I want to have a 3×3 grid layout structured like this:

 _ _ _ _ _ _ _ _ _ _ _ _
|        Label         |
| Button Button Button |
|       TextArea       |
|_ _ _ _ _ _ _ _ _ _ _ |

But the layout is broken.

    public static void main(String[] args){
    JFrame frame = new JFrame("title");
    frame.setSize(400,500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    GridLayout layout = new GridLayout(3,3);

    JPanel pan0 = new JPanel();
    JPanel pan1 = new JPanel();
    JPanel pan2 = new JPanel();

    pan0.setLayout(layout);
    GridBagConstraints c = new GridBagConstraints();

    JLabel title = new JLabel("title");
    title.setFont(new Font("Serif", Font.BOLD, 40));
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 3;
    pan0.add(title, c);

    JButton b1 = new JButton("button1");
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    pan0.add(b1, c);

    JButton b2 = new JButton("button2");
    c.gridx = 1;
    c.gridy = 2;
    c.gridwidth = 1;
    pan0.add(b2, c);

    JButton b3 = new JButton("button3");
    c.gridx = 2;
    c.gridy = 3;
    c.gridwidth = 1;
    pan0.add(b3, c);    

    TextArea text1 = new TextArea(15,40);
    c.gridx = 2;
    c.gridy = 0;
    c.gridwidth = 3;
    pan0.add(text1, c);

    frame.add(pan0);
    frame.pack();

  }
}
  • 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-22T13:01:13+00:00Added an answer on May 22, 2026 at 1:01 pm

    You’re confusing GridLayout with GridBagLayout, but I’d use a BoxLayout for the vertical positioning myself, and then perhaps a GridLayout for the buttons. For e.g.,

    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import javax.swing.*;
    
    public class Foo003 {
       private static final int BTN_COUNT = 3;
       private static final int VERT_GAP = 10;
       private static final int EB_GAP = 5;
       private static final float TITLE_SIZE = 36f;
       private static final String TITLE_TEXT = "This is my Title";
    
       private static void createAndShowUI() {
    
          JLabel titleLabel = new JLabel(TITLE_TEXT, SwingConstants.CENTER);
          titleLabel.setFont(titleLabel.getFont().deriveFont(TITLE_SIZE));
          JPanel titlePanel = new JPanel();
          titlePanel.add(titleLabel);
    
          JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
          for (int i = 0; i < BTN_COUNT; i++) {
             JButton btn = new JButton("Button " + (i + 1));
             buttonPanel.add(btn);
          }
    
          JTextArea textArea = new JTextArea(20, 30);
    
          JPanel mainPanel = new JPanel();
          mainPanel.setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP));
          mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
          mainPanel.add(titlePanel);
          mainPanel.add(Box.createVerticalStrut(VERT_GAP));
          mainPanel.add(buttonPanel);
          mainPanel.add(Box.createVerticalStrut(VERT_GAP));
          mainPanel.add(new JScrollPane(textArea));
    
          JFrame frame = new JFrame("Foo003");
          frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want have a query with a column that is a hardcoded value not
I want have a field in mysql where the Current time must be auto-generated,
I am creating website (football, soccer) in ASP.NET MVC3 and I want have users
What I want to do is count the number of employees a publisher employs
I have two tables. Student and Job. I need to search students who possess
I want to use the information from XML response produced by using YQL for
i have C++ program exporting log files as HTML table and I wanted to
I have django model: class UserProfile(models.Model): user = models.OneToOneField(User) #other fields When I create
I have been seeing alot of code examples online of retrieving cookie data during
I have just started learning coding and PHP so I have been looking at

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.