javafx 将父 borderpane 设置为 null
javafx set parent borderpane to null
我有 2 个 fxml
FXML 一:
它包含 ID 为 fx:id="UnitBorderPane"
的边框
FXML B:
它包含 ID 为 fx:id="UnitForm"
的锚窗格
我在左侧的边框 A 加载 "FXML B"
FXMLLoader loader = new
FXMLLoader(getClass().getResource("/projectname/unit/UnitForm.fxml"));
Pane pane = (Pane) loader.load();
UnitBorderPane.setLeft(pane);
它是一种 fxml 形式,所以我们有一个带动作的按钮
<Button layoutX="102.0" layoutY="169.0" mnemonicParsing="false" onAction="#saveUnit" text="Save" />
如何隐藏左侧的 FXML A BorderPane?
@FXML
private void saveUnit(ActionEvent event) {
BorderPane borpane = (BorderPane)UnitForm.getParent().lookup("#UnitBorderPane");
borpane.setLeft(null);
}
此代码无效,borpane 变量为 null,因此我无法将 borderPane FXML A Left 设置为 null。
我觉得应该是
BorderPane borpane = (BorderPane)UnitForm.getParent();
不过,none这个感觉很稳健;例如,如果您决定完全更改布局结构,您可能需要在各种 类 中更改大量代码。我会在 UnitForm.fxml
的控制器中添加一个 属性,您可以从 UnitBorderPane
的控制器中观察到它。类似于:
public class UnitFormController { // your actual class name may differ....
private final BooleanProperty saved = new SimpleBooleanProperty();
public BooleanProperty savedProperty() {
return saved ;
}
public final boolean isSaved() {
return savedProperty().get();
}
public final void setSaved(boolean saved) {
savedProperty().set(saved);
}
// other code as you already have...
@FXML
private void saveUnit() {
setSaved(true);
}
// ...
}
那你就做
FXMLLoader loader =
new FXMLLoader(getClass().getResource("/projectname/unit/UnitForm.fxml"));
Pane pane = (Pane) loader.load();
UnitFormController controller = loader.getController();
controller.savedProperty().addListener((obs, wasSaved, isNowSaved) -> {
if (isNowSaved) {
UnitBorderPane.setLeft(null);
}
});
UnitBorderPane.setLeft(pane);
现在 UnitBorderPane
的管理全部集中在一个地方,而不是分成两个控制器,并且没有查找(不可靠)。 UnitForm
的控制器只需设置一个 属性 并让另一个控制器根据需要做出响应。
我有 2 个 fxml
FXML 一: 它包含 ID 为 fx:id="UnitBorderPane"
的边框FXML B: 它包含 ID 为 fx:id="UnitForm"
的锚窗格我在左侧的边框 A 加载 "FXML B"
FXMLLoader loader = new
FXMLLoader(getClass().getResource("/projectname/unit/UnitForm.fxml"));
Pane pane = (Pane) loader.load();
UnitBorderPane.setLeft(pane);
它是一种 fxml 形式,所以我们有一个带动作的按钮
<Button layoutX="102.0" layoutY="169.0" mnemonicParsing="false" onAction="#saveUnit" text="Save" />
如何隐藏左侧的 FXML A BorderPane?
@FXML
private void saveUnit(ActionEvent event) {
BorderPane borpane = (BorderPane)UnitForm.getParent().lookup("#UnitBorderPane");
borpane.setLeft(null);
}
此代码无效,borpane 变量为 null,因此我无法将 borderPane FXML A Left 设置为 null。
我觉得应该是
BorderPane borpane = (BorderPane)UnitForm.getParent();
不过,none这个感觉很稳健;例如,如果您决定完全更改布局结构,您可能需要在各种 类 中更改大量代码。我会在 UnitForm.fxml
的控制器中添加一个 属性,您可以从 UnitBorderPane
的控制器中观察到它。类似于:
public class UnitFormController { // your actual class name may differ....
private final BooleanProperty saved = new SimpleBooleanProperty();
public BooleanProperty savedProperty() {
return saved ;
}
public final boolean isSaved() {
return savedProperty().get();
}
public final void setSaved(boolean saved) {
savedProperty().set(saved);
}
// other code as you already have...
@FXML
private void saveUnit() {
setSaved(true);
}
// ...
}
那你就做
FXMLLoader loader =
new FXMLLoader(getClass().getResource("/projectname/unit/UnitForm.fxml"));
Pane pane = (Pane) loader.load();
UnitFormController controller = loader.getController();
controller.savedProperty().addListener((obs, wasSaved, isNowSaved) -> {
if (isNowSaved) {
UnitBorderPane.setLeft(null);
}
});
UnitBorderPane.setLeft(pane);
现在 UnitBorderPane
的管理全部集中在一个地方,而不是分成两个控制器,并且没有查找(不可靠)。 UnitForm
的控制器只需设置一个 属性 并让另一个控制器根据需要做出响应。