[英]JavaFX - Keep toolbar when loading next scene
加載新場景時如何保留工具欄?
到目前為止,這是我的代碼:
主類
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
primaryStage.setTitle("Test FXML application");
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Main.fxml
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ToolBar?>
<GridPane fx:controller="main.java.Controller"
xmlns:fx="http://javafx.com/fxml">
<ToolBar prefHeight="40.0" prefWidth="400.0">
<Button text="testButton" onAction="#testButton"/>
</ToolBar>
</GridPane>
控制器類
我認為問題出在這里。 我如何生成下一個場景有些破損。
public class Controller {
@FXML
private void testButton1() throws IOException {
Parent window1;
try {
window1 = FXMLLoader.load(getClass().getResource("src/testButton1.fxml"));
Stage window1Stage;
Scene window1Scene = new Scene(window1, 400, 400);
window1Stage = Main.main();
window1Stage.setScene(window1Scene);
} catch (IOException e) {
e.printStackTrace();
}
}
}
有比GridPane
更合適的布局來替換部分內容,但是基本上您需要為ToolBar
使用父級,以允許您替換其他子級。 在這種情況下, BorderPane
可能是一個好主意,但也可以使用GridPane
來完成; 它不那么優雅。
<BorderPane fx:id="container" fx:controller="main.java.Controller"
xmlns:fx="http://javafx.com/fxml">
<top>
<ToolBar prefHeight="40.0" prefWidth="400.0">
<Button text="testButton" onAction="#testButton"/>
</ToolBar>
</top>
</BorderPane>
@FXML
private BorderPane container;
@FXML
private void testButton1() throws IOException {
try {
container.setCenter(FXMLLoader.load(getClass().getResource("src/testButton1.fxml")));
container.getScene().getWindow().sizeToScene(); // resize scene to fit the full size of the content
} catch (IOException e) {
e.printStackTrace();
}
}
如果改為使用GridPane
,使用fx:id
注入它,請確保ToolBar
是您要保留的唯一子項,它是第一個子項,並且是位於rowIndex=0, columnIndex=0)
的唯一子項,也可以手動刪除其他子項:
@FXML
private GridPane container;
@FXML
private void testButton1() throws IOException {
ObservableList<Node> children = container.getChildren();
children.remove(1, children.size()); // remove every child after the first
try {
container.add(FXMLLoader.load(getClass().getResource("src/testButton1.fxml")), 0, 1);
container.getScene().getWindow().sizeToScene(); // resize scene to fit the full size of the content
} catch (IOException e) {
e.printStackTrace();
}
}
您需要為包含工具欄的主階段使用單獨的FXML
,然后使用BorderPane
在工具欄和其他屏幕之間進行分隔
參見以下示例,
主類
public class Main extends Application {
private static Stage pStage;
// Creating a static root to pass to the controller
private static BorderPane root = new BorderPane();
@Override
public void start(Stage primaryStage) throws Exception {
Parent main = FXMLLoader.load(getClass().getResource("Main.fxml"));
AnchorPane screen1 = FXMLLoader.load(getClass().getResource("screen1.fxml"));
root.setTop(main);
root.setCenter(screen1);
primaryStage.setTitle("Test FXML application");
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
/**
* Just a root getter for the controller to use
*/
public static BorderPane getRoot() {
return root;
}
控制器類
public class Controller {
@FXML
private void testButton1() throws IOException {
try {
AnchorPane screen1 = FXMLLoader.load(getClass().getResource("screen1.fxml"));
BorderPane border = Main.getRoot();
border.setCenter(screen1);
}
catch (IOException e) {
e.printStackTrace();
}
}
@FXML
private void testButton2() throws IOException {
try {
AnchorPane screen2 = FXMLLoader.load(getClass().getResource("screen2.fxml"));
BorderPane border = Main.getRoot();
border.setCenter(screen2);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Main.fxml (由場景構建器生成)
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane prefHeight="0.0" prefWidth="402.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Main.Controller">
<children>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="37.0" prefWidth="402.0">
<children>
<ToolBar prefHeight="0.0" prefWidth="400.0">
<items>
<Button onAction="#testButton1" text="SCREEN 1" />
<Button onAction="#testButton2" text="SCREEN 2" />
</items>
</ToolBar>
</children>
</Pane>
</children>
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="200.0" minHeight="34.0" prefHeight="34.0" />
<RowConstraints maxHeight="166.0" minHeight="0.0" prefHeight="166.0" />
</rowConstraints>
</GridPane>
場景1
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="210.0" prefWidth="407.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label layoutX="10.0" layoutY="10.0" prefHeight="36.0" prefWidth="374.0" text="SCREEN 1">
<font>
<Font name="System Bold" size="36.0" />
</font>
</Label>
</children>
</AnchorPane>
場景2
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="210.0" prefWidth="407.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label layoutX="10.0" layoutY="10.0" prefHeight="36.0" prefWidth="374.0" text="SCREEN 2">
<font>
<Font name="System Bold" size="36.0" />
</font>
</Label>
</children>
</AnchorPane>
輸出將是這樣的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.