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

Java 调整大小时不更改控件的行布局,java,layout,swt,Java,Layout,Swt,在调整外壳大小时,我在RowLayout中遇到一些问题。我创建了该组合,并将布局设置为该组合的RowLayout。在合成中添加了三个按钮。现在,当我调整壳的大小时, 如果没有足够的空间放置第三个按钮,则应将其关闭。但它不会下降。 有人能说出代码中的错误吗?请看下面我的代码 包com.rcp.mytraining.layout.composite; 导入org.eclipse.swt.widgets.Composite; 公共类ChatComposite扩展了Composite{ /** *创建组

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

包com.rcp.mytraining.layout.composite;
导入org.eclipse.swt.widgets.Composite;
公共类ChatComposite扩展了Composite{
/**
*创建组合。
* 
*@param父级
*@param风格
*/
公共聊天室复合(复合父级,int样式){
超级(父母,风格);
复合复合材料=新复合材料(母体,SWT.NONE);
//颜色s=新颜色(显示,新RGB(30,30,30));
颜色s=Display.getDefault().getSystemColor(1);
公司背景;
RowLayout RowLayout=新的RowLayout(SWT.水平);
rowLayout.wrap=true;
rowLayout.pack=false;
组件设置布局(行布局);
按钮btnNewButton=新按钮(组件,SWT.NONE);
设置文本(“新按钮”);
按钮btnNewButton_1=新按钮(组件,SWT.NONE);
Btnewbutton_1.setText(“ss”);
按钮btnNewButton_2=新按钮(组件,SWT.NONE);
btnNewButton_2.setText(“新按钮”);
comp.pack();
}
@凌驾
受保护的void checkSubclass(){
//禁用阻止SWT组件子类化的检查
}
公共静态void main(字符串[]args){
显示=新显示();
外壳=新外壳(显示);
//外壳。设置尺寸(200200);
//setLayout(新的FillLayout());
ChatComposite ChatComposite=新的ChatComposite(外壳,SWT.NONE);
shell.pack();
shell.open();
//chatComposite.pack();
而(!shell.isDisposed()){
如果(!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();

    }

}

您的组合没有使用shell调整大小,因为您没有指定它这样做

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

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

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

您所说的“它没有下降”是什么意思?你能贴个截图吗?我有三个按钮。因此,当外壳调整大小时,它应该换行。这意味着当外壳在第三个按钮附近调整大小时,第三个按钮应该位于第二行。明白了吗?我想知道我的代码有什么问题。我找不到它。我没有试着用你的方式。问题是你没有告诉
comp
的家长需要填补可用空间。因此,它不会自动调整大小以执行此操作。这就是
GridData
所说的“告诉”。这两个
true
参数都告诉我们
comp
需要继续获取水平和垂直多余的空间。但是对于第二个示例,它可以正常工作。你看到了吗?可能是因为你使用了
shell.setLayout(newfilllayout())您在第一个示例中注释掉了它。完美!!!它工作得很好。只是这个问题。谢谢。它很好用。