在這個範例(由官網取得)中,我們針對3個RadioButton建立了一個群組(ButtonGroup)稱為numbers。同時在它們下方有一個PushButton,按下它會以Alert顯示出哪一個RadioButton是被選取的。
============範例展示=================================================
============範例展示=================================================
下面是ui.bxml:
各個WTK元件的標籤(Label)是使用butttonData來指定。
<!--ui.bxml--> <myex:RBWindows title="Radio Buttons" maximized="true" xmlns:bxml="http://pivot.apache.org/bxml" xmlns:myex="myex" xmlns="org.apache.pivot.wtk"> <Border> <BoxPane orientation="vertical" styles="{padding:4}"> <bxml:define> <ButtonGroup bxml:id="numbers"/> </bxml:define> <RadioButton bxml:id="oneButton" buttonData="第一個RadioButton" buttonGroup="$numbers" selected="true"/> <RadioButton bxml:id="twoButton" buttonData="第二個RadioButton" buttonGroup="$numbers"/> <RadioButton bxml:id="threeButton" buttonData="第三個RadioButton" buttonGroup="$numbers"/> <PushButton bxml:id="selectButton" buttonData="請選擇"/> </BoxPane> </Border> </myex:RBWindows>
myex#RBWindows.java
package myex; import java.net.URL; import org.apache.pivot.beans.BXML; import org.apache.pivot.beans.Bindable; import org.apache.pivot.collections.Map; import org.apache.pivot.util.Resources; import org.apache.pivot.wtk.*; public class RBWindows extends Window implements Bindable { @BXML private PushButton selectButton = null; @BXML private ButtonGroup numbers = null; @Override public void initialize(Map<String, Object> namespace, URL location, Resources resources) { selectButton.getButtonPressListeners().add(new ButtonPressListener() { @Override public void buttonPressed(Button button) { //使用ButtonGroup#getSelection()取得使用者按的RadioButton String message = "你按了 \"" + numbers.getSelection().getButtonData() + "\"."; Alert.alert(MessageType.INFO, message, RBWindows.this); } }); } }
主程式
//newClass.java import java.awt.Font; import org.apache.pivot.beans.BXMLSerializer; import org.apache.pivot.collections.Map; import org.apache.pivot.wtk.*; public class NewClass implements Application { private Window window = null; public static void main(String[] args) { DesktopApplicationContext.main(NewClass.class, args); } @Override public void startup(Display display, Map<String, String> properties) throws Exception { Theme.getTheme().setFont(new Font("PMingLiU",Font.PLAIN,12)); BXMLSerializer bxmlSerializer = new BXMLSerializer(); window = (Window) bxmlSerializer.readObject(NewClass.class, "ui.bxml"); window.open(display); } @Override public boolean shutdown(boolean optional) { if (window != null) { window.close(); } return false; } @Override public void suspend() { } @Override public void resume() { } }
PS:
要特別注意的是,自訂類別的首字一定是大寫英文字母,不然會被BXMLSerializer誤認為是java bean的屬性。
No comments:
Post a Comment