2012-02-12 27 views
6

有沒有方法可以更改在行佈局中創建的元素的順序, 我想在首次顯示的第一個元素中顯示它。 例如,如果我創建部件1,然後element2的元素3,元素4更改RowLayout SWT中元素的順序Java

我想看到的佈局 元素4元素3 element2的部件1

這意味着最後一個被創建的內容將是,這將是第一要素顯示在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(); 
      } 
     } 
    } 
} 

在此先感謝。

回答

0

似乎沒有要設置的屬性,以便RowLayout的元素將以相反的順序放置。所以你可以把數組中的元素顛倒過來,然後將它們全部添加到循環中,或者對於這個特定的例子,只需改變for循環的開始和結束條件,以便它像Button99 Button98 ...:D

5

FillLayout,RowLayoutGridLayout使用控件in order to determine the ordering of the controls的z順序。 (由於這三種佈局不允許控件在視覺上彼此重疊,所以z順序將被忽略。)

默認z順序基於創建 - 因此控制默認值爲您添加的順序給他們的父母。

您可以使用Control.moveAbove()Control.moveBelow()方法更改z順序(從而更改小部件繪製的順序)。

+0

非常感謝您的回覆。 – user1205079 2012-02-13 08:20:41

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