2013-05-30 53 views
5

Tôi có một ứng dụng RCP/SWT trong đó tôi đang cố gắng xây dựng một khung nhìn từ các vật liệu tổng hợp hiện có. Một là một hỗn hợp FillLayout, cái kia sử dụng GridLayout.Tôi có thể kết hợp SWT GridLayout và FillLayout

Tôi muốn kết thúc với chế độ xem trong đó hỗn hợp GridLayout được xếp ở bên trái của hỗn hợp FillLayout (nghĩ biểu ngữ dọc) và khoảng 10 phần trăm chiều rộng của toàn bộ chế độ xem, với Hỗn hợp FillLayout bao gồm 90% khác.

Tôi không chắc liệu SWT có thể kết hợp bố cục hay không, nhưng tôi đang nghĩ một thứ gì đó giống như GridLayout có hai cột. Cột một sẽ chứa tiện ích GridLayout và cột hai sẽ chứa hỗn hợp FillLayout. Điều này có thể được thực hiện trong SWT? Nếu vậy, điều này trông giống như mã khôn ngoan?

Thanks-

Trả lời

7

Bắt đầu thiết lập một FormLayout để tổng hợp bên ngoài của bạn. Đặt hai vật liệu tổng hợp khác vào trong nó, đặt thông tin FormData để định vị chúng theo ý bạn. Sau đó, đặt hai bố cục của hỗn hợp đó (Grid và Fill, như bạn đã nói).

Dưới đây là một số mã để bắt đầu. Có một hình ảnh sau khi nó hiển thị những gì nó tạo ra. Bạn cũng có thể kiểm tra chế độ xem SWT của Eclipse của Eclipse.

Shell shell = new Shell(); 

FillLayout fillLayout = new FillLayout(); 
fillLayout.marginHeight = 5; 
fillLayout.marginWidth = 5; 
shell.setLayout(fillLayout); 

Composite outer = new Composite(shell, SWT.BORDER); 
outer.setBackground(new Color(null, 207, 255, 206)); // Green 

FormLayout formLayout = new FormLayout(); 
formLayout.marginHeight = 5; 
formLayout.marginWidth = 5; 
formLayout.spacing = 5; 
outer.setLayout(formLayout); 

Composite innerLeft = new Composite(outer, SWT.BORDER); 
innerLeft.setLayout(new GridLayout()); 
innerLeft.setBackground(new Color(null, 232, 223, 255)); // Blue 

FormData fData = new FormData(); 
fData.top = new FormAttachment(0); 
fData.left = new FormAttachment(0); 
fData.right = new FormAttachment(10); // Locks on 10% of the view 
fData.bottom = new FormAttachment(100); 
innerLeft.setLayoutData(fData); 

Composite innerRight = new Composite(outer, SWT.BORDER); 
innerRight.setLayout(fillLayout); 
innerRight.setBackground(new Color(null, 255, 235, 223)); // Orange 

fData = new FormData(); 
fData.top = new FormAttachment(0); 
fData.left = new FormAttachment(innerLeft); 
fData.right = new FormAttachment(100); 
fData.bottom = new FormAttachment(100); 
innerRight.setLayoutData(fData); 

shell.open(); 

Code output

2

Bạn có thể sử dụng đoạn mã sau như một điểm khởi đầu:

public static void main(String[] args) 
{ 
    Display display = new Display(); 
    Shell shell = new Shell(display); 
    shell.setLayout(new GridLayout(1, false)); 

    SashForm form = new SashForm(shell, SWT.HORIZONTAL); 
    form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 

    Composite left = new Composite(form, SWT.BORDER); 
    left.setLayout(new GridLayout(3, true)); 
    left.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 

    for(int i = 0; i < 9; i++) 
    { 
     Button button = new Button(left, SWT.PUSH); 
     button.setText("Button " + i); 
     button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true)); 
    } 

    final Composite right = new Composite(form, SWT.BORDER); 
    right.setLayout(new GridLayout(1, true)); 
    right.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 

    Button fillButton = new Button(right, SWT.PUSH); 
    fillButton.setText("Fill"); 
    fillButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 

    /* Set the width to 80% and 20% */ 
    form.setWeights(new int[] {4, 1}); 

    shell.setSize(400, 400); 
    shell.open(); 
    while (!shell.isDisposed()) 
    { 
     if (!display.readAndDispatch()) 
      display.sleep(); 
    } 
    display.dispose(); 
} 

Nó trông như thế này:

enter image description here


Đó là cơ bản một SashForm với hai phần. Phần bên trái là GridLayout với ba cột và phần bên phải là GridLayout với một cột. Không cần phải trộn Layout s.

Tỷ lệ được thiết lập với form.setWeights(new int[] {4, 1});