<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-17  評論-64  文章-79  trackbacks-1
    1、Eclipse中swt的配置

      建議配置:jdk1.4.2以及eclipse3.1

      在代碼中調用swt控件之前,首先建立一個項目,然后選擇該項目的properties -> Java Build Path,將standard Widget ToolKit加入到Library頁當中。

      接下來可以建立第一個eclipse小程序,新建一個class,并且在該class所對應的代碼中輸入如下程序,其中package以及class名稱根據實際情況來確定名稱。

    package mypakage;
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.*;
    /*導入需要的類庫*/
    public class Myfrm1 {
    public Myfrm1() {
    super();
    }
    public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    /*shell為一個窗口對象*/
    Label label = new Label(shell, SWT.NONE);
    label.setText("Hello, World!"); /*創建一個標簽對象并且設置標題文字*/
    label.pack();
    shell.pack();
    shell.open(); /*打開并顯示窗口*/

    while(!shell.isDisposed())
    if(!display.readAndDispatch())
    display.sleep(); /*在窗口沒有銷毀之前,顯示對象一直處于等待狀態*/

    display.dispose(); /*否則,銷毀對象,釋放對象所占據的資源*/
    label.dispose();
    }
    }

      運行上述代碼(run -> debug -> swt application)將產生如下所示的一個窗口

      2、button的使用

      按鈕可能的類型有很多,例如:

    SWT.BORDER 含有邊框的按鈕

    SWT.CHECK 復選按鈕

    SWT.PUSH 普通按鈕

    SWT.RADIO 單選按鈕

      3、Text的使用

      文本框的類型也有很多種選擇,例如:

    SWT.BORDER 含有邊框

    SWT.READ_ONLY 只讀

      下圖為包含按鈕以及文本框的窗口

      設計上述窗口所對應的代碼為:

    package mypakage;
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.*;
    import org.eclipse.swt.layout.*;
    public class Myfrm1 {
    public Myfrm1() {
    super();
    }
    public static void main(String[] args) {
    Display display = new Display( );
    Shell shell = new Shell(display);
    shell.setSize(300, 200);
    shell.setLayout(new RowLayout( ));
    shell.setText("Button Example");
    final Button button = new Button(shell, SWT.BORDER);
    button.setText("Click Me");
    final Text text = new Text(shell, SWT.BORDER);
    shell.open( );

    while(!shell.isDisposed( )) {
    if(!display.readAndDispatch( )) display.sleep( );
    }
    display.dispose( );
    }
    }

      如果想對控件的位置以及大小進行精確的設置,可以使用setBounds(x, y, width, height)方法來取代shell.setLayout(new RowLayout( ))。例如:button.setBounds(80, 80, 90, 20);

      button的監聽及事件處理

      對按鈕單擊事件處理的代碼:

    button.addSelectionListener(new SelectionListener( )
    {
    public void widgetSelected(SelectionEvent event)
    {
    text.setText("No worries!");
    }
    public void widgetDefaultSelected(SelectionEvent event)

    {
    text.setText("No worries!");
    }
    });

      將以上代碼加入到shell.open之前,當點擊按鈕時產生以下效果:


      分析:由于為button按鈕增加了一個監聽器,按鈕時刻處于被“監控”的狀態,當按鈕控件被選擇(點擊)既選擇事件發生時,對文本控件進行賦值”No worries”。

      根據監聽事件的原理,設計如下程序,該程序能夠獲得鼠標的X坐標,顯示在文本框中:

    package mypakage;
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.*;
    import org.eclipse.swt.layout.*;

    public class Myfrm1 {
    public Myfrm1() {
    super();
    }
    public static void main(String[] args) {
    Display display = new Display( );
    Shell shell = new Shell(display);
    shell.setSize(300, 200);
    shell.setLayout(new RowLayout( ));
    final Text text = new Text(shell, SWT.SHADOW_IN);

    shell.addMouseMoveListener(new MouseMoveListener( )
    {
    public void mouseMove(MouseEvent e)
    {
    Integer y=new Integer(e.x); /*將x坐標轉換為Integer類型的對象*/
    text.setText(y.toString());
    }
    });

    shell.open( );
    while(!shell.isDisposed( )) {
    if(!display.readAndDispatch( )) display.sleep( );
    }
    display.dispose( );
    }
    }

      監聽方式:

    ControlListener 用于處理移動以及尺寸變化

    FocusListener 用于處理得到焦點以及失去焦點

    KeyListener 處理按鍵的輸入

    MouseListener , MouseMoveListener, MouseTrackListener 對鼠標的動作進行處理

    SelectionListener 處理控件的選擇行為(包括按鈕的點擊)

      注意:監聽方式與其所能夠處理的事件具有一定的關聯性,既監聽方式決定了所能夠處理事件的種類,例如:

    shell.addMouseListener(new MouseListener( )
    {
    public void mouseMove(MouseEvent e)
    {text.setText("mousemove");}
    public void mouseDoubleClick(MouseEvent e)
    {text.setText("mousedbclc");}
    public void mouseDown(MouseEvent e)
    {}
    public void mouseUp(MouseEvent e)
    {}
    });

      你會發現在鼠標移動時,text.setText("mousemove");始終不能夠執行;并且mouseDown、mouseUp事件不能夠省略,原因就在于MouseListener只能處理mouseDoubleClick、mouseDown、mouseUp三類事件,而且這三類事件不能夠分離。

      3、List控件

      List控件的樣式包括:

    SWT.BORDER 含有邊框

    SWT.H_SCROLL 含有水平滾動條

    SWT.V_SCROLL 含有垂直滾動條

    SWT.SINGLE 允許單選

    SWT.MULTI 允許復選

      若要創建一個含有從11個元素的List,可以通過以下代碼來實現

    final List list = new List (shell, SWT.SINGLE);
    for (int i=0;i<=10;i++)
    list.add("item"+i);

      以下實例能夠判斷List控件中所選擇的選項,并且輸出顯示在控制臺中:

    package mypakage;
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.*;
    import org.eclipse.swt.layout.*;
    public class Myfrm1 {
    public Myfrm1() {
    super();
    }
    public static void main(String[] args) {
    Display display = new Display ( );
    Shell shell = new Shell (display);
    shell.setText("List Example");
    shell.setSize(300, 200);
    shell.setLayout(new FillLayout(SWT.VERTICAL));
    final List list = new List (shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
    for (int loopIndex = 0; loopIndex < 100; loopIndex++){
    list.add("Item " + loopIndex);
    }
    list.addSelectionListener(new SelectionListener( )
    {
    public void widgetSelected(SelectionEvent event)
    {
    int selections[] = list.getSelectionIndices ( );
    String outText = "";
    for (int loopIndex = 0; loopIndex < selections.length;
    loopIndex++) outText += selections[loopIndex] + " ";
    System.out.println ("You selected: " + outText);
    }
    public void widgetDefaultSelected(SelectionEvent event)
    {
    int [] selections = list.getSelectionIndices ( );
    String outText = "";
    for (int loopIndex = 0; loopIndex < selections.length; loopIndex++)
    outText += selections[loopIndex] + " ";
    System.out.println ("You selected: " + outText);
    }
    });
    shell.open ( );
    while (!shell.isDisposed ( )) {
    if (!display.readAndDispatch ( )) display.sleep ( );
    }
    display.dispose ( );
    }
    }

      效果圖:

      You selected: 4 5 6 7 8 9 10

      分析:list.getSelectionIndices ( )方法將會獲得被選擇項目的集合, selections[]或者[] elections表示動態一維數組。

      4、Menu控件

      建立菜單的一般步驟為:

      1、在建立菜單時,首先需要建立一個菜單欄,需要使用SWT.BAR屬性

      Menu menuBar = new Menu(shell, SWT.BAR);

      2、在菜單欄的基礎之上,創建下拉菜單的所對應的頂級菜單項,需要使用SWT.CASCADE屬性
    fileMenuHeader = new MenuItem(menuBar, SWT.CASCADE);

      fileMenuHeader.setText("&File");

      3、建立與頂級菜單項相關的下拉式菜單

      dropMenu1 = new Menu(shell, SWT.DROP_DOWN);

      4、將頂級菜單項與下拉菜單關聯

    MenuHeader1.setMenu(dropMenu1);

      5、為下拉菜單添加子菜單項

    dropitem1= new MenuItem(dropMenu1, SWT.PUSH);
    dropitem1.setText("open");

      6、最后,在窗口中指定需要顯示的菜單欄

    shell.setMenuBar(menuBar);

      菜單的監聽及事件

      參照按鈕的監聽以及事件,設計如下程序,當點擊 File子菜單下的“open”時,在文本框中顯示“click open menu!”

    dropitem1.addSelectionListener(new SelectionListener()
    {
    public void widgetSelected(SelectionEvent event)
    {
    text.setText("click open menu!");
    }
    public void widgetDefaultSelected(SelectionEvent event)
    {
    text.setText("click open menu!");
    }
    });

      5、使用工具欄toobar

      建立工具欄可以通過如下方式:ToolBar toolbar = new ToolBar(shell, SWT.NONE);

      在工具欄的基礎之上創建工具欄子按鈕,并且設置子按鈕的標題:

    ToolItem item1 = new ToolItem(toolbar, SWT.PUSH);
    item1.setText("item1");

      例如:

    ToolBar toolbar = new ToolBar(shell, SWT.NONE);
    ToolItem item1 = new ToolItem(toolbar, SWT.PUSH);
    item1.setText("item1");
    ToolItem item2 = new ToolItem(toolbar, SWT.PUSH);
    item2.setText("item2");

      工具欄的監聽及事件

      實例:創建一個監聽對象,將該監聽對象應用于每一個按鈕,最終來判斷鼠標點擊的是哪一個按鈕,效果圖如下。

    Listener listener = new Listener( ) {
    public void handleEvent(Event event) {
    ToolItem item =(ToolItem)event.widget;
    String string = item.getText( );
    text.setText("You selected:" + string); }
    };
    item1.addListener(SWT.Selection, listener);
    item2.addListener(SWT.Selection, listener);
    item3.addListener(SWT.Selection, listener);
    item4.addListener(SWT.Selection, listener);

      6、滾動條slider的使用

      滾動條分為有邊框、垂直、水平三種類型,利用slider.setBounds方法可以指定滾動條所在的位置。

      滾動條所能夠處理事件的包括:

    SWT.ARROW_DOWN 向下或向右按鈕被點擊

    SWT.ARROW_UP 向左或向上按鈕被點擊

    SWT.DRAG 滑塊按鈕被托動

    SWT.END 滑塊到達終點

    SWT.HOME 滑塊到達起點

    SWT.PAGE_DOWN 下方或右側的滾動條被點擊

    SWT.PAGE_UP 上方或左側的滾動條被點擊

      實例:根據滑塊的位置移動按鈕位置

    slider.addListener(SWT.Selection, new Listener( ) {

    public void handleEvent(Event event) {

    switch(event.detail) {

    case SWT.ARROW_DOWN: button.setBounds(slider.getSelection(),0,20,10);

    break;

    case SWT.ARROW_UP:button.setBounds(slider.getSelection(),0,20,10);

    break;

    case SWT.DRAG:button.setBounds(slider.getSelection(),0,20,10);

    break;

    case SWT.END:button.setBounds(slider.getSelection(),0,20,10);

    break;

    case SWT.HOME:button.setBounds(slider.getSelection(),0,20,10);

    break;

    case SWT.PAGE_DOWN:button.setBounds(slider.getSelection(),0,20,10);

    break;

    case SWT.PAGE_UP:button.setBounds(slider.getSelection(),0,20,10);

    break;

    }
    }

    });

      7、樹形控件Tree

      樹形控件使用的方法為,首先創建一個Tree類型的對象,其次在該對象的基礎之上繼續擴展節點,以及擴展節點的子節點。

      final Tree tree = new Tree(shell, SWT.BORDER);

      可以利用tree.setSize方法來改變樹形控件的大小。在創建節點時,需要指明該節點所依賴的父節點的名稱,如TreeItem item0 = new TreeItem(tree, 0);,那么item0將成為tree對象中的0級(頂級)節點。

      如下程序將在tree對象的基礎之上產生9個節點:

    final Tree tree = new Tree(shell, SWT.BORDER);

    tree.setSize(290, 290);

    for(int loopIndex1 = 2000; loopIndex1 <= 2008; loopIndex1++) {

    TreeItem item0 = new TreeItem(tree, 0);

    item0.setText("Year " + loopIndex1);

    }

      在上述實例的基礎上為每一個0級節點的基礎上擴展出12個節點:

    for(int loopIndex1 = 2000; loopIndex1 <= 2008; loopIndex1++) {

    TreeItem item0 = new TreeItem(tree, 0);

    item0.setText("Year " + loopIndex1);

    for(int loopIndex2 = 1; loopIndex2 <= 12; loopIndex2++) {

    TreeItem item1 = new TreeItem(item0, 0);

    item1.setText("Month " + loopIndex2);
    }

    }

      8、對話框dialog

      對話框是一個依托于主窗體的子窗體,如圖所示。

      例如:當在主窗體中點擊按鈕時,彈出一個對話框dialog,當關閉對話框時按鈕顯示“dialog is disposed”

    Display display = new Display( );

    final Shell shell = new Shell(display);

    shell.setSize(300, 200);

    shell.setText("main");

    final Button opener = new Button(shell, SWT.PUSH);

    opener.setText("Click Me");

    opener.setBounds(20, 20, 50, 25);

    final Shell dialog = new Shell(shell, SWT.APPLICATION_MODAL |

    SWT.DIALOG_TRIM);

    dialog.setText("dialog");

    dialog.setBounds(10,10,50,60);

    dialog.addDisposeListener(new DisposeListener(){

    public void widgetDisposed(DisposeEvent e){

    opener.setText("dialog is disposed");

    }
    });

    Listener openerListener = new Listener( ) {

    public void handleEvent(Event event) {

    dialog.open( );

    }

    };

    opener.addListener(SWT.Selection, openerListener);

    shell.open( );

    while(!dialog.isDisposed( )) {

    if(!display.readAndDispatch( )) display.sleep( );

    }

    while (!shell.isDisposed( )) {

    if (!display.readAndDispatch( ))

    display.sleep( );

    }

    display.dispose( );

    posted on 2006-04-22 01:23 飛鳥 閱讀(634) 評論(3)  編輯  收藏 所屬分類: JAVA

    評論:
    # cheap cialis 2014-05-16 16:48 | cheap_cialis
    Hello!
      回復  更多評論
      
    # cheap viagra 2014-05-16 16:48 | cheap_viagra
    Hello!
      回復  更多評論
      
    # cheap cialis 2014-05-16 16:49 | cheap_cialis
    Hello!
      回復  更多評論
      
    主站蜘蛛池模板: 我的小后妈韩剧在线看免费高清版| 亚洲最大AV网站在线观看| 免费av片在线观看网站| 曰批全过程免费视频免费看| 亚洲午夜国产精品无卡| 亚洲av综合avav中文| 久久久久亚洲av毛片大| 国产小视频免费观看| 久久精品免费一区二区喷潮| 久久w5ww成w人免费| 在线免费视频你懂的| 理论片在线观看免费| 亚洲精品乱码久久久久久蜜桃图片| 亚洲视频国产视频| 亚洲免费在线视频| 亚洲av综合av一区| 国产成人精品日本亚洲| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 色噜噜的亚洲男人的天堂| 亚洲人成电影在线观看青青| 亚洲综合婷婷久久| 久久久久亚洲av无码专区喷水| 亚洲av永久无码精品漫画| 亚洲日韩欧洲乱码AV夜夜摸| 久久久久久久亚洲精品| 曰韩亚洲av人人夜夜澡人人爽 | 国产精品亚洲а∨天堂2021| 亚洲国产av玩弄放荡人妇 | 日本xxwwxxww在线视频免费| 无码中文在线二区免费| 免费观看的毛片大全| 中文字幕无码成人免费视频| 成人免费视频网站www| 国产大片91精品免费观看不卡| 成人免费的性色视频| 国产又黄又爽又猛免费app| 大学生一级毛片免费看| 免费鲁丝片一级观看| 国产免费小视频在线观看| 国产免费观看黄AV片| 亚洲国产精品一区二区九九|