Button Hierarchy
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);