我正在尝试创建一个表单并且遇到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);
我尝试使用各种锚点,没有任何帮助。我遇到的问题是应该在第二行和第三行的两个部分都与第一行重叠。对此有任何建议都非常感谢。
答案 0 :(得分:3)
你的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);
}
}