Вопрос

I'm setting a panel, and I'm using a GridBagLayout. However, when I'm setting up the panel, the labels are not aranging the way I want (I want them to read like a statsheet) The rotationLabels should be showing on the far left, but they are showing in the middle. Am I missing something in my code?

GridBagConstraints rotationGB = new GridBagConstraints();
            rotationGB.insets = new Insets(5,5,5,5);
            rotationGB.anchor = GridBagConstraints.LINE_START;
            rotationPanel = new JPanel(new GridBagLayout());
            rotationLabels = new JLabel[countStarters(team)];
            eraArray = new JLabel[countStarters(team)];
            winArray = new JLabel[countStarters(team)];
            lossArray = new JLabel[countStarters(team)];
            savesArray = new JLabel[countStarters(team)];
            bsArray = new JLabel[countStarters(team)];
            resetXY(0,0);
            for(int i = 0; i < countStarters(team); i++){
                labelX = 0;
                final int n = i;
                rotationLabels[i] = new JLabel(team.rotation.get(i).getName());
                    //Label setup code
                //Mouse listener stuff

                addGCComp(rotationLabels[i], rotationPanel, rotationGB, labelX, labelY, labelSize);
                labelX += 125;
                eraArray[i] = new JLabel(pitcherF.format(team.rotation.get(i).getERA()));
                eraArray[i].setForeground(Color.BLACK);
                addGCComp(eraArray[i], rotationPanel, rotationGB, labelX, labelY, labelSize);
                labelX += 75;
                DecimalFormat df = new DecimalFormat("0");
                winArray[i] = new JLabel(df.format(team.rotation.get(i).getPitcherWins()));
                winArray[i].setForeground(Color.BLACK);
                addGCComp(winArray[i], rotationPanel, rotationGB, labelX, labelY, labelSize);
                labelX += 75;
                lossArray[i] = new JLabel(df.format(team.rotation.get(i).getPitcherLosses()));
                lossArray[i].setForeground(Color.BLACK);
                addGCComp(lossArray[i], rotationPanel, rotationGB, labelX, labelY, labelSize);
                labelX += 75;
                savesArray[i] = new JLabel(df.format(team.rotation.get(i).getPitcherSaves()));
                savesArray[i].setForeground(Color.BLACK);
                addGCComp(savesArray[i], rotationPanel, rotationGB, labelX, labelY, labelSize);
                labelX += 75;
                bsArray[i] = new JLabel(df.format(team.rotation.get(i).getPitcherBlownSaves()));
                bsArray[i].setForeground(Color.BLACK);
                addGCComp(bsArray[i], rotationPanel, rotationGB, labelX, labelY, labelSize);
                labelX += 75;

                labelY += 25;
            }

What am I missing? I tried GridBagConstraints.NORTHWEST for an anchor, but that didn't seem to be working. Any help would be appreciated!

Cheers, David

Это было полезно?

Решение

You need to create the Gridbaglayout and then add the Components to be layout by it.

Like this

GridBagLayout layout = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();

layout.addLayoutComponent(Component, cons);

then add the layout to the Container.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top