Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java swt布局建议:带下表的按钮_Java_User Interface_Swt - Fatal编程技术网

Java swt布局建议:带下表的按钮

Java swt布局建议:带下表的按钮,java,user-interface,swt,Java,User Interface,Swt,我试图设计一个SWT UI,作为SWT和UI编程的新手。根据SWT文档中的片段,我编写了以下内容: Display.setAppName("App Name"); Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout()); Group group = new Group(shell, SWT.DEFAULT)

我试图设计一个SWT UI,作为SWT和UI编程的新手。根据SWT文档中的片段,我编写了以下内容:

    Display.setAppName("App Name");

    Display display = new Display();

    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());

    Group group = new Group(shell, SWT.DEFAULT);
    group.setLayout(new GridLayout());

    for (int i = 0; i < 3; i++)
        new Button(group, SWT.PUSH).setText("ABC" + i);

    new Label(group, SWT.SEPARATOR | SWT.VERTICAL);

    for (int i = 0; i < 3; i++)
        new Button(group, SWT.PUSH).setText("ABC" + i);

    new Label(group, SWT.SEPARATOR | SWT.HORIZONTAL);

    Table table = new Table(group, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
    table.setLinesVisible(true);
    table.setHeaderVisible(true);
    GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
    table.setLayoutData(data);

    for (String title : new String[] { "A", "B", "C", "D", "E", "F", "G" })
        new TableColumn(table, SWT.NONE).setText(title);

    for (int i = 0; i < 128; i++) {
        TableItem item = new TableItem (table, SWT.NONE);
        item.setText(0, "123");
        item.setText(1, "234");
        item.setText(2, "345");
        item.setText(3, "456");
        item.setText(4, "567");
        item.setText(5, "678");
        item.setText(6, "789");
    }

    for (TableColumn column : table.getColumns())
        column.pack();

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

    while (shell.isDisposed() != true)
        if (display.readAndDispatch() != true)
            display.sleep();

    display.dispose();
}
Display.setAppName(“应用程序名称”);
显示=新显示();
外壳=新外壳(显示);
setLayout(新的GridLayout());
组组=新组(shell,SWT.DEFAULT);
setLayout(新的GridLayout());
对于(int i=0;i<3;i++)
新按钮(组,SWT.PUSH).SETEXT(“ABC”+i);
新标签(组,SWT.SEPARATOR | SWT.VERTICAL);
对于(int i=0;i<3;i++)
新按钮(组,SWT.PUSH).SETEXT(“ABC”+i);
新标签(组,SWT.SEPARATOR | SWT.HORIZONTAL);
表格=新表格(组,SWT.MULTI | SWT.BORDER | SWT.FULL|U选择);
表.setLinesVisible(真);
表.setheadervible(true);
GridData数据=新的GridData(SWT.FILL,SWT.FILL,true,true);
表.setLayoutData(数据);
for(字符串标题:新字符串[]{“A”、“B”、“C”、“D”、“E”、“F”、“G”})
新的TableColumn(table,SWT.NONE).setText(title);
对于(int i=0;i<128;i++){
TableItem项目=新的TableItem(表,SWT.NONE);
第条第(0,“123”)款;
第条第(1,“234”)款;
项目.第(2,“345”)款;
第2项第(3,“456”)款;
第2项第(4,“567”)款;
第6项第(5,“678”)款;
第6项,“789”);
}
for(TableColumn列:table.getColumns())
column.pack();
shell.pack();
shell.open();
while(shell.isDisposed()!=true)
if(display.readAndDispatch()!=true)
display.sleep();
display.dispose();
}
这会导致如下结果:

ABC1 ABC2 ABC3 | ABC1 ABC2 ABC3 --- Table ABC1 ABC2 ABC3 | ABC1 ABC2 ABC3 --- 桌子 如果将组上的布局更改为RowLayout(),但不将表的GridData()更改为RowData(),则会出现类强制转换异常。如果将其更改为RowData(),则会得到如下结果:

ABC1 ABC2 ABC3 | ABC1 ABC2 ABC3 ---- Table ABC1 ABC2 ABC3 | ABC1 ABC2 ABC3----表格 我想要的是:

ABC1 ABC2 ABC3 | ABC1 ABC2 ABC3 ------------------------------- Table ABC1 ABC2 ABC3 | ABC1 ABC2 ABC3 ------------------------------- 桌子
有什么建议吗?

我不擅长手工编写布局管理器,所以我使用了WindowBuilder Pro,一种eclipse功能。您只需将所有内容拖放到需要的位置,然后将它们粘在一起。

将shell布局设置为宽度为2的gridLayout。然后在制作表格时,将gridData水平跨度设置为2

大部分来自内存,未经测试

Shell myshell = new Shell(display, SWT.NONE);
GridLayout twoColumn = new GridLayout();
twoColumn.numColumns = 2;
myshell.setLayout(gl);

Group g1 = new Group(myshell, SWT.DEFAULT);
Group g2 = new Group(myshell, SWT.DEFAULT);

GridData twospan = new GridData(SWT.FILL, SWT.FILL,true,true);
twospan.horizontalSpan = 2;

Table t1 = new Table(myshell, SWT.DEFAULT);
t1.setLayoutData(twospan);

这应该设置您的GridLayout有两列,所以您添加g1并将其置于左上角,g1并将其置于右上角。然后指定t1将占用两列,因此添加它时将占用整个第二行。现在它还忽略了您的分隔符标签。

成功!某种程度上。它看起来仍然不像我想要的那样,但它更近了。将在新帖子中提出其他问题。