当我添加两个Text控件(dUsername和dPassword)时,它们没有正确对齐。请参阅随附的屏幕截图。 添加到组合旁边的两个Text控件应放在各自的标签标题下。但是,我无法找到不正确对齐的原因。如果我能得到关于它的任何指示,那将是非常有帮助的。
public void createControl(Composite Parent) {
Composite container = new Composite(Parent, SWT.NULL);
GridLayout layout = new GridLayout();
container.setLayout(layout);
layout.numColumns = 10;
layout.verticalSpacing = 20;
layout.horizontalSpacing = 15;
GridData gd1 = new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false, 1, 1);
createLabelSection(container,gd1);
getARowForEach(container,gd1);
}
private void getARowForEach(Composite container, GridData grid) {
while (some condition) {
String dString = (String) dIterator.next();
addControls(dString, container, grid);
}
}
private void addControls(String dvString, Composite container, GridData grid) {
Button checkBox = setCheckBoxText(container, null);
String dvLabel = dvString;
getLabelCaption(container,dvLabel);
Combo hostCombo = setComboText(container);
Text dUsername = new Text(container, SWT.NONE);
Text dPassword = new Text(container, SWT.PASSWORD);
}
public Button setCheckBoxText(Composite container, Label label){
Button checkBox = new Button(container, SWT.CHECK);
GridData gd = new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1);
checkBox.setLayoutData(gd);
checkBox.setSelection(true);
return checkBox;
}
public Label getLabelCaption(Composite container, String caption){
Label label = new Label(container, SWT.NONE);
GridData grid = new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1);
label.setLayoutData(grid);
label.setText(caption);
return label;
}
public Combo setComboText(Composite container){
Combo combo = new Combo(container, SWT.READ_ONLY);
GridData gd = new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1);
combo.setLayoutData(gd);
.
.
combo.select(0);
return combo;
}
private void createLabelSection(Composite container,GridData grid ) {
addLabel(container, grid, "some details");
addLabel(container, grid, "some details");
addLabel(container, grid, "some details");
addLabel(container, grid,"username");
addLabel(container, grid, "password");
}
答案 0 :(得分:0)
我想您正在尝试使用控件来模拟表格。
您正在努力使小部件完美对齐。只需根据需要创建一个包含尽可能多列的主容器(在屏幕截图中有5个),然后只需创建一个向该容器添加行的方法。
运行:
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
*
* @author ggrec
*
*/
public class Tester
{
// ==================== 3. Static Methods ====================
public static void main(final String[] args)
{
new Tester();
}
// ==================== 4. Constructors ====================
private Tester()
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
createContents(shell);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
// ==================== 5. Creators ====================
private static void createContents(final Composite parent)
{
final Composite mainContainer = new Composite(parent, SWT.NULL);
mainContainer.setLayout(new GridLayout(5, false));
mainContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
createHeaderRow(mainContainer);
createRow(mainContainer);
createRow(mainContainer);
createRow(mainContainer);
}
/**
* @param parent Must have a grid layout with 5 columns!
*/
private static void createHeaderRow(final Composite parent)
{
new Label(parent, SWT.NULL); // Filler label
final Label nameLabel = new Label(parent, SWT.BORDER);
nameLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
nameLabel.setText("Name");
final Label comboLabel = new Label(parent, SWT.BORDER);
comboLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
comboLabel.setText("Combo");
final Label usernameLabel = new Label(parent, SWT.BORDER);
usernameLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
usernameLabel.setText("Username");
final Label passwordLabel = new Label(parent, SWT.BORDER);
passwordLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
passwordLabel.setText("Password");
}
/**
* @param parent Must have a grid layout with 5 columns!
*/
private static void createRow(final Composite parent/*, final Entity model*/)
{
final Button checkbox = new Button(parent, SWT.CHECK);
checkbox.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
final Label nameLabel = new Label(parent, SWT.NULL);
nameLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
nameLabel.setText("FirstName LastName");
final Combo combo = new Combo(parent, SWT.NULL);
combo.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
final Text usernameWidget = new Text(parent, SWT.NULL);
usernameWidget.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
final Text passwordWidget = new Text(parent, SWT.NULL);
passwordWidget.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
}
}