日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

自定义ui_如何允许用户自定义UI

發(fā)布時(shí)間:2023/12/3 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义ui_如何允许用户自定义UI 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

自定義ui

理念

利用JavafX / FXML的聲明性設(shè)計(jì)模式,并允許用戶僅通過(guò)使用例如SceneBuilder打開(kāi)某個(gè)視圖即可重新定制布局或添加新控件,甚至根據(jù)用戶需要更改樣式,從而無(wú)需任何編碼即可自定義某個(gè)視圖。

FXML文件+ CSS基本上可以放置在通過(guò)URL可以到達(dá)的任何地方。 用戶必須只知道FXML內(nèi)部分配的控制器類的接口/方法。

遙控器

假設(shè)此簡(jiǎn)單的演示控制器類提供了用于遠(yuǎn)程控制設(shè)備和發(fā)送MQTT消息的方法,則用戶可以自定義自己的遙控器。

public class RemoteController{@FXMLpublic void onTest(){Alert alert = new Alert(Alert.AlertType.INFORMATION);alert.setContentText("");alert.setHeaderText("WORKS!");alert.show();}public void onTest(String value){Alert alert = new Alert(Alert.AlertType.INFORMATION);alert.setHeaderText("WORKS!");alert.setContentText(value);alert.show();}public void onSwitch(String houseCode, int groudId, int deviceId, String command){Alert alert = new Alert(Alert.AlertType.INFORMATION);alert.setHeaderText("Switch!");alert.setContentText(String.format("Command: send %s %d %d %s", houseCode, groudId, deviceId, command));alert.show();} }

remote.fxml和remote.css

請(qǐng)注意引用的de.jensd.shichimifx.demo.ext.RemoteController和remote.css

因此,基本上可以通過(guò)以下方式調(diào)用控制器動(dòng)作:

onAction="#onTest".

不錯(cuò):

如果添加:

<?language javascript?>

到FXML,也可以通過(guò)controller -instance通過(guò)JavaScript調(diào)用傳遞參數(shù)。

onAction=controller.onTest('OFF') onAction=controller.onSwitch('a',1,1,'ON')

不幸的是,除了-> this之外 ,我找不到有關(guān)此功能的更多文檔,但是以某種方式它神奇地起作用了;-)。 甚至可以傳遞不同類型的參數(shù)。

<?xml version="1.0" encoding="UTF-8"?><?language javascript?> <?import javafx.geometry.*?> <?import java.lang.*?> <?import java.net.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?><VBox alignment="TOP_CENTER" prefHeight="400.0" prefWidth="600.0" spacing="20.0" styleClass="main-pane" stylesheets="@remote.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.jensd.shichimifx.demo.ext.RemoteController"><children><Label styleClass="title-label" text="Universal Remote" /><HBox alignment="CENTER_RIGHT" spacing="20.0"><children><Label layoutX="228.0" layoutY="96.0" styleClass="sub-title-label" text="Light Frontdoor" /><Button layoutX="43.0" layoutY="86.0" mnemonicParsing="false" onAction="#onTest" prefWidth="150.0" styleClass="button-on" text="ON" /><Button layoutX="411.0" layoutY="86.0" mnemonicParsing="false" onAction="#onTest" prefWidth="150.0" styleClass="button-off" text="OFF" /></children><padding><Insets left="10.0" right="10.0" /></padding></HBox><HBox alignment="CENTER_RIGHT" spacing="20.0"><children><Label layoutX="228.0" layoutY="96.0" styleClass="sub-title-label" text="Light Garden" /><Button layoutX="43.0" layoutY="86.0" mnemonicParsing="false" onAction="controller.onTest('ON')" prefWidth="150.0" styleClass="button-on" text="ON" /><Button layoutX="411.0" layoutY="86.0" mnemonicParsing="false" onAction="controller.onTest('OFF')" prefWidth="150.0" styleClass="button-off" text="OFF" /></children><padding><Insets left="10.0" right="10.0" /></padding></HBox><HBox alignment="CENTER_RIGHT" spacing="20.0"><children><Label layoutX="228.0" layoutY="96.0" styleClass="sub-title-label" text="Light Garden" /><Button layoutX="43.0" layoutY="86.0" mnemonicParsing="false" onAction="controller.onSwitch('a', 1,1,'ON')" prefWidth="150.0" styleClass="button-on" text="ON" /><Button layoutX="411.0" layoutY="86.0" mnemonicParsing="false" onAction="controller.onTest('OFF')" prefWidth="150.0" styleClass="button-off" text="OFF" /></children><padding><Insets left="10.0" right="10.0" /></padding></HBox></children><padding><Insets bottom="20.0" left="20.0" right="20.0" top="20.0" /></padding> </VBox>

基于此示例,用戶可以使用SceneBuilder輕松打開(kāi)FXM1,并添加新的Button來(lái)調(diào)用controller.onSwitch()方法,以控制為家庭自動(dòng)化安裝的不同/新設(shè)備。

FxmlUtils

的下一個(gè)版本ShichimiFX將包含新Utilily類負(fù)載FXML如圖中ExternalFXMLDemoController請(qǐng)注意 ,已加載的窗格通過(guò)onLoadExternalFxml()添加到演示應(yīng)用程序的externalPane (BorderPane)的中心:

public class ExternalFXMLDemoController {@FXMLprivate ResourceBundle resources;@FXMLprivate BorderPane externalPane;@FXMLprivate TextField fxmlFileNameTextField;@FXMLprivate Button chooseFxmlFileButton;@FXMLprivate Button loadFxmlFileButton;private StringProperty fxmlFileName;public void initialize() {fxmlFileNameTextField.textProperty().bindBidirectional(fxmlFileNameProperty());loadFxmlFileButton.disableProperty().bind(fxmlFileNameProperty().isEmpty());}public StringProperty fxmlFileNameProperty() {if (fxmlFileName == null) {fxmlFileName = new SimpleStringProperty("");}return fxmlFileName;}public String getFxmlFileName() {return fxmlFileNameProperty().getValue();}public void setFxmlFileName(String fxmlFileName) {this.fxmlFileNameProperty().setValue(fxmlFileName);}@FXMLpublic void chooseFxmlFile() {FileChooser chooser = new FileChooser();chooser.setTitle("Choose FXML file to load");if (getFxmlFileName().isEmpty()) {chooser.setInitialDirectory(new File(System.getProperty("user.home")));} else {chooser.setInitialDirectory(new File(getFxmlFileName()).getParentFile());}File file = chooser.showOpenDialog(chooseFxmlFileButton.getScene().getWindow());if (file != null) {setFxmlFileName(file.getAbsolutePath());}}@FXMLpublic void onLoadExternalFxml() {try {Optional<URL> url = FxmlUtils.getFxmlUrl(Paths.get(getFxmlFileName()));if (url.isPresent()) {Pane pane = FxmlUtils.loadFxmlPane(url.get(), resources);externalPane.setCenter(pane);} else {Alert alert = new Alert(Alert.AlertType.WARNING);alert.setContentText(getFxmlFileName() + " could not be found!");alert.show();}} catch (IOException ex) {Dialogs.create().showException(ex);}} }

翻譯自: https://www.javacodegeeks.com/2015/01/how-to-allow-users-to-customize-the-ui.html

自定義ui

總結(jié)

以上是生活随笔為你收集整理的自定义ui_如何允许用户自定义UI的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。