Search code examples
javaswingjpaneljtextfield2d-games

JPanel additional column on GUI upon run?


Been working on a school project in which my group and I are making a Wheel of Fortune game with a GUI and all. When making the panel for the puzzle to be displayed, I'm running into the problem of an additional column being created. It's also possible that it's just empty space, but regardless, I would like to know how to get rid of it. Here is the code for the letterBoard class which runs through the wheelGUI class.

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

 public class letterBoard extends JPanel
                            implements ActionListener                   
 {
 private JTextField[] fields = new JTextField[TEXT_FIELD_COUNT];
 private Box[] boxes = new Box[SUIT_COUNT];
 private static int TEXT_FIELD_COUNT = 14;
 private static int SUIT_COUNT = 1;
 Color datGreen=new Color(0, 180, 100);

public letterBoard()
{
    setBackground(Color.GRAY);
    JPanel panel = new JPanel(new GridLayout(4,1));
    panel.setBackground(datGreen);
    for(int t=0; t<4; t++)
    {
        for (int i =0; i < boxes.length; i++)
        {
            boxes[i] = Box.createHorizontalBox();
            for (int j=0; j< TEXT_FIELD_COUNT/SUIT_COUNT; j++)
            {
                int index = i * (TEXT_FIELD_COUNT/SUIT_COUNT) + j;
                fields[index] = new JTextField("   ");
                fields[index].setEditable(false);
                fields[index].setPreferredSize(new Dimension(50, 50));
                fields[index].setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
                panel.add(fields[index]);
                panel.add(boxes[i]);
                panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK,3),"WHEEL OF FORTUNE"));
            }
        }
    }
    Box b1 = Box.createVerticalBox();
    b1.add(panel);
    b1.add(Box.createVerticalStrut(5));
    b1.add(Box.createHorizontalStrut(5));
    add(b1);
}
public void actionPerformed(ActionEvent e) 
{

}
}

Any help in identifying the problem would be great. Here's a picture of what it looks like upon compiling. The green bar on the right is the problem. Thanks!


Solution

  • Maybe I just miss the point, but I don't see the need for all the Box properties...

    I simply modified the GridLayout to layout 14 columns as a priority over the rows and removed all the Box stuff...

    enter image description here

    public LetterBoard() {
        setBackground(Color.GRAY);
        JPanel panel = new JPanel(new GridLayout(0, 14));
        panel.setBackground(datGreen);
        for (int t = 0; t < 4; t++) {
            for (int i = 0; i < boxes.length; i++) {
                for (int j = 0; j < TEXT_FIELD_COUNT / SUIT_COUNT; j++) {
                    int index = i * (TEXT_FIELD_COUNT / SUIT_COUNT) + j;
                    fields[index] = new JTextField("   ");
                    fields[index].setEditable(false);
                    fields[index].setPreferredSize(new Dimension(50, 50));
                    fields[index].setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
                    panel.add(fields[index]);
                }
            }
        }
        panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK, 3), "WHEEL OF FORTUNE"));
        add(panel);
    }
    

    All the Box stuff was adding a extra "column" at the end of each row...