gpt4 book ai didi

java - Java SWT GridLayout 可以从右到左布局小部件吗?

转载 作者:行者123 更新时间:2023-12-01 23:03:54 25 4
gpt4 key购买 nike

来自API document GridLayout 的:“小部件从左到右按列布置,...”,我想知道是否有某种方法可以从右到左布置小部件?或者,当我只有一列应用 GridLayout 时,我可以从下到上布局小部件吗? (抱歉我的英语不好!)

最佳答案

这里的神奇词是 SWT.RIGHT_TO_LEFT 。但是,它不会应用于 Layout,而是应用于包含您的小部件的 Composite

这里是一些示例代码:

public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(2, true));
shell.setText("ExpandBar Example");

Composite ltr = new Composite(shell, SWT.LEFT_TO_RIGHT);
ltr.setLayout(new GridLayout(3, true));
ltr.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Composite rtl = new Composite(shell, SWT.RIGHT_TO_LEFT);
rtl.setLayout(new GridLayout(3, true));
rtl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

for(int i = 0; i < 10; i++)
{
new Button(ltr, SWT.PUSH).setText("Button " + i);
new Button(rtl, SWT.PUSH).setText("Button " + i);
}

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

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

这就是它的样子:

enter image description here

<小时/>

要同时从右到左和从下到上,您只需使用 moveAbove/moveBelownull 作为参数:

public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(2, true));
shell.setText("StackOverflow");

Composite ltr = new Composite(shell, SWT.LEFT_TO_RIGHT);
ltr.setLayout(new GridLayout(3, true));
ltr.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Composite rtl = new Composite(shell, SWT.LEFT_TO_RIGHT);
rtl.setLayout(new GridLayout(3, true));
rtl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

int nrOfItems = 10;
int nrOfColumns = ((GridLayout) rtl.getLayout()).numColumns;
int fillerItems = nrOfColumns - nrOfItems % nrOfColumns;

for (int i = 0; i < nrOfItems; i++)
{
new Button(ltr, SWT.PUSH).setText("Button " + i);
new Button(rtl, SWT.PUSH).setText("Button " + i);
}

for(int i = 0; i < fillerItems; i++)
new Label(rtl, SWT.NONE);

Control[] children = rtl.getChildren();
for (int i = 0; i < children.length; i++)
{
children[i].moveAbove(null);
}

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

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

看起来像这样:

enter image description here

关于java - Java SWT GridLayout 可以从右到左布局小部件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23079030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com