[英]how to load 2 fxml files in the same time
我有两个 javafx 窗格,“pane1”和“pane2”(“pane1”是“pane2”的第一个父级)。 如何同时加载两个 fxml 文件,比如“fxml1.fxml”和“fxml2.fxml”,同时加载“pane1”中的“fxml1.fxml”和“pane2”中的“fxml2.fxml”。
这是我尝试过的。 但这仅加载 fxml1.fxml 文件但不加载 fxml2.fxml 文件...
public class HostelController implements Initializable {
@FXML
private Button hostlersBut;
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@FXML
private void hostlersClkHostel(ActionEvent event) {
try {
pane1.getChildren().clear();
pane1.getChildren().add(FXMLLoader.load(getClass().getResource("fxml1.fxml")));
pane2.getChildren().clear();
pane2.getChildren().add(FXMLLoader.load(getClass().getResource("fxml2.fxml")));
} catch (IOException ex) {
System.out.print(ex);
}
}
}
这是当前视图的 fxml 文件
<AnchorPane id="AnchorPane" fx:id="anchorPane" prefHeight="508.0" prefWidth="968.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.HostelController">
<children>
<Pane fx:id="pane1" prefHeight="538.0" prefWidth="1014.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Button fx:id="hostelersBut" layoutX="7.0" layoutY="100.0" onAction="#hostelAction" prefHeight="30.0" prefWidth="150.0" text="Hostel"> </Button>
<Pane fx:id="pane2" layoutX="166.0" prefHeight="538.0" prefWidth="846.0">
<children>
</children>
</Pane>
</children>
</Pane>
</children>
</AnchorPane>
这是一个最小的代码,还有一些其他按钮,所以我不能使用 initialize 方法在加载时初始化一个窗格......正如我所说,只有第一个 fxml 文件被加载,第二个没有......
在您的 FXML 中, pane2
是pane2
的子pane1
。 因此,当您调用pane1.getChildren().clear()
您将pane2
从 UI 中删除pane2
。 您需要pane1
和pane2
没有父/子关系。
取决于你真正想要发生的事情,比如
<AnchorPane id="AnchorPane" fx:id="anchorPane" prefHeight="508.0" prefWidth="968.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.HostelController">
<children>
<Pane fx:id="pane1" prefHeight="538.0" prefWidth="1014.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0" />
<Button fx:id="hostelersBut" layoutX="7.0" layoutY="100.0" onAction="#hostelAction" prefHeight="30.0" prefWidth="150.0" text="Hostel"> </Button>
<Pane fx:id="pane2" layoutX="166.0" prefHeight="538.0" prefWidth="846.0" />
</children>
</AnchorPane>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.