FORTUNE

          THE WAY TO THE MASTER...
          posts - 49, comments - 18, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          SWT Basic Controls -- Button

          Posted on 2006-03-13 16:11 fortune 閱讀(1701) 評論(0)  編輯  收藏 所屬分類: 我的學習筆記

          Button Hierarchy

          graphics/07inf05.gif


          Button Styles

          SWT.ARROW

          Draw an arrow instead of text or an image 將button的形狀顯示為箭頭

          SWT.CHECK

          Create a check button

          SWT.PUSH

          Create a push button

          SWT.RADIO

          Create a radio button

          SWT.TOGGLE

          Create a toggle button

          SWT.FLAT

          Draw the button with a flat look  使button看起來是flat的

          SWT.UP

          Draw an up arrow        箭頭向上(arrow時才有效)

          SWT.DOWN

          Draw a down arrow      箭頭向下(arrow時有效)

          SWT.LEFT

          Left-align the button (or draw a left arrow)文字向左對齊或箭頭向左(arrow時)

          SWT.RIGHT

          Right-align the button (or draw a right arrow)文字向右對齊或箭頭向右(arrow時)

          SWT.CENTER

          Center align the button  button上的文字中間對齊



          Button Events

          SWT.Selection

             The button was selected      button被選中事件

           
          button是活動控件,當用戶點擊他們會發送事件

          Text and Images

          button支持顯示文本和圖像,它的使用大體上與label相同,但是它不支持SWT.WARP和換行符,既button上的文本字符只能是一行,而且它能只能顯示文本或圖像中的一種不能同時都顯示

          setText(String string)
          getText()
          setImage(Image image)
          getImage()

          盡管button支持圖片顯示但是通常不這么使用,一般使用Tool bars 來代替

          Alignment

          setAlignment(int alignment)   設置對齊方式  SWT.LEFT, SWT.CENTER, or SWT.RIGHT中的一種
          getAlignment()                     返回對齊方式

          通常很少使用這2個函數,因為默認的對齊一般都很好

          Push Buttons

          SWT.PUSH,它們通常用來產生一個動作,與其它類型的button不同push  button不會在 SWT.Selection 事件中保持選中或未選中狀態。它們通常被認為是未選中。
          Button button = new Button(parent, SWT.PUSH);
          button.setText("Ok");

          Check Buttons

          SWT.CHECK。它與push  button不同的是它會保持一個選中的狀態,用戶使用鼠標或者鍵盤選擇這個狀態

          你可以使用方法設置check  button的選中狀態
          setSelection(boolean selection)  設置該button為選中狀態check, radio, or toggle button時有效
          getSelection() 返回該button是否被選中

          Button button = new Button(parent, SWT.CHECK);
          button.setText("Overwrite when typing");
          button.setSelection(true);

          注意:調用 setSelection() 不會產生一個SWT.Selection 事件,SWT的編程新手經常會對這個感到很困惑。

          當用戶點擊一個check,就會觸發一個動作事件。你可以設置動作監聽器

          ActionListener listener =....
          bold.addActionListener(listener);
          italic.addActionListener(listener);
          監聽器的actionPerformed方法將查詢bold和italic兩個check  button的狀態并且相應地把面板中的字體設為普通、加粗、傾斜以及后兩種組合。
          public void actionPerformed(ActionEvent event)
          {
             int mode = 0;
             if (bold.isSelected()) mode +=Font.BOLD;
             if (italic.isSelected()) mode += Font.ITALIC;
             label.setFont(new Font("Serif", mode, FONTSIZE));
          }

          Radio Buttons

          SWT.RADIO 與check  button一樣它們會保持一個布爾狀態,但是當多個radio  button都是屬于一個parent時,一次只能有一個被選中
          Button button1 = new Button(parent, SWT.RADIO);
          button1.setText("Don't wrap");
          button1.setSelection(true);
          Button button2 = new Button(parent, SWT.RADIO);
          button2.setText("Wrap to window");
          Button button3 = new Button(parent, SWT.RADIO);
          button3.setText("Wrap to ruler");


          Toggle Buttons

          SWT.TOGGLE    toggle  button是當被選中時一直停留在pressed狀態下的push  button


          Arrow Buttons
          SWT.ARROW
          Button button = new Button(parent, SWT.ARROW);
          button.setAlignment(SWT.RIGHT);

          arrow  button主要用于顯示“上一頁”或“下一頁”時使用

          Button Events

          SWT.Selection (SelectionEvent)

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

              public void handleEvent(Event event) {

                  System.out.println("Ok Pressed");

          }

          });

          Using SWT.Selection with Radio Buttons

          SWT.Selection 很少與radio一起使用,與check button一樣,它們通常使用于對話框,對于絕大多數的操作系統,程序不會產生一個action直到dialog被關閉
          令人意外的是當用戶在一組radio button中選擇一個radio時,2個SWT.Selection 事件被發送:前一個被選中的radio button接收到一個 SWT.Selection 事件通知它的選中狀態改為false,新的被選中的radio button接收到一個 SWT.Selection事件通知它已被選中

          Listener listener = new Listener() {
              public void handleEvent(Event event) {
                  Button button = (Button) event.widget;
                  if (!button.getSelection()) return;
                  System.out.println(
                   "Arriving " + button.getText());
              }
          };
          Button land = new Button(shell, SWT.RADIO);
          land.setText("By Land");
          land.addListener(SWT.Selection, listener);
          Button sea = new Button(shell, SWT.RADIO);
          sea.setText("By Sea");
          sea.addListener(SWT.Selection, listener);
          sea.setSelection(true);






          主站蜘蛛池模板: 新余市| 庆云县| 玉林市| 克山县| 岳西县| 九台市| 梅河口市| 天全县| 齐齐哈尔市| 克山县| 兴国县| 万山特区| 德化县| 乐平市| 定陶县| 巫溪县| 张家界市| 巴中市| 襄樊市| 九龙县| 商丘市| 汨罗市| 泉州市| 保德县| 遵义县| 陆丰市| 镶黄旗| 巫山县| 故城县| 云南省| 常熟市| 城口县| 正镶白旗| 中江县| 镇宁| 巢湖市| 八宿县| 肃宁县| 许昌县| 甘德县| 科技|