我正在手动设计我的第一个用户界面(不使用任何“拖放”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);
}
}
答案 0 :(得分:0)
我发现了问题。
它位于gc.anchor = GridBagConstraints.X,其中X是方向。我应该使用不同的方向,我应该保持它不变或匹配两列和正确的方向线。