在TabFolder中设置SWT Composite的大小

时间:2012-07-25 10:29:22

标签: swt

如何在标签文件夹中设置复合材料的宽度和高度?

PS:setSizesetBounds不起作用!

1 个答案:

答案 0 :(得分: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标签文件夹中的某些错误或功能),那么可能是一个问题。一个非侵入性的 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解决方案不是一个非常优雅的解决方案,但它适用于我的开发环境。