1. 程式人生 > >FormLayout實現自適應視窗

FormLayout實現自適應視窗

package test;

import java.awt.Dimension;
import java.awt.Toolkit;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

public class MainWindow {

    protected Shell shell;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            MainWindow window = new MainWindow();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
//        shell.setFullScreen(true);
        shell.setMaximized(true);
        shell.setText("SWT Application");
        int topNumerator =70;
        FormLayout layout =new FormLayout();
        shell.setLayout (layout);
        //頂部
        Composite cTop=new Composite(shell,SWT.NONE);
        FormData fdTop=new FormData();
        fdTop.top =new FormAttachment(0);
        fdTop.right=new FormAttachment(100);
        fdTop.bottom=new FormAttachment(topNumerator);
        fdTop.left=new FormAttachment(0);
        cTop.setLayoutData(fdTop);
       
        //頂部子佈局
        cTop.setLayout(new FillLayout());
        List lLeft=new List(cTop,SWT.MULTI);
        List lcenter=new List(cTop,SWT.MULTI);
        List lright=new List(cTop,SWT.MULTI);
        //底部
        Composite cBottom=new Composite(shell,SWT.BORDER);
        FormData fdBottom=new FormData();
        fdBottom.top=new FormAttachment(topNumerator);
        fdBottom.right=new FormAttachment(100);
        fdBottom.bottom=new FormAttachment(100);
        fdBottom.left=new FormAttachment(0);
        cBottom.setLayoutData(fdBottom);
        //底部子佈局
        cBottom.setLayout(new RowLayout());
        Button b1=new Button(cBottom,SWT.PUSH);
        b1.setText("button1");
        Button b2=new Button(cBottom,SWT.PUSH);
        b2.setText("button2");
        Button b3=new Button(cBottom,SWT.PUSH);
        b3.setText("button3");
        Button b4=new Button(cBottom,SWT.PUSH);
        b4.setText("button4");
        Button b5=new Button(cBottom,SWT.PUSH);
        b5.setText("button5");
    }

}