2014-11-21 82 views
0

当我添加两个Text控件(dUsername和dPassword)时,它们没有正确对齐。请参阅附加的屏幕截图。 添加到组合旁边的两个Text控件应放置在它们各自的标签标题下。但是,我无法找到不正确对齐的原因。如果我能得到任何关于相同的指示,这将是非常有帮助的。SWT文本在网格布局中未正确对齐

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"); 
     } 

enter image description here

回答

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)); 
    } 

} 
+0

文本dashUsername =新文本(容器,SWT.NONE); dashUsername.setLayoutData(new GridData(SWT.FILL,SWT.BEGINNING,false,false,2,1)); \t \t \t \t Text dashPassword = new Text(container,SWT.PASSWORD); dashPassword.setLayoutData(new GridData(SWT.FILL,SWT.BEGINNING,false,false,2,1)); – user3594891 2014-12-03 10:02:59

+0

我试过这个,发现文本与现有代码正确对齐。感谢GGrec的帮助。 – user3594891 2014-12-03 10:06:02