从GridLayout的API document:“窗口小部件从左到右排列,......”,我想知道是否有从右到左布置小部件的方法?或者,当我只有一个应用GridLayout的列时,我可以从下到上布置小部件吗? (抱歉我的英语不好!)
答案 0 :(得分:2)
这里的神奇词是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();
}
这就是它的样子:
要实现从右到左和从下到上,您只需使用moveAbove
/ moveBelow
并null
作为参数:
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();
}
看起来像这样:
答案 1 :(得分:1)
通过引用SWT Snippets,我找到了一种从下到上垂直布局小部件的方法(在单列的情况下),只需使用控件的moveAbove() method(在这种情况下按钮是生成,Button是Control的子类。
以下是一个例子:
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
shell.setText("GridLayout Example");
for(int i = 0; i < 10; i++)
{
Button btn = new Button(shell, SWT.PUSH);
btn.setText("Button " + i);
btn.moveAbove(null);
}
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
无法发布图片来显示结果,因为我是新来的,声誉不够,抱歉。
答案 2 :(得分:0)
根据您要添加的数据的表示方式,您可以在创建组件之前反转循环,然后再将它们添加到面板中?假设你有一个数据数组,而不是for (i = 0; i < n; i++)
,你可以只for (i = n-1; i >= 0; i--)
创建一个“反向”效果?