【问题标题】:Components a "bit" out of position with GridBagLayout - Java Swing使用 GridBagLayout - Java Swing 组件“有点”错位
【发布时间】:2013-09-16 20:30:10
【问题描述】:

我正在完全用手工代码设计我的第一个用户界面(不使用任何“拖放”IDE)。

因此,我在使用一种表单时遇到了问题。 正如您在我的布局屏幕截图中看到的那样:

红色的 textFields 未与 From 单选按钮对齐。蓝色的文本字段与标签“To:”不对齐也有同样的问题。它们看起来好像没有按一行对齐,但我在“发件人:”单选按钮和“收件人:”标签中为 gridy 使用了相同的值。

绿色的也稍微不对齐。

我已经检查了我的代码,但我找不到错误在哪里。有人可以帮我正确对齐它们吗?

编辑

public void layoutComponents()
{
    //add to the layout (what is above )
    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints(); //class that specifies where goes what we want



    ////////// Insert and Align Labels and Fields //////////


    gc.weightx = 1; // how much space it takes relatively to other cells
    gc.weighty = 1; // how much space it takes relatively to other cells
    gc.fill = GridBagConstraints.NONE;

    //specify to wich side it will stick to
    gc.anchor = GridBagConstraints.LINE_START; // stick to the right hand side

    Insets fivePixels = new Insets(0, 0, 0, 5);// adds 5 pixels of spcae to the right
    Insets zeroPixels = new Insets(0, 0, 0, 0);

    gc.gridx = 0;
    gc.gridy = 0; //y increase downwards
    gc.insets = fivePixels;
    add(dataSourceBoldLabel, gc);

    gc.gridy = 1; //second row
    add(sourceTableLabel, gc);

    gc.gridy = 2;
    add(processDataPeriodLabel, gc);

    gc.gridy = 5;
    add(resultsBoldLabel, gc);

    gc.gridy = 6;
    add(writeResultsToNewTableRadio,gc);

    gc.gridy = 7;
    add(writeResultsToExistingTableRadio, gc);

    gc.gridy = 8;
    add(processDataOptionsLabel, gc);

    gc.gridy = 9;
    add(accessSessionTimeThresholdLabelLabel, gc);

    gc.gridy = 10;
    add(transitionTimeThresholdLabelLabel, gc);







    ///////////     Second Column  X = 1     ////////////


    gc.insets = zeroPixels;
    gc.gridx = 1;


    gc.gridy = 1;
    add(sourceTables, gc);

    gc.gridy = 2;
    add(allRadio, gc);

    gc.gridy = 3;
    add(fromRadio, gc);

    gc.gridy = 4;
    gc.anchor = GridBagConstraints.LINE_END;
    add(toLabel, gc);

    gc.anchor = GridBagConstraints.NORTHWEST;
    gc.gridy = 6;
    add(newTableField, gc);

    gc.gridy = 7;
    add(resultsTables, gc);

    gc.gridy = 9;
    add(accessSessionTimeThresholdField,gc);

    gc.gridy = 10;
    add(transitionTimeThresholdField, gc);

    gc.anchor = GridBagConstraints.NORTHWEST; //push the buttom to the top
    gc.gridy = 15;
    add(start, gc);



    ////////////////// Third Column    X = 2 //////////////////
    gc.gridx = 2;
    gc.gridy = 4;
    add(fromDateField, gc);

    gc.gridy = 5;
    add(toDateField, gc);





    ///////////////// 4th Column    X = 3 /////////////////////
    gc.gridx = 3;
    gc.gridy = 4;
    add(fromTimeField, gc);

    gc.gridy = 5;
    add(toTimeField, gc);


}

}

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,请发帖 SSCCE。 2) 对于那个 GUI,我很想使用 GroupLayout - 但它从来没有打算手动编码..
  • 我已编辑我的帖子以发布 SSCCE。
  • 三等舱有近 250 个 LOC。虽然我可能仍然认为 250 行对于 SSCCE 来说足够“短”,但很多人不会。但除此之外,它需要 3 次复制/粘贴代码才能看到它的工作原理。 SSCCE总是一个源文件。任何包含 2 个或更多源文件的代码都只是“不是 SSCCE”。我建议你a)不要扩展框架,只使用一个实例。这会将框架代码放在main() 中并摆脱一个类。然后.. b) 将 main() 放在 GBL 源代码的末尾。

标签: java swing layout-manager gridbaglayout


【解决方案1】:

我找到了问题。

它在 gc.anchor = GridBagConstraints.X 上,其中 X 是方向。当我应该保持方向不变或将列和行都匹配正确的方向时,我使用了不同的方向。

【讨论】:

    猜你喜欢
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 2018-07-30
    相关资源
    最近更新 更多