在RowLayout SWT Java中更改元素的顺序

时间:2012-02-12 17:29:55

标签: java swt

有没有办法改变在Row布局中创建的元素的顺序, 我想在首先显示的元素中显示它。 例如,如果我创建element1,则element2 element3,element4

我希望将布局视为 element4 element3 element2 element1

表示最后创建的元素将是将在shell中显示的第一个元素。

是否可以轻松使用行布局并执行此操作。

我想将以下示例更改为显示 Button99 Button98 Button97 Button96 Button95 Button94 ....................................

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class TestExample
{
    public static void main(String[] args)
    {
        Display display = Display.getDefault();
        Shell shell = new Shell(display);
        RowLayout rowLayout = new RowLayout();

        shell.setLayout(rowLayout);

        for (int i=0;i<100;i++)
        {
            Button b1 = new Button(shell, SWT.PUSH);
            b1.setText("Button"+i);

        }
        shell.open();
        while (!display.isDisposed())
        {
            if (!display.readAndDispatch())
            {
                display.sleep();
            }
        }
    }
}

先谢谢。

3 个答案:

答案 0 :(得分:5)

FillLayoutRowLayoutGridLayout使用控件in order to determine the ordering of the controls的z顺序。 (由于这三个布局不允许控件在视觉上相互重叠,否则z顺序将被忽略。)

默认的z顺序基于创建 - 因此控制默认为您将它们添加到父级的顺序。

您可以使用Control.moveAbove()Control.moveBelow()方法更改z顺序(从而更改窗口小部件的绘制顺序)。

答案 1 :(得分:0)

似乎没有要设置的属性,因此RowLayout的元素将以相反的顺序放置。因此,您可以将数组中的元素反转,然后将它们全部放入循环中或者针对此特定示例,只需更改for循环的开始和结束条件,使其像Button99 Button98 ...:D

答案 2 :(得分:0)

如果你使用其中一个swt的布局,那么:

已经:item01,item02,item03 在item02之前插入item04: 1.创建item04 2. item04.moveAbove(item02)

Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(640, 480);

shell.setLayout(new FillLayout());

final Composite itemComposite = new Composite(shell, SWT.NONE);
itemComposite.setLayout(new RowLayout());
Label item01 = new Label(itemComposite, SWT.NONE);
item01.setText("item01");
final Label item02 = new Label(itemComposite, SWT.NONE);
item02.setText("item02");
Label item03 = new Label(itemComposite, SWT.NONE);
item03.setText("item03");

Composite buttonComposite = new Composite(shell, SWT.NONE);
buttonComposite.setLayout(new GridLayout());
Button insertItem = new Button(buttonComposite, SWT.NONE);
insertItem.setText("Insert");
insertItem.addListener(SWT.Selection, new Listener() {
    public void handleEvent(Event arg0) {
    Label item04 = new Label(itemComposite, SWT.NONE);
    item04.setText("item04");
    item04.moveAbove(item02);
    itemComposite.layout();
}
});

shell.open();
while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
}
display.dispose();