2012-02-12 29 views
6

Có cách nào để thay đổi thứ tự của các phần tử được tạo trong Bố cục hàng, Tôi muốn hiển thị nó trong các phần tử đầu tiên được hiển thị lần đầu tiên. Ví dụ nếu tôi tạo element1, sau đó element2 element3, element4Thay đổi thứ tự của phần tử trong RowLayout SWT Java

Tôi muốn nhìn thấy cách bố trí như element4 element3 element2 element1

đó có nghĩa là các yếu tố kéo dài được tạo ra sẽ là yếu tố đầu tiên mà sẽ được hiển thị trong trình bao.

Có cách nào dễ dàng để làm việc với bố cục hàng và thực hiện.

Tôi muốn thay đổi ví dụ sau để hiển thị Nút99 Nút98 Nút97 Nút96 Nút95 Nút94 ……………………………….

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

Cảm ơn trước.

Trả lời

0

Có vẻ như không có thuộc tính nào để thiết lập sao cho các phần tử của RowLayout sẽ được đặt theo thứ tự ngược lại. Vì vậy, bạn có thể giữ các phần tử trong một mảng đảo ngược và sau đó thêm tất cả trong vòng lặp hoặc cho ví dụ cụ thể này, chỉ cần thay đổi điều kiện bắt đầu và kết thúc của vòng lặp for để nó sẽ giống như Button99 Button98 ...: D

5

FillLayout, RowLayoutGridLayout sử dụng thứ tự z của các nút điều khiển in order to determine the ordering of the controls. (Vì ba bố cục đó không cho phép điều khiển chồng chéo nhau, thứ tự z sẽ bị bỏ qua.)

Thứ tự z mặc định dựa trên tạo - do đó điều khiển mặc định theo thứ tự bạn đã thêm chúng với cha mẹ của họ.

Bạn có thể thay đổi thứ tự z (và do đó, thay đổi thứ tự mà các tiện ích con được vẽ) bằng cách sử dụng các phương pháp Control.moveAbove()Control.moveBelow().

+0

Cảm ơn bạn rất nhiều vì đã trả lời. – user1205079

0

Nếu bạn sử dụng một trong những cách bố trí của swt, sau đó:

đã: item01, item02, item03 chèn item04 trước item02: 1. tạo 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(); 
Các vấn đề liên quan