2012-07-25 76 views

回答

3

因为你没有说过你在何时,在哪里以及如何设置复合尺寸,所以我假设你正在设置一个事件的大小,以及setSize适合我。

我在Win7,eclipse 4.2和JDK 1.6_b30上。参见示例代码:

import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.SelectionAdapter; 
import org.eclipse.swt.events.SelectionEvent; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Button; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.TabFolder; 
import org.eclipse.swt.widgets.TabItem; 
import org.eclipse.swt.widgets.Text; 


public class Test 
{ 
    public static void main(String[] args) throws Exception 
    { 
     Display display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setLayout(new GridLayout(1, false)); 

     TabFolder folder = new TabFolder(shell, SWT.TOP); 
     folder.setLayout(new GridLayout()); 
     folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 


     TabItem item = new TabItem(folder, SWT.NONE); 
     item.setText("Tab 1"); 

     final Composite composite = new Composite(folder, SWT.BORDER); 
     composite.setLayout(new GridLayout()); 
     composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); 
     Text text = new Text(composite, SWT.BORDER); 
     text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); 
     text.setText("Tab 1"); 
     Button b = new Button(composite, SWT.PUSH); 
     b.setText("Press"); 
     b.addSelectionListener(new SelectionAdapter() { 
      @Override 
      public void widgetSelected(SelectionEvent e) { 
       composite.setSize(23, 43); 
      } 
     }); 

     item.setControl(composite); 

     shell.setSize(300, 200); 
     shell.open(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) 
       display.sleep(); 
     } 
     display.dispose(); 
    } 
} 

Before Pressing the Button

enter image description here

After Pressing the Button

enter image description here

虽然有赶上!只要您调整应用程序的大小,复合材料就会回到的原始尺寸


也有 可能是一个问题,如果你不使用设置大小的任何事件(可能在SWT标签文件夹了一些bug或功能)。非侵入性的 hack(不需要任何用户交互)是使用 PaintListener。请参见下面的代码:

import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.PaintEvent; 
import org.eclipse.swt.events.PaintListener; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.TabFolder; 
import org.eclipse.swt.widgets.TabItem; 
import org.eclipse.swt.widgets.Text; 


public class Test 
{ 
    public static void main(String[] args) throws Exception 
    { 
     Display display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setLayout(new GridLayout(1, false)); 

     final TabFolder folder = new TabFolder(shell, SWT.TOP); 
     folder.setLayout(new GridLayout()); 
     folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 


     TabItem item = new TabItem(folder, SWT.NONE); 
     item.setText("Tab 1"); 

     final Composite composite = new Composite(folder, SWT.BORDER); 
     composite.setLayout(new GridLayout()); 
     composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); 
     Text text = new Text(composite, SWT.BORDER); 
     text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); 
     text.setText("Tab 1"); 


     folder.addPaintListener(new PaintListener() { 
      public void paintControl(PaintEvent e) { 
       composite.setSize(23, 43); 
      } 
     }); 


     item.setControl(composite); 

     shell.setSize(300, 200); 
     shell.open(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) 
       display.sleep(); 
     } 
     display.dispose(); 
    } 
} 

PaintListener解决方案是不是一个非常优雅的解决方案,但它适合我的发展环境。