2013-04-23 53 views
0

我想添加一个复合到Eclipse,当某个复选框被选中时出现。该复合材料将由两部分组成:WARNING_ICON和带有文本的标签。SWT排队ICON_WARNING与标签

我似乎无法得到布局非常正确 - 我想显示的图像和标签并排。这里是我的代码段:

 final Composite warningComposite = new Composite(parent, SWT.NONE); 
     warningComposite.setLayout(new GridLayout(2, false)); 
     warningComposite.setLayoutData(new GridData(0, 0, true, false)); 

     Label l = SWTUtilities.createLabel(composite, "", -1, -1, 1, GridData.BEGINNING); 
     final Image img = PlatformUI.getWorkbench().getDisplay().getSystemImage(SWT.ICON_WARNING); 
     l.setImage(img); 
     l.setLayoutData(new GridData()); 

     l = SWTUtilities.createLabel(composite, "Wizards Gone Wild", -1, -1, 1, GridData.END); 

而且SWTUtilities.createLabel方法:

public static Label createLabel(final Composite parent, final String label, final int width, final int indent, final int span, final int style) { 
     Assert.isNotNull(parent); 
     Assert.isNotNull(label); 
     final Label control = new Label(parent, SWT.WRAP); 
     control.setText(label); 
     if (width > 0 || indent > 0 || span > 0) { 
      final GridData data = new GridData(style); 
      if (width > 0) 
       data.widthHint = width; 
      if (indent > 0) 
       data.verticalIndent = indent; 
      if (span > 1) 
       data.horizontalSpan = span; 
      control.setLayoutData(data); 
     } 
     return control; 
    } 

回答

1

你去那里:

public static void main(String[] args) 
{ 
    Display d = new Display(); 
    final Shell shell = new Shell(d); 
    shell.setLayout(new GridLayout(2, false)); 

    Label image = new Label(shell, SWT.NONE); 
    image.setImage(d.getSystemImage(SWT.ICON_WARNING)); 
    image.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 

    Label text = new Label(shell, SWT.NONE); 
    text.setText("SOME TEXT HERE"); 
    text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true)); 


    shell.pack(); 
    shell.open(); 
    while (!shell.isDisposed()) 
     while (!d.readAndDispatch()) 
      d.sleep(); 
} 

是这样的:

enter image description here


以下是解释:SWT不支持Label内的文本垂直对齐(因为并非所有操作系统都支持它)。解决方案是将标签对齐父级中心,而不是标签内的文本。