2012-02-10 67 views
1

我是一個SWT新手,並且在我的用戶界面看起來像我想要它時遇到了一些麻煩。考慮下面的代碼,從SWT片段拼湊起來:SWT:veritcal分隔符太高;橫向太短;表太窄

Display.setAppName("App Name"); 

    Display display = new Display(); 

    Shell shell = new Shell(display); 
    GridLayout shellLayout = new GridLayout(); 
    shellLayout.numColumns = 1; 
    shell.setLayout(shellLayout); 

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

    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(shell, SWT.SEPARATOR | SWT.HORIZONTAL); 

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

    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(); 

運行時,所產生的顯示具有以下問題:

  1. 垂直隔板爲大約兩倍「高」作爲按鈕。在網格佈局的最上面一行是這樣的:
 
ABC1 ABC2 ABC3 | ABC1 ABC2 ABC3 
       | 

我想獲得的分離是隻按鈕一樣高。

  1. 水平分隔符約爲屏幕寬度的1/10。我怎樣才能讓它擴大到網格中整行的寬度?

  2. 該表的列只與它們包含的文本一樣寬。這會導致表格寬度約爲窗口寬度的1/2。我希望表格的寬度(以及擴展列的寬度)隨着窗口寬度的增加和縮小而變化。任何一點都可以指導我如何實現這個目標?

回答

4

需要告知水平標籤以獲取更多空間;

Label temp = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); 
temp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 

對column.pack()的調用是改變列寬以匹配數據。如果你想將它們設置爲顯式大小,你需要在你的Column對象上實際設置屬性,而不是用新的Column創建它們。獲取所有列以動態調整窗口和表的大小需要爲該表編寫ResizeListener。否則,無論表大小如何,這些列都會保持不變。

+0

修復了水平分隔符。有關如何使垂直標籤只與按鈕一樣高的想法?目前它是兩倍高。 – jph 2012-02-11 17:20:35

1

垂直分隔符的高度可以使用佈局數據進行設置。

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

Label verticalSeparator = new Label(group, SWT.SEPARATOR | SWT.VERTICAL); 
RowData layoutData = new RowData(); 
layoutData.height = button.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; 
verticalSeparator.setLayoutData(layoutData);