JavaFX:从 Vbox 中删除 child
JavaFX: Remove a child from Vbox
所以我是 JavaFX 的新手,我正在尝试为学校做一个应用程序并且我已经阅读了很多相关的帖子,但我似乎无法弄清楚如何为我自己的案例做这件事。这是一张图片
所以大屏是我的主屏,红色边框的是Vbox的children。对于 child,我有一个单独的 FXML 和一个控制器。当我点击 "Close Deal" 按钮时,我想删除 Vbox 的特定 child。
我知道我应该做 parent.getChildren().remove(specific_child_node); 问题是我不知道如何获得 specific_child_node 表示 child 已按下 "Closed Deal" 按钮。关闭按钮在 child 的控制器中,Vbox 在主页面控制器中
有人知道我该怎么做吗?
我创建了一个小示例来展示如何做到这一点:
主控制器Class:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import java.io.IOException;
public class MainController {
@FXML
private VBox middleVBox;
@FXML
public void handleAddChildBtnClick() {
FXMLLoader loader = new FXMLLoader(getClass().getResource("deal.fxml"));
GridPane root = null;
try {
root = loader.load();
} catch (IOException ex) {
ex.printStackTrace();
}
if (root == null) return;
// Get the controller instance:
DealController controller = loader.getController();
// Set a reference for the parent vbox:
controller.setParent(middleVBox);
middleVBox.getChildren().add(1, root);
}
}
FXML 文件 "main.fxml":
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<GridPane alignment="center" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.MainController">
<children>
<VBox style="-fx-background-color: black;">
<children>
<Label text="Welcome, Client" textFill="WHITE" />
</children>
</VBox>
<VBox fx:id="middleVBox" GridPane.columnIndex="1">
<children>
<Label text="Generate Public Auction" />
<VBox>
<children>
<Label text="dfgh" textFill="#1aab0d" />
</children>
</VBox>
</children>
</VBox>
<VBox GridPane.columnIndex="2">
<children>
<Button mnemonicParsing="false" onAction="#handleAddChildBtnClick" text="Add Child" />
</children>
</VBox>
</children>
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints prefHeight="200.0" />
</rowConstraints>
</GridPane>
DealController Class:
package sample;
import javafx.fxml.FXML;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
public class DealController {
@FXML
private GridPane dealRoot;
private VBox parent;
@FXML
public void handleCloseDealBtnClick() {
parent.getChildren().remove(dealRoot);
}
void setParent(VBox parent) {
this.parent = parent;
}
}
FXML 文件 "deal.fxml":
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane fx:id="dealRoot" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.DealController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" />
<ColumnConstraints hgrow="SOMETIMES" />
</columnConstraints>
<rowConstraints>
<RowConstraints vgrow="SOMETIMES" />
<RowConstraints vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="cvbn" textFill="#008612" />
<Button mnemonicParsing="false" onAction="#handleCloseDealBtnClick" text="Close Deal" GridPane.columnIndex="1" GridPane.rowIndex="1" />
</children>
</GridPane>
主要Class:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
所以我是 JavaFX 的新手,我正在尝试为学校做一个应用程序并且我已经阅读了很多相关的帖子,但我似乎无法弄清楚如何为我自己的案例做这件事。这是一张图片
所以大屏是我的主屏,红色边框的是Vbox的children。对于 child,我有一个单独的 FXML 和一个控制器。当我点击 "Close Deal" 按钮时,我想删除 Vbox 的特定 child。
我知道我应该做 parent.getChildren().remove(specific_child_node); 问题是我不知道如何获得 specific_child_node 表示 child 已按下 "Closed Deal" 按钮。关闭按钮在 child 的控制器中,Vbox 在主页面控制器中
有人知道我该怎么做吗?
我创建了一个小示例来展示如何做到这一点:
主控制器Class:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import java.io.IOException;
public class MainController {
@FXML
private VBox middleVBox;
@FXML
public void handleAddChildBtnClick() {
FXMLLoader loader = new FXMLLoader(getClass().getResource("deal.fxml"));
GridPane root = null;
try {
root = loader.load();
} catch (IOException ex) {
ex.printStackTrace();
}
if (root == null) return;
// Get the controller instance:
DealController controller = loader.getController();
// Set a reference for the parent vbox:
controller.setParent(middleVBox);
middleVBox.getChildren().add(1, root);
}
}
FXML 文件 "main.fxml":
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<GridPane alignment="center" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.MainController">
<children>
<VBox style="-fx-background-color: black;">
<children>
<Label text="Welcome, Client" textFill="WHITE" />
</children>
</VBox>
<VBox fx:id="middleVBox" GridPane.columnIndex="1">
<children>
<Label text="Generate Public Auction" />
<VBox>
<children>
<Label text="dfgh" textFill="#1aab0d" />
</children>
</VBox>
</children>
</VBox>
<VBox GridPane.columnIndex="2">
<children>
<Button mnemonicParsing="false" onAction="#handleAddChildBtnClick" text="Add Child" />
</children>
</VBox>
</children>
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints prefHeight="200.0" />
</rowConstraints>
</GridPane>
DealController Class:
package sample;
import javafx.fxml.FXML;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
public class DealController {
@FXML
private GridPane dealRoot;
private VBox parent;
@FXML
public void handleCloseDealBtnClick() {
parent.getChildren().remove(dealRoot);
}
void setParent(VBox parent) {
this.parent = parent;
}
}
FXML 文件 "deal.fxml":
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane fx:id="dealRoot" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.DealController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" />
<ColumnConstraints hgrow="SOMETIMES" />
</columnConstraints>
<rowConstraints>
<RowConstraints vgrow="SOMETIMES" />
<RowConstraints vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="cvbn" textFill="#008612" />
<Button mnemonicParsing="false" onAction="#handleCloseDealBtnClick" text="Close Deal" GridPane.columnIndex="1" GridPane.rowIndex="1" />
</children>
</GridPane>
主要Class:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}