码迷,mamicode.com
首页 > 其他好文 > 详细

SWT 初步试水

时间:2016-06-04 19:19:49      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

package swt;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;

public class FirstWin {

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        Display display = Display.getDefault();
        Shell shell = new Shell();
        shell.setSize(450, 300);
        shell.setText("SWT Application");
        
        TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
        tabFolder.setBounds(0, 0, 434, 261);
        
        TabItem tbtmP_2 = new TabItem(tabFolder, SWT.NONE);
        tbtmP_2.setText("p");
        
        ScrolledComposite scrolledComposite = new ScrolledComposite(tabFolder, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
        tbtmP_2.setControl(scrolledComposite);
        scrolledComposite.setExpandHorizontal(true);
        scrolledComposite.setExpandVertical(true);
        
        Composite composite = new Composite(scrolledComposite, SWT.NONE);
        composite.setLayout(new GridLayout(1, false));
        
        Button btnB = new Button(composite, SWT.NONE);
        btnB.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                new D1(shell, 0).open();
            }
        });
        btnB.setText("b1");
        
        Button btnB_1 = new Button(composite, SWT.NONE);
        btnB_1.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                new S2(display).open();
            }
        });
        btnB_1.setText("b2");
        scrolledComposite.setContent(composite);
        scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        
        TabItem tbtmP = new TabItem(tabFolder, SWT.NONE);
        tbtmP.setText("p1");
        
        TabItem tbtmP_1 = new TabItem(tabFolder, SWT.NONE);
        tbtmP_1.setText("p2");

        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
}
package swt;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class D1 extends Dialog {

    protected Object result;
    protected Shell shell;

    /**
     * Create the dialog.
     * @param parent
     * @param style
     */
    public D1(Shell parent, int style) {
        super(parent, style);
        setText("SWT Dialog");
    }

    /**
     * Open the dialog.
     * @return the result
     */
    public Object open() {
        createContents();
        shell.open();
        shell.layout();
        Display display = getParent().getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        return result;
    }

    /**
     * Create contents of the dialog.
     */
    private void createContents() {
        shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MIN | SWT.MAX);
        shell.setSize(450, 300);
        shell.setText(getText());
        
        Label lblD = new Label(shell, SWT.NONE);
        lblD.setBounds(92, 44, 61, 17);
        lblD.setText("d1");
        
        Button btnConcso = new Button(shell, SWT.NONE);
        btnConcso.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                shell.close();
            }
        });
        btnConcso.setBounds(71, 131, 80, 27);
        btnConcso.setText("Cancel");

    }

}
package swt;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;

public class C1 extends Composite {

    /**
     * Create the composite.
     * @param parent
     * @param style
     */
    public C1(Composite parent, int style) {
        super(parent, style);
        
        Label lblC = new Label(this, SWT.NONE);
        lblC.setBounds(139, 142, 61, 17);
        lblC.setText("c1");

    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }

}
package swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;

public class S2 extends Shell {

    /**
     * Launch the application.
     * @param args
     */
    public  void open(Shell shell) {
        try {
            Display display = getParent().getDisplay();
            shell.open();
            shell.layout();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the shell.
     * @param display
     */
    public S2(Display display) {
        super(display, SWT.SHELL_TRIM);
        
        Label lblS = new Label(this, SWT.NONE);
        lblS.setBounds(57, 55, 61, 17);
        lblS.setText("s2");
        createContents();
    }

    /**
     * Create contents of the shell.
     */
    protected void createContents() {
        setText("SWT Application");
        setSize(450, 300);

    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }
}

 

SWT 初步试水

标签:

原文地址:http://www.cnblogs.com/playboytears/p/5559209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!