【问题标题】:row layout not changing controls when resizing调整大小时行布局不更改控件
【发布时间】:2013-01-22 10:22:50
【问题描述】:

在调整外壳大小时,我在 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();

    }

}

【问题讨论】:

  • 你说的“它不下来”是什么意思?可以发个截图吗?
  • 我连续三个按钮。因此,当外壳调整大小时,它应该换行。这意味着当外壳调整到靠近第三个按钮的大小时,第三个按钮应该出现在第二行。清楚吗。

标签: java layout swt


【解决方案1】:

您的复合材料没有使用外壳调整大小,因为您没有指定它这样做。

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

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

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

【讨论】:

  • 我想知道我的代码的问题。我无法追踪它。我没有尝试过你的方式。
  • 那么问题是您没有告诉comp 的父级需要填充可用空间。所以它不会自动调整大小来这样做。这就是GridData 的“告诉”。 true 两个参数都告诉comp 需要不断抓取水平和垂直的多余空间。
  • 但它适用于第二个示例。你能看到吗?
  • 可能是因为您使用了在第一个示例中注释掉的shell.setLayout(new FillLayout());。
  • 完美!!!它工作正常。只是那个问题。谢谢..它工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-12
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
相关资源
最近更新 更多