Posted on 2006-03-13 16:11
fortune 閱讀(1697)
評(píng)論(0) 編輯 收藏 所屬分類:
我的學(xué)習(xí)筆記
Button Hierarchy
習(xí)資料\swt教程\Addison.Wesley.Professional.SWT.The.Standard.Widget.Toolkit.Volume.1.Jun.2004.eBook-DDU.chm::/0321256638/images/0321256638/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時(shí)才有效) |
SWT.DOWN |
Draw a down arrow 箭頭向下(arrow時(shí)有效) |
SWT.LEFT |
Left-align the button (or draw a left arrow)文字向左對(duì)齊或箭頭向左(arrow時(shí)) |
SWT.RIGHT |
Right-align the button (or draw a right arrow)文字向右對(duì)齊或箭頭向右(arrow時(shí)) |
SWT.CENTER |
Center align the button button上的文字中間對(duì)齊 |
Button Events
SWT.Selection |
The button was selected button被選中事件 |
button是活動(dòng)控件,當(dāng)用戶點(diǎn)擊他們會(huì)發(fā)送事件
Text and Images
button支持顯示文本和圖像,它的使用大體上與label相同,但是它不支持SWT.WARP和換行符,既button上的文本字符只能是一行,而且它能只能顯示文本或圖像中的一種不能同時(shí)都顯示
setText(String string)
getText()
setImage(Image image)
getImage()
盡管button支持圖片顯示但是通常不這么使用,一般使用Tool bars 來代替
Alignment
setAlignment(int alignment) 設(shè)置對(duì)齊方式 SWT.LEFT, SWT.CENTER, or SWT.RIGHT中的一種
getAlignment() 返回對(duì)齊方式
通常很少使用這2個(gè)函數(shù),因?yàn)槟J(rèn)的對(duì)齊一般都很好
Push Buttons
SWT.PUSH,它們通常用來產(chǎn)生一個(gè)動(dòng)作,與其它類型的button不同push button不會(huì)在 SWT.Selection 事件中保持選中或未選中狀態(tài)。它們通常被認(rèn)為是未選中。
Button button = new Button(parent, SWT.PUSH);
button.setText("Ok");
Check Buttons
SWT.CHECK。它與push button不同的是它會(huì)保持一個(gè)選中的狀態(tài),用戶使用鼠標(biāo)或者鍵盤選擇這個(gè)狀態(tài)
你可以使用方法設(shè)置check button的選中狀態(tài)
setSelection(boolean selection) 設(shè)置該button為選中狀態(tài)check, radio, or toggle button時(shí)有效
getSelection() 返回該button是否被選中
Button button = new Button(parent, SWT.CHECK);
button.setText("Overwrite when typing");
button.setSelection(true);
注意:調(diào)用 setSelection() 不會(huì)產(chǎn)生一個(gè)SWT.Selection 事件,SWT的編程新手經(jīng)常會(huì)對(duì)這個(gè)感到很困惑。
當(dāng)用戶點(diǎn)擊一個(gè)check,就會(huì)觸發(fā)一個(gè)動(dòng)作事件。你可以設(shè)置動(dòng)作監(jiān)聽器
ActionListener listener =....
bold.addActionListener(listener);
italic.addActionListener(listener);
監(jiān)聽器的actionPerformed方法將查詢bold和italic兩個(gè)check button的狀態(tài)并且相應(yīng)地把面板中的字體設(shè)為普通、加粗、傾斜以及后兩種組合。
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一樣它們會(huì)保持一個(gè)布爾狀態(tài),但是當(dāng)多個(gè)radio button都是屬于一個(gè)parent時(shí),一次只能有一個(gè)被選中
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是當(dāng)被選中時(shí)一直停留在pressed狀態(tài)下的push button
Arrow Buttons
SWT.ARROW
Button button = new Button(parent, SWT.ARROW);
button.setAlignment(SWT.RIGHT);
arrow button主要用于顯示“上一頁(yè)”或“下一頁(yè)”時(shí)使用
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一樣,它們通常使用于對(duì)話框,對(duì)于絕大多數(shù)的操作系統(tǒng),程序不會(huì)產(chǎn)生一個(gè)action直到dialog被關(guān)閉
令人意外的是當(dāng)用戶在一組radio button中選擇一個(gè)radio時(shí),2個(gè)SWT.Selection 事件被發(fā)送:前一個(gè)被選中的radio button接收到一個(gè) SWT.Selection 事件通知它的選中狀態(tài)改為false,新的被選中的radio button接收到一個(gè) 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);