0

我正在完全用手工代码设计我的第一个用户界面(不使用任何“拖放”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);


}

}

4

1 回答 1

0

我发现了问题。

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

于 2013-09-16T21:55:41.067 回答