我正在javafx创建项目作为我去年的项目作为javafx中的新手我很困惑我对将fxml加载到父级有很多疑问。其中一个疑问是
@FXML
private StackPane stackmain;
public void start(Stage primaryStage) {
try {
Parent root =FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("/application/application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
@FXML
public void newpopup(ActionEvent event) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.load(getClass().getResource("/anyfile.fxml").openStream());
StackPane root1 = fxmlLoader.getRoot();
stackMain.getChildren().clear();
stackMain.getChildren().add(root1);
}
这是我的主控制器java文件,我是fxml
<StackPane fx:id="stackcountry" maxHeight="-Infinity"
maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="598.0"
prefWidth="537.0" xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="maincontroller">
<children>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<BorderPane layoutX="124.0" layoutY="83.0" prefHeight="598.0"
prefWidth="537.0" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0">
<left>
<VBox prefHeight="598.0" prefWidth="138.0"
BorderPane.alignment="CENTER">
<children>
<Button mnemonicParsing="false" onAction="#newpopup"
prefHeight="44.0" prefWidth="138.0" text="newpopup" />
</children>
</VBox>
</left>
<center>
<StackPane fx:id="stackmain" prefHeight="150.0"
prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
</children>
</AnchorPane>
</children>
</StackPane>
现在当用户点击popupnew按钮然后newpopup事件触发它调用(合并)new.fxml到stackmain
<StackPane fx:id="stacknext" maxHeight="-Infinity" maxWidth="- Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="598.0" prefWidth="537.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="maincontroller">
<children>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<Button onAction="#loadnewfxml" layoutX="113.0" layoutY="29.0"
mnemonicParsing="false"
prefHeight="36.0" prefWidth="111.0" text="open" />
</children>
</AnchorPane>
</children>
</StackPane>
在我的newcontroller中,当用户点击该按钮时,我有一个按钮,另一个fxml已加载
@FXML
private StackPane stacknext;
@FXML
public void newpopup(ActionEvent event) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.load(getClass().getResource("another.fxml").openStream());
StackPane root1 = fxmlLoader.getRoot();
现在我的问题是为什么当我使用stacknext
时,下面描述的代码工作原理 stacknext.getChildren().clear();
stacknext.getChildren().add(root1);
但是当使用stackmain是javafx的这种限制或任何其他方法时,我会混淆为什么这个stackmain无法从another.fxml访问
stackmain.getChildren().clear();
stackmain.getChildren().add(root1);
最后一件事我想知道.getparent有谁知道请帮我解决这个问题