1

如何在选项卡文件夹中设置合成的宽度和高度?

PS:setSize而且setBounds不起作用!

4

1 回答 1

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

在此处输入图像描述

After Pressing the Button

在此处输入图像描述

虽然有捕捉!只要您调整应用程序的大小,复合材料就会恢复到原来的大小


如果您不使用任何事件来设置大小, 也可能会出现问题(可能是 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解决方案不是一个非常优雅的解决方案,但它适用于我的开发环境。

于 2012-07-27T14:20:57.047 回答