SWT 初步试水

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SWT 初步试水相关的知识,希望对你有一定的参考价值。

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 初步试水的主要内容,如果未能解决你的问题,请参考以下文章

SWT和Swing代码的区别

php初步

Eclipse SWT开发教程以及一个连连看游戏的代码实现下载

SWT标签和Clabel无法正确呈现'&'字符

笔画宽度变换 (SWT) 实现 (Python)

SWT(JFace) Wizard(Eclipse插件编程必备)