我正在为一个游戏开发GUI,并希望在JavaFX中将3D SubScene与2D Pane混合。我有一个名为root3D的组,其中包含所有正确设置的3D对象,然后我使用通过JavaFX Scene Builder设置的FXML文件创建Pane。但是什么也没有显示出来,我只能看到我的3D对象。
PerspectiveCamera camera = new PerspectiveCamera(true);
camera.setTranslateZ(-30);
Group root3D = new Group(model1,model2,model3); //various 3d models I imported
SubScene subScene = new SubScene(root3D, 1280, 700, true,SceneAntialiasing.BALANCED);
subScene.setCamera(camera);
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/file.fxml"));
AnchorPane pane = loader.load();
pane.getChildren().add(subScene);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="700.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<StackPane prefHeight="150.0" prefWidth="200.0">
<children>
<ImageView fitHeight="214.0" fitWidth="169.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../Sprite/Cards/Small/podium-characters-Poseidon.png" />
</image>
</ImageView>
<TextField prefHeight="62.0" prefWidth="274.0" text="APOLLO">
<font>
<Font name="Trebuchet MS Bold Italic" size="22.0" />
</font>
</TextField>
</children>
</StackPane>
</children>
</AnchorPane>
编辑:我发现调整子场景的大小后,可以看到FXML元素。显然它们被子场景“覆盖”了。有没有人知道如何将FXML元素放在子场景的上面而不是像现在这样相反的情况呢?
StackPane
。此外,从您对pane.setCenter(subScene)
的调用中可以推断出pane
是一个BorderPane
。如果是正确的,那么您应该删除对pane.getChildren().add(subScene)
的调用。对于像BorderPane
这样的布局,您不应直接与子元素列表交互。 - SlawHBox
或BorderPane
之一。BorderPane
可能更受欢迎。将Subcene
设置为center
或right
。 - SedJ601