[英]JavaFX buttons disappear when .fxml not root in scene
所以我有這個.fxml:
<StackPane fx:controller="controller.MainController" xmlns:fx="http://javafx.com/fxml">
<Pane fx:id="aDrawPane" prefHeight="8000" prefWidth="10000" minWidth="10000" minHeight="8000">
</Pane>
<BorderPane fx:id="aBorderPane">
<top>
<VBox>
<ToolBar fx:id="aToolBar" orientation="HORIZONTAL">
<HBox fx:id="umlBox">
<Button text="Create" fx:id="createBtn"/>
<Button text="Package" fx:id="packageBtn"/>
<Button text="Edge" fx:id="edgeBtn"/>
<Button text="Draw" fx:id="drawBtn"/>
</HBox>
<HBox fx:id="utilBox">
<Button text="Select" fx:id="selectBtn"/>
<Button text="Move" fx:id="moveBtn"/>
</HBox>
<HBox fx:id="undoBox">
<Button text="Delete" fx:id="deleteBtn"/>
<Button text="Undo" fx:id="undoBtn"/>
<Button text="Redo" fx:id="redoBtn"/>
</HBox>
<HBox fx:id="recognizeBox">
<Button text="Recognize" fx:id="recognizeBtn"/>
</HBox>
</ToolBar>
</VBox>
</top>
<bottom>
<ToolBar>
<Pane HBox.hgrow="ALWAYS" />
<VBox alignment="CENTER">
<Slider fx:id="zoomSlider" min="10" max="200" value="100"/>
<Label text="Zoom"/>
</VBox>
<Pane HBox.hgrow="ALWAYS" />
</ToolBar>
</bottom>
</BorderPane>
<stylesheets>
<URL value="@main.css" />
</stylesheets>
</StackPane>
我的應用程序啟動器類:
public class Launcher extends Application {
public void start(Stage stage) throws IOException {
BorderPane tabView = null;
FXMLLoader loader;
StackPane canvasView = null;
try {
loader = new FXMLLoader(getClass().getClassLoader().getResource("view.fxml"));
canvasView = (StackPane) loader.load();
} catch (IOException e) {
System.out.println(e.getMessage());
}
Scene scene = new Scene(canvasView, 1000, 800);
stage.setScene(scene);
stage.show();
}
}
使用此功能,頂部工具欄中的按鈕可見,並且與底部的滑塊一樣有效。
但是,如果我將代碼更改為以下內容:
Group root = new Group();
root.getChildren().add(canvasView);
Scene scene = new Scene(root, 1000, 800);
頂部工具欄可見,但按鈕不可見且不可點擊,底部工具欄完全消失(第一個窗格“aDrawPane”按預期工作)。 當我想將view.fxml放在Tab中時,我遇到了這個問題。 為什么會發生這種情況,如何讓工具欄和按鈕再次可見?
原因是一個集團沒有調整其子項的大小,而是將它們設置為首選大小。
FXML表明Pane應該是
prefHeight="8000" prefWidth="10000" minWidth="10000"
而這正是你得到的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.