使用JavaFX构建反应系统
JavaFX是用于在Java中構建圖形應用程序的新標準庫,但是許多程序員仍然對Swing甚至(高音)AWT感到困惑。 在Java誕生20年來,發生了很多事情。 兩年前,當我開始研究Speedment UI的JavaFX庫時,發現很多東西很著迷! 這里有一些技巧,說明如何使用JavaFX工具包中的許多令人敬畏的新功能來構建反應Swift的應用程序!
1.財產價值
如果您窺探了JavaFX組件,那么您一定遇到過“屬性”一詞。 幾乎可以觀察到FX庫中的每個值,分隔線的寬度,圖像的大小,標簽中的文本,列表的子級以及復選框的狀態。 屬性分為兩類: 可寫和可讀。 可以使用設置器或直接修改屬性來更改可寫值。 JavaFX將處理事件處理,并確保將通知依賴該屬性的每個組件。 可讀值具有使您可以在值更改時接收通知的方法。
例:
// Read- and writable StringProperty name = new SimpleStringProperty("Emil"); // Only readable ObservableBooleanValue nameIsEmpty = name.isEmpty();2.綁定值
當具有可寫和可讀的值時,可以開始定義這些值如何關聯的規則。 可寫屬性可以綁定到可讀屬性,以便其值將始終與可讀屬性匹配。 綁定不是立即的,但是在觀察值之前,它們將被解析(請參閱我在那里所做的)。 綁定可以是單向或雙向的。 當然,如果它們是雙向的,則兩個屬性都必須是可寫的。
例:
TextField fieldA = new TextField(); TextField fieldB = new TextField(); fieldA.prefWidthProperty().bind(fieldB.widthProperty());3.可觀察的清單
屬性不是唯一可以觀察到的東西。 如果列表包含在ObservableList中,則也可以觀察列表的成員。 ObservableList的反應模型非常先進。 修改列表后,您不僅會收到通知,還可以確切地看到列表的更改方式。
例:
List<String> otherList = Arrays.asList("foo", "bar", "bar"); ObservableList<String> list = FXCollections.observableList(otherList);list.addListener((ListChangeListener.Change<? extends String> change) -> {System.out.println("Received event.");while (change.next()) {if (change.wasAdded()) {System.out.println("Items " + change.getAddedSubList() + " was added.");}if (change.wasRemoved()) {System.out.println("Items " + change.getRemoved() + " was removed.");}} });System.out.println("Old list: " + list); list.set(1, "foo"); System.out.println("New list: " + list);上面的輸出是:
Old list: [foo, bar, bar] Received event. Items [foo] was added. Items [bar] was removed. New list: [foo, foo, bar]如您所見,設置操作僅創建一個事件。
4. StringConverter
有時,您會發現需要創建綁定時組件中沒有確切的值。 一個典型的例子是您擁有一個StringProperty ,它具有從TextField獲得的路徑。 如果您希望此值表示為Path的可觀察屬性,則需要為此創建一個StringConverter。
例:
TextField fileLocation = new TextField(); StringProperty location = fileLocation.textProperty(); Property<Path> path = new SimpleObjectProperty<>();Bindings.bindBidirectional(location, path, new StringConverter<Path>() {@Overridepublic String toString(Path path) {return path.toString();}@Overridepublic Path fromString(String string) {return Paths.get(string);} });對象屬性未雙向綁定到文本字段值。
5.表達
使用前面顯示的Bindings-class可以創建各種表達式。 假設您有兩個文本字段,用戶可以在其中輸入信息。 現在,您需要定義一個只讀字段,該字段始終包含一個字符串,如果兩個字符串的長度相等,則表示兩個字符之間的字符混合。 如果長度不相等,則應顯示一條幫助消息。
例:
TextField first = new TextField(); TextField second = new TextField(); TextField mix = new TextField();mix.textProperty().bind(Bindings.when(first.lengthProperty().isEqualTo(second.lengthProperty())).then(Bindings.createStringBinding(() -> {int length = first.lengthProperty().get();String firstText = first.textProperty().get();String secondText = second.textProperty().get();char[] result = new char[length * 2];for (int i = 0; i < length; i++) {result[i * 2] = firstText.charAt(i);result[i * 2 + 1] = secondText.charAt(i);}return new String(result);}, first.textProperty(),second.textProperty())).otherwise("Please enter two strings of exactly the same length.") );結論
這些只是JavaFX眾多功能中的少數。 希望您能找到更多利用事件系統的創新方法!
翻譯自: https://www.javacodegeeks.com/2016/02/building-reactive-systems-javafx.html
總結
以上是生活随笔為你收集整理的使用JavaFX构建反应系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 消息称特斯拉Model 3焕新版也准备向
- 下一篇: gkz cloud sql_使用Clou