gpt4 book ai didi

java - GridBagConstraints 无法正常工作?需要从面板的最左侧开始

转载 作者:行者123 更新时间:2023-12-01 13:37:13 25 4
gpt4 key购买 nike

我正在设置一个面板,并且正在使用 GridBagLayout。但是,当我设置面板时,标签没有按照我想要的方式排列(我希望它们像统计表一样阅读)。旋转标签应该显示在最左侧,但它们显示在中间。我的代码中是否缺少某些内容?

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;
}

我错过了什么?我尝试使用 GridBagConstraints.NORTHWEST 作为 anchor ,但这似乎不起作用。任何帮助将不胜感激!

干杯,大卫

最佳答案

您需要创建Gridbaglayout,然后添加要通过它进行布局的组件。

像这样

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

layout.addLayoutComponent(Component, cons);

然后将布局添加到容器中。

关于java - GridBagConstraints 无法正常工作?需要从面板的最左侧开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21175927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com