- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个表单,但在使用 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/
我正在尝试创建一个设置 JLabels 的程序,使用 GridBagLayout。 我想创建一个按钮,按下该按钮会删除某个 gridY 位置的所有内容。我该怎么做呢。谢谢。 最佳答案 简单。当您将这些
我正在尝试创建一个表单,但在使用 GridBagConstraints 时遇到困难。当我设置gridy时,它似乎不起作用。这是我的代码: thisWindow = new GridBagConstra
我在制作的游戏中无法正确布置某些组件。我的 GUI 由带有 BorderLayout 的外部 JFrame 组成,在 BorderLayout 的东侧我有一个带有 GridBagLayout 的 JP
我正在尝试使用 gridx 和 gridy 约束来定位我的按钮。但它们不起作用!如果我更改 gridx 和 gridy 变量,什么也不会发生。如果我将 fill to GridBagConstrain
我正在尝试使用 gridx 和 gridy 约束来创建如下所示的界面(忽略“下一步”按钮)。前两个 JLabel-JTextField 对工作正常,但我不确定我为其余 10 个字段设置了什么 grid
我有一个带有 gridbaglayout 布局的 jpanel,其中我有几个 jtextfields、几个 jlabels、几个动态添加的 jbuttons。因此我不知道他们的具体命令,因此不能使用
我有一个带有 gridbaglayout 布局的 jpanel,其中有几个 jtextfields、几个 jlabels、几个动态添加的 jbuttons。因此我无法知道它们的具体顺序,因此无法使用
我是一名优秀的程序员,十分优秀!