3

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();
            }
        });
    }
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Punkrockie
  • 97
  • 1
  • 5

1 Answers1

3

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks. Yeah the book I'm using is A Guide to Programming in Java and just talks about the rows and columns. The problem I'm working on even says to use a GridLayout but the picture of what it needs to look like, makes it look like it has a margin between the button and the cell. Also, to clarify, it wasn't my teacher but the teacher's assistant, the reason I even brought it up was to let you guys know I was trying and not just dumping my question here without doing any research first. Anyway, thanks a ton and sorry that my question was so lame :( – Punkrockie Feb 27 '12 at 18:42
  • Setting a marging is not a problem, using the hgap and vgap arguments of the constructor. But all cells share the same hgap and vgap. – JB Nizet Feb 27 '12 at 19:13