0
votes

I'm making a program that asks for an integer from the user, it then should display a new window like this depending on how big the integer is.

This would be the layout if the user entered 2:

Enter test score:    JTextField over here
Enter test score:    JTextField over here
Reset             Submit

For some reason I can't make the grid layout in java have only 2 columns per row.

Here is my code:

public void createAverageGUI()
{
    averageFrame = new JFrame("Get All Test Scores");
    txtAllAverages = new JTextField[testCount];
    averageFrame.setLayout(new GridLayout(2, testCount));
    for (int i = 0; i < testCount; i++)
    {
        averageFrame.add(new JLabel("Enter test score: "));
        JTextField field = new JTextField(10);
        averageFrame.add(field);
        txtAllAverages[i] = field;
    }
    averageFrame.add(resetAverages);
    averageFrame.add(submitAverages);
    averageFrame.setSize(400, 600);
    averageFrame.setVisible(true);
}
2
For better help sooner, post an SSCCE. - Andrew Thompson

2 Answers

4
votes

What this GUI really needs is an entirely different layout. Here is how your planned GUI would look.

enter image description here

Here is how it should probably be laid out.

enter image description here

ExcellentGUI.java

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

class ExcellentGUI {

    private JTextField[] txtAllAverages;
    int testCount = 2;

    public void createAverageGUI()
    {
        txtAllAverages = new JTextField[testCount];

        JPanel inputControls = new JPanel(new BorderLayout(5,5));

        JPanel inputControlsLabels = new JPanel(new GridLayout(0,1,3,3));
        JPanel inputControlsFields = new JPanel(new GridLayout(0,1,3,3));
        inputControls.add(inputControlsLabels, BorderLayout.WEST);
        inputControls.add(inputControlsFields, BorderLayout.CENTER);
        for (int i = 0; i < testCount; i++)
        {
            inputControlsLabels.add(new JLabel("Test score: "));
            JTextField field = new JTextField(10);
            inputControlsFields.add(field);
            txtAllAverages[i] = field;
        }

        JPanel controls = new JPanel(new FlowLayout(FlowLayout.CENTER,5,2));

        controls.add(new JButton("Reset"));
        controls.add(new JButton("Submit"));

        JPanel gui = new JPanel(new BorderLayout(10,10));
        gui.setBorder(new TitledBorder("Averages"));
        gui.add(inputControls, BorderLayout.CENTER);
        gui.add(controls, BorderLayout.SOUTH);

        JFrame averageFrame = new JFrame("Get All Test Scores");
        averageFrame.setContentPane(gui);
        // let the components assume the natural size
        // averageFrame.setSize(400, 600);
        averageFrame.pack();
        averageFrame.setLocationByPlatform(true);
        averageFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        averageFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ExcellentGUI().createAverageGUI();
            }
        });
    }
}
2
votes

You understand that this new GridLayout(2, testCount) means two rows and testCount columns, which isn't what I think you want. I strongly urge you to read the GridLayout API to see exactly how this and the other constructors work.

You can simplify things greatly by making your layout new GridLayout(0, 2); which means any number of rows and always 2 columns.