首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >调整大小时不更改控件的行布局

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

Stack Overflow用户
提问于 2013-01-22 18:22:50
回答 1查看 1K关注 0票数 0

在调整外壳的大小时,我在RowLayout中遇到了一些问题。我创建了这个组合,并将该组合的布局设置为RowLayout。在复合体中添加了三个按钮。现在当我调整外壳的大小时,如果没有足够的空间放置第三个按钮,它应该会掉下来。但它不会降下来。有人能说出代码中哪里出了问题吗?请看我下面的代码。

代码语言:javascript
复制
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();

    }

}

请看下面代码中的区别。我也想以同样的方式工作。上面的代码应该和下面的代码一样工作。

代码语言:javascript
复制
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();

    }

}
EN

Stack Overflow用户

回答已采纳

发布于 2013-01-22 18:30:23

您的组合不会随壳调整大小,因为您没有指定它这样做。

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

代码语言:javascript
复制
comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

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

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14456278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档