调整大小时行布局不更改控件

时间:2013-01-22 10:22:50

标签: java layout swt

在调整shell大小时,我在RowLayout中遇到了一些问题。我创建了合成并将布局设置为该合成的RowLayout。在复合材料中添加了三个按钮。现在,当我调整外壳大小时, 如果第三个按钮没有足够的空间,它应该会降下来。但它并没有下降。 任何人都可以告诉代码中的错误吗?请参阅下面的代码。

package com.rcp.mytraining.layout.composite;

import org.eclipse.swt.widgets.Composite;

public class ChatComposite extends Composite {

    /**
     * Create the composite.
     * 
     * @param parent
     * @param style
     */
    public ChatComposite(Composite parent, int style) {
        super(parent, style);

        Composite comp = new Composite(parent, SWT.NONE);
        // Color s = new Color(display, new RGB(30,30,30));

        Color s = Display.getDefault().getSystemColor(1);

        comp.setBackground(s);

        RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);

        rowLayout.wrap = true;
        rowLayout.pack = false;     

        comp.setLayout(rowLayout);

        Button btnNewButton = new Button(comp, SWT.NONE);
        btnNewButton.setText("New Button");

        Button btnNewButton_1 = new Button(comp, SWT.NONE);
        btnNewButton_1.setText("ss");

        Button btnNewButton_2 = new Button(comp, SWT.NONE);
        btnNewButton_2.setText("New Button");
        comp.pack();
    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }

    public static void main(String[] args) {

        Display display = new Display();

        Shell shell = new Shell(display);

        // shell.setSize(200, 200);

        // shell.setLayout(new FillLayout());

        ChatComposite chatComposite = new ChatComposite(shell, SWT.NONE);


        shell.pack();
        shell.open();

        // chatComposite.pack();

        while (!shell.isDisposed()) {

            if (!display.readAndDispatch()) {

                display.sleep();
            }

        }
        // display.dispose();

    }

}

请参阅以下代码中的差异。我想以同样的方式工作。上面的代码应该与下面的代码一样工作

package com.rcp.mytraining.layout.composite;

import org.eclipse.swt.widgets.Composite;

public class ChatComposite extends Composite {

    /**
     * Create the composite.
     * 
     * @param parent
     * @param style
     */
    public ChatComposite(Composite parent, int style) {
        super(parent, style);

        /*
         * RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
         * 
         * // rowLayout.wrap = true; //rowLayout.pack = true;
         * setLayout(rowLayout);
         * 
         * Button btnNewButton = new Button(this, SWT.NONE);
         * btnNewButton.setText("New Button");
         * 
         * Button btnNewButton_1 = new Button(this, SWT.NONE);
         * btnNewButton_1.setText("New Button");
         * 
         * Button btnNewButton_2 = new Button(this, SWT.NONE);
         * btnNewButton_2.setText("New Button");
         */

        pack();

    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }

    public static void main(String[] args) {

        Display display = new Display();

        Shell shell = new Shell(display);

        // shell.setSize(200, 200);

        shell.setLayout(new FillLayout());

        Composite comp = new Composite(shell, SWT.NONE);
        // Color s = new Color(display, new RGB(30,30,30));

        Color s = Display.getDefault().getSystemColor(1);

        comp.setBackground(s);

        RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);

        rowLayout.wrap = true;
        // rowLayout.pack = true;
        comp.setLayout(rowLayout);

        Button btnNewButton = new Button(comp, SWT.NONE);
        btnNewButton.setText("New Button");

        Button btnNewButton_1 = new Button(comp, SWT.NONE);
        btnNewButton_1.setText("ss");

        Button btnNewButton_2 = new Button(comp, SWT.NONE);
        btnNewButton_2.setText("New Button");
        comp.pack();

        // ChatComposite chatComposite = new ChatComposite(shell, SWT.NONE);

        shell.pack();
        shell.open();

        // chatComposite.pack();

        while (!shell.isDisposed()) {

            if (!display.readAndDispatch()) {

                display.sleep();
            }

        }
        // display.dispose();

    }

}

1 个答案:

答案 0 :(得分:1)

您的复合材料未使用shell进行调整,因为您没有指定它。

GridData对象添加到其布局数据中,如下所示:

comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

注意:出于调试目的,您可以将comp的背景设置为红色(或其他一些华丽的颜色)并查看差异。