簡體   English   中英

什么是JavaFX Alert OK Button的fx:id?

[英]What is fx:id of JavaFX Alert OK Button?

我使用TestFX框架來測試我的javaFX應用程序。 我這樣測試我的應用程序:

@Test
public void shouldClickOnDeletMarkerButton() {
    FxRobot bot = new FxRobot();
    bot.robotContext();
    bot.clickOn("#deleteMarkerButton");
    bot.clickOn("javafx.scene.control.Alert.CANCEL_BUTTON"); //This doesn't work.
}

我希望他單擊JavaFX Alert Dialogs的OK按鈕,但是我沒有找到fx:id。

什么是JavaFX Alert OK Button的fx:id?

編輯:我解決了我的問題,FxRobot知道“讀”,這足以使:

@Test
public void shouldClickOnDeletMarkerButtonWhenAnyMarkerAsBeenCreated() {
    bot.robotContext();
    bot.clickOn("#deleteMarkerButton");
    bot.clickOn("OK"); //Target text / Target Button / ...
}
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane fx:controller="sample.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <Button text="Click me!" fx:id="button" onAction="#clickAction" BorderPane.alignment="CENTER"/>
</BorderPane>

如Button按鈕的代碼所示,為元素分配fx:id值會在文檔的命名空間中創建一個變量,您可以從代碼的其他地方引用該變量。

@KaluNight,您的解決方案還可以,但僅適用於英語本地化。

更好地使用ID。 實際上Alert並沒有為自己的按鈕定義fx:id ,但是我們可以做到:

Button okButton = (Button) alert.getDialogPane().lookupButton(ButtonType.OK);
okButton.setId("my custom id");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM