gpt4 book ai didi

Java Gridbagconstraints gridy 问题

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

我正在尝试创建一个表单,但在使用 GridBagConstraints 时遇到困难。当我设置gridy时,它似乎不起作用。这是我的代码:

thisWindow = new GridBagConstraints();
thisWindow.insets = new Insets(5, 5, 5, 5);
thisWindow.weightx = 1.0;
thisWindow.weighty = 1.0;
thisWindow.gridheight = 4;
thisWindow.anchor = GridBagConstraints.NORTHWEST;
//set elements
thisWindow.gridx = 0;
thisWindow.gridy = 0;
charScreen.add(nameLbl, thisWindow);
thisWindow.gridx = 1;
thisWindow.gridy = 0;
charScreen.add(charNameCmb, thisWindow);
thisWindow.gridx = 2;
thisWindow.gridy = 0;
charScreen.add(raceLbl, thisWindow);
thisWindow.gridx = 3;
thisWindow.gridy = 0;
charScreen.add(raceCmb, thisWindow);
thisWindow.gridx = 4;
thisWindow.gridy = 0;
charScreen.add(genderLbl, thisWindow);
thisWindow.gridx = 5;
thisWindow.gridy = 0;
charScreen.add(genderCmb, thisWindow);
//This should be on a new line.
thisWindow.gridx = 0;
thisWindow.gridy = 1;
charScreen.add(levelLbl, thisWindow);
thisWindow.gridx = 1;
thisWindow.gridy = 1;
charScreen.add(levelSpn, thisWindow);
thisWindow.gridx = 2;
thisWindow.gridy = 1;
charScreen.add(charClassLbl, thisWindow);
thisWindow.gridx = 3;
thisWindow.gridy = 1;
charScreen.add(charClassCmb, thisWindow);
thisWindow.gridx = 4;
thisWindow.gridy = 1;
charScreen.add(deityLbl, thisWindow);
thisWindow.gridx = 5;
thisWindow.gridy = 1;
charScreen.add(deityCmb, thisWindow);
thisWindow.gridx = 6;
thisWindow.gridy = 1;
charScreen.add(homelandLbl, thisWindow);
thisWindow.gridx = 7;
thisWindow.gridy = 1;
charScreen.add(homelandTxt, thisWindow);
//This should be on a third line.
thisWindow.gridx = 0;
thisWindow.gridy = 2;
charScreen.add(sizeLbl, thisWindow);
thisWindow.gridx = 1;
thisWindow.gridy = 2;
charScreen.add(sizeTxt, thisWindow);
charScreen.setVisible(true);

我尝试过使用各种 anchor ,但没有任何帮助。我遇到的问题是,应该位于第二行和第三行的两个部分都与第一行重叠。非常感谢任何对此的建议。

最佳答案

你的 gridHeight 把你搞乱了。

更改:

thisWindow.gridheight = 4;

thisWindow.gridheight = 1;

请注意,我创建了自己的 MCVE 来测试这一点:

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

public class TestCharScreen {

private static GridBagConstraints thisWindow;
private static JPanel charScreen = new JPanel(new GridBagLayout());
private static JLabel nameLbl = new JLabel("nameLbl");
private static JLabel charNameCmb = new JLabel("charNameCmb");
private static JLabel raceLbl = new JLabel("raceLbl");
private static JLabel raceCmb = new JLabel("raceCmb");
private static JLabel genderLbl = new JLabel("genderLbl");
private static JLabel genderCmb = new JLabel("genderCmb");
private static JLabel levelLbl = new JLabel("levelLbl");
private static JLabel levelSpn = new JLabel("levelSpn");
private static JLabel charClassLbl = new JLabel("charClassLbl");
private static JLabel charClassCmb = new JLabel("charClassCmb");
private static JLabel deityLbl = new JLabel("deityLbl");
private static JLabel deityCmb = new JLabel("deityCmb");
private static JLabel homelandLbl = new JLabel("homelandLbl");
private static JLabel homelandTxt = new JLabel("homelandTxt");
private static JLabel sizeLbl = new JLabel("sizeLbl");
private static JLabel sizeTxt = new JLabel("sizeTxt");


public static void main(String[] args) {


thisWindow = new GridBagConstraints();
thisWindow.insets = new Insets(5, 5, 5, 5);
thisWindow.weightx = 1.0;
thisWindow.weighty = 1.0;

// *****
thisWindow.gridheight = 4; // 4? *****

thisWindow.anchor = GridBagConstraints.NORTHWEST;
//set elements
thisWindow.gridx = 0;
thisWindow.gridy = 0;
charScreen.add(nameLbl, thisWindow);
thisWindow.gridx = 1;
thisWindow.gridy = 0;
charScreen.add(charNameCmb, thisWindow);
thisWindow.gridx = 2;
thisWindow.gridy = 0;
charScreen.add(raceLbl, thisWindow);
thisWindow.gridx = 3;
thisWindow.gridy = 0;
charScreen.add(raceCmb, thisWindow);
thisWindow.gridx = 4;
thisWindow.gridy = 0;
charScreen.add(genderLbl, thisWindow);
thisWindow.gridx = 5;
thisWindow.gridy = 0;
charScreen.add(genderCmb, thisWindow);
//This should be on a new line.
thisWindow.gridx = 0;
thisWindow.gridy = 1;
charScreen.add(levelLbl , thisWindow);
thisWindow.gridx = 1;
thisWindow.gridy = 1;
charScreen.add(levelSpn, thisWindow);
thisWindow.gridx = 2;
thisWindow.gridy = 1;
charScreen.add(charClassLbl, thisWindow);
thisWindow.gridx = 3;
thisWindow.gridy = 1;
charScreen.add(charClassCmb, thisWindow);
thisWindow.gridx = 4;
thisWindow.gridy = 1;
charScreen.add(deityLbl, thisWindow);
thisWindow.gridx = 5;
thisWindow.gridy = 1;
charScreen.add(deityCmb, thisWindow);
thisWindow.gridx = 6;
thisWindow.gridy = 1;
charScreen.add(homelandLbl, thisWindow);
thisWindow.gridx = 7;
thisWindow.gridy = 1;
charScreen.add(homelandTxt, thisWindow);
//This should be on a third line.
thisWindow.gridx = 0;
thisWindow.gridy = 2;
charScreen.add(sizeLbl, thisWindow);
thisWindow.gridx = 1;
thisWindow.gridy = 2;
charScreen.add(sizeTxt, thisWindow);

charScreen.setBorder(BorderFactory.createTitledBorder("charScreen"));
// charScreen.setVisible(true);

JOptionPane.showMessageDialog(nameLbl, charScreen);
}
}

关于Java Gridbagconstraints gridy 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27204399/

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