openplanning

Hướng dẫn và ví dụ Java SWT SashForm

  1. SWT SashForm
  2. Ví dụ SashForm
  3. SashForm với WindowBuilder

1. SWT SashForm

SashForm là class mở rộng từ Composite nó sắp đặt (lays out) các thành phần con thành một hàng hoặc một cột. Và nó đặt một Sash nằm giữa 2 thành phần con cạnh nhau. Người dùng có thể sử dụng chuột để thay đổi vị trí của Sash.
Ví dụ dưới đây là một SashForm nằm ngang với 3 thành phần con, người dùng có thể sử dụng chuột để di chuyển các Sash.
Ví dụ một SashForm thẳng đứng với 4 thành phần con.
Với SashForm là nằm ngang, chiều dài của các thành phần con tỷ lệ thuận với trọng lượng (weight) của chúng. Tương tự khi SashForm là kiểu thẳng đứng, chiều cao của các thành phần con tỷ lệ thuận với trong lượng của chúng.
// Tạo một SashForm nằm ngang:
SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);

// Tạo một SashForm thẳng đứng.
SashForm sashForm = new SashForm(shell, SWT.VERTICAL);

2. Ví dụ SashForm

SashFormDemo.java
package org.o7planning.swt.sashform;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class SashFormDemo {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("SWT SashForm (o7planning.org)");
        FillLayout fillLayout = new FillLayout();
        fillLayout.marginHeight= 5;
        fillLayout.marginWidth = 5;
        shell.setLayout(fillLayout);

        SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);

        Button button1 = new Button(sashForm, SWT.NONE);
        button1.setText("Button 1");
        Button button2 = new Button(sashForm, SWT.NONE);
        button2.setText("Button 2");
        Button button3 = new Button(sashForm, SWT.NONE);
        button3.setText("Button 3");
        sashForm.setWeights(new int[] { 2, 3, 1 });
        sashForm.setSashWidth(10);

        shell.setSize(400, 250);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}
Chạy ví dụ:

3. SashForm với WindowBuilder

Thiết kế SashForm với WindowBuilder.