如何在 JavaFX 中组合 FXML 文件和组?
How can I combine a FXML file and a group in JavaFX?
所以我为学校制作的一个小游戏制作了一个 FXML 文件,里面有一些按钮和标签,还有自己的控制器。现在我做了一组矩形,想把它添加到和fxml文件相同的场景中。
button.getParent().getChildren().add(group);
我在这里写的代码不起作用。有人知道如何在 fxml 文件中添加组或只是在场景中渲染它吗?
在 2 个不同场景中渲染 fxml 和组确实有效,因此没有错误。
编辑:
申请class:
package retris;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Arno Vandersmissen, Casper Vranken, Rani Vanhoudt
*/
public class Retris extends Application {
private Stage stage;
@Override
public void start(Stage stage) throws Exception {
this.stage = stage;
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("FXMLRetris.fxml"));
Parent root = loader.load();
FXMLRetrisController controller = loader.getController();
controller.playMusic();
stage.setOnCloseRequest(e -> {
e.consume();
FXMLConfirmController confirm= new FXMLConfirmController();
if(confirm.close("Close?")){
Platform.exit();
}
});
Scene scene = new Scene(root);
stage.setTitle("Retris");
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
一个Scene
一次只能显示一个Parent
。无论您想在 GUI 中显示什么,都将包含在 Parent
中。假设,正如您在评论中所建议的那样,您想在运行时更新该父级,您需要引用应该包含您的 group of rectangles
.
的父级的任何子级
假设您的 fxml 文件的根元素是 AnchorPane
,并且您还想将 group of rectangles
添加到该根元素。在您的 .fxml 文件中,您需要一个 fx:id 标签 <AnchorPane fx:id="myRoot">
这允许您使用 @FXML 注释将元素注入到您的控制器 class。
public class MyController {
@FXML private AnchorPane myRoot;
@FXML private void createAndAddRectangles {
/**myRoot is already instantiated. you can simply add nodes to it at runtime
by using onAction="createAndAddRectangles" tag on a button in your .fxml file.**/
}
}
所以我为学校制作的一个小游戏制作了一个 FXML 文件,里面有一些按钮和标签,还有自己的控制器。现在我做了一组矩形,想把它添加到和fxml文件相同的场景中。
button.getParent().getChildren().add(group);
我在这里写的代码不起作用。有人知道如何在 fxml 文件中添加组或只是在场景中渲染它吗?
在 2 个不同场景中渲染 fxml 和组确实有效,因此没有错误。
编辑:
申请class:
package retris;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Arno Vandersmissen, Casper Vranken, Rani Vanhoudt
*/
public class Retris extends Application {
private Stage stage;
@Override
public void start(Stage stage) throws Exception {
this.stage = stage;
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("FXMLRetris.fxml"));
Parent root = loader.load();
FXMLRetrisController controller = loader.getController();
controller.playMusic();
stage.setOnCloseRequest(e -> {
e.consume();
FXMLConfirmController confirm= new FXMLConfirmController();
if(confirm.close("Close?")){
Platform.exit();
}
});
Scene scene = new Scene(root);
stage.setTitle("Retris");
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
一个Scene
一次只能显示一个Parent
。无论您想在 GUI 中显示什么,都将包含在 Parent
中。假设,正如您在评论中所建议的那样,您想在运行时更新该父级,您需要引用应该包含您的 group of rectangles
.
假设您的 fxml 文件的根元素是 AnchorPane
,并且您还想将 group of rectangles
添加到该根元素。在您的 .fxml 文件中,您需要一个 fx:id 标签 <AnchorPane fx:id="myRoot">
这允许您使用 @FXML 注释将元素注入到您的控制器 class。
public class MyController {
@FXML private AnchorPane myRoot;
@FXML private void createAndAddRectangles {
/**myRoot is already instantiated. you can simply add nodes to it at runtime
by using onAction="createAndAddRectangles" tag on a button in your .fxml file.**/
}
}