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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:43:08+00:00 2026-05-30T13:43:08+00:00

I’m a beginner and doing my homework. My button is filling up the whole

  • 0

I’m a beginner and doing my homework. My button is filling up the whole cell in the gridLayout. I’ve read about GridBagLayout but my book doesn’t mention anything about it so I think there’s just an error with my current code. The teacher has been trying to help me but we can’t figure it out so I thought I would try here. Any help would be appreciated! Here is what I have:

package riddle;

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

public class Riddle implements ActionListener {
    private final String LABEL_TEXT = "Why did the chicken cross the road?";
    JFrame frame;
    JPanel contentPane;
    JLabel label, label1;
    JButton button;
    JButton button1;
    private static int i;

    public Riddle() {
        /* Create and set up the frame */
        frame = new JFrame(LABEL_TEXT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /* Create a content pane with a GridLayout and empty borders */
        contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(0, 2, 10, 5));


        /* Create and add label that is centered and has empty borders */
        label = new JLabel("Why did the chicken cross the road?");
        label.setAlignmentX(JButton.LEFT_ALIGNMENT);
        label.setBorder(BorderFactory.createEmptyBorder(20, 50, 20, 50));
        contentPane.add(label);

        label1 = new JLabel(" ");
        label1.setAlignmentX(JButton.LEFT_ALIGNMENT);
        label1.setBorder(BorderFactory.createEmptyBorder(20, 50, 20, 50));
        contentPane.add(label1);

        /* Create and add button that is centered */
        button = new JButton("Answer");
        button.setAlignmentX(JButton.RIGHT_ALIGNMENT);
        button.setActionCommand("Show Answer");
        button.addActionListener(this);

        contentPane.add(button);

        /* Add content pane to frame */
        frame.setContentPane(contentPane);

        /* Size and then display the frame */
        frame.pack();
        frame.setVisible(true);
     }

    /** Handle button click action event
     * pre: 
     * post: clicked button shows answer
     */
    public void actionPerformed(ActionEvent event) {
        String eventName = event.getActionCommand();

        if (eventName.equals("Show Answer")) {
            label1.setText("To get to the other side. ");
            button.setText("Answer");
            button.setActionCommand("Answer");
        } 
    }

    /** 
     * Create and show the GUI
     */
    private static void runGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);

        Riddle greeting = new Riddle();
    }



    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                runGUI();
            }
        });
    }
}
  • 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-30T13:43:09+00:00Added an answer on May 30, 2026 at 1:43 pm

    Here’s a quote from the Swing tutorial about GridLayout:

    A GridLayout object places components in a grid of cells. Each
    component takes all the available space within its cell, and each cell
    is exactly the same size.

    If your book doesn’t tell such an important thing, it’s a bad book, and I would advise using the Swing tutorial instead. I’m astonished your teacher hasn’t been able to tell you that.

    If this behavior is not the one you want, choose another layout manager. The Swing tutorial has a guide for all standard ones.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
Seemingly simple, but I cannot find anything relevant on the web. What is 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.