SWT/JFACE 第五天,常用組件

來源:互聯網
上載者:User

swt的常用組件button ,text ,combo,list ,還有一些容器類composite ,group,這裡選擇幾個,列寫簡單的用法
不寫解釋了,因為代碼很簡單,而且代碼上的注釋足以說明.
1,combo  和text
package com.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class CTShow {

    private static List list;
    private static Combo combo;
    private static Text text;
    public static void main(String[] args) {
        final Display display = Display.getDefault();
        final Shell shell = new Shell();
        shell.setSize(478, 120);
        shell.setText("combo and text");
        shell.open();
        //構造text
        text = new Text(shell, SWT.BORDER);
        text.setBounds(160, 34, 281, 20);
        //構造combo
        combo = new Combo(shell, SWT.NONE);
        combo.setItems(new String[] {"one", "two", "three", "four"});
        combo.setData("one", "1");
        combo.setData("two", "2");
        combo.setData("three", "3");
        combo.setData("four", "4");
        combo.setBounds(28, 34, 102, 20);
        //添加監聽器
        combo.addSelectionListener(new SelectionAdapter(){
            public void widgetSelected(SelectionEvent e){
                String selText = combo.getText();
                String selValue = (String)combo.getData(selText);
                text.setText("text: " + selText + " value: " + selValue);
            }});
       
        shell.layout();
        //訊息迴圈
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }

}
2,list、 button 、messagebox
package com.test;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

public class LBMShow {

    private static List list;
    /** *//**
     * Launch the application
     * @param args
     */
    public static void main(String[] args) {
        final Display display = Display.getDefault();
        final Shell shell = new Shell();
        shell.setSize(192, 217);
        shell.setText("list and button and messagebox");
        //

        shell.open();
        //構造list
        list = new List(shell, SWT.BORDER|SWT.MULTI|SWT.V_SCROLL);
        list.setItems(new String[] {"語文", "數學", "英語", "物理", "化學"});
        list.setData("化學", "HX");
        list.setData("物理", "WL");
        list.setData("語文", "YW");
        list.setData("數學", "SX");
        list.setData("英語", "YY");
        list.setBounds(29, 25, 100, 75);
        //構造button
        final Button button = new Button(shell, SWT.NONE);
        button.setText("提示");
        button.setBounds(48, 121, 58, 22);
        //添加監聽器
        button.addSelectionListener(new SelectionAdapter(){
            public void widgetSelected(SelectionEvent e){
                for (String str : list.getSelection()) {
                    //messagebox提示
                    MessageDialog.openInformation(null, null, str);
                }
            }
        });
       
        shell.layout();
        //訊息迴圈
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }

}
3,group組
package com.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class GroupShow {
    private static Text text;
    public static void main(String[] args) {
        final Display display = Display.getDefault();
        Shell shell = new Shell();
        shell.setText("group");
        shell.open();
        //構造group
        final Group group = new Group(shell, SWT.NONE);
        group.setText("   組  ");
        group.setBounds(41, 28, 143, 100);
        //把按鈕放在group中,注意new Button(group這裡
        final Button button = new Button(group, SWT.NONE);
        button.setText("按鈕");
        button.setBounds(42, 59, 48, 22);
        //構造text
        text = new Text(group, SWT.BORDER);
        text.setBounds(32, 28, 80, 25);
        //
        shell.layout();
        while(!shell.isDisposed()){
            if (!display.readAndDispatch()){
                display.sleep();
            }
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.