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 阅读(1676) 评论(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);







只有注册用户登录后才能发表评论。


网站导航: