图像FXML未在JavaFX中打开

图像FXML未在JavaFX中打开,java,javafx,javafx-2,fxml,fxmlloader,Java,Javafx,Javafx 2,Fxml,Fxmlloader,当我尝试加载两个不同的场景时,它不会打开图像FXML,基本上我希望先打开图像FXML,然后打开欢迎屏幕。有些图像场景是如何不显示的 Main.java public class Main extends Application { @Override public void start(Stage stage) throws Exception { System.out.println("im in main"); Parent root = FXMLLoader.load(g

当我尝试加载两个不同的场景时,它不会打开图像FXML,基本上我希望先打开图像FXML,然后打开欢迎屏幕。有些图像场景是如何不显示的

Main.java

public class Main extends Application {

@Override
public void start(Stage stage) throws Exception {
    System.out.println("im in main");
    Parent root = FXMLLoader.load(getClass().getResource("ImageScreen.fxml"));


    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.setTitle("test");
    stage.show();
}
public static void main(String[] args) {
    launch(args);
}
public class WelcomeFXMLController implements Initializable {

/**
 * Initializes the controller class.
 *
 */

@FXML
private AnchorPane welcomepane;

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}
}

ImageScreen.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>

<StackPane fx:id="imagepane" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.102"        xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.ImageScreenController">
<children>
    <ImageView fitHeight="438.0" fitWidth="603.0" pickOnBounds="true"
        preserveRatio="true">
        <image>
            <Image url="@bamboo-fountain-and-zen-stone.jpg" />
        </image>
    </ImageView>
    <Pane prefHeight="200.0" prefWidth="200.0" />
</children>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>


<AnchorPane fx:id="welcomepane" maxHeight="-Infinity"
maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.102"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.WelcomeFXMLController">
<children>
    <Text layoutX="212.0" layoutY="100.0" strokeType="OUTSIDE"
        strokeWidth="0.0" text="Welcome" wrappingWidth="176.13671875">
        <font>
            <Font size="35.0" />
        </font>
    </Text>
</children>
}

WelcomeFXMLDoc.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>

<StackPane fx:id="imagepane" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.102"        xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.ImageScreenController">
<children>
    <ImageView fitHeight="438.0" fitWidth="603.0" pickOnBounds="true"
        preserveRatio="true">
        <image>
            <Image url="@bamboo-fountain-and-zen-stone.jpg" />
        </image>
    </ImageView>
    <Pane prefHeight="200.0" prefWidth="200.0" />
</children>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>


<AnchorPane fx:id="welcomepane" maxHeight="-Infinity"
maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.102"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.WelcomeFXMLController">
<children>
    <Text layoutX="212.0" layoutY="100.0" strokeType="OUTSIDE"
        strokeWidth="0.0" text="Welcome" wrappingWidth="176.13671875">
        <font>
            <Font size="35.0" />
        </font>
    </Text>
</children>

}

ImageScreenController
中使用
imagepane.getChildren().setAll(pane)
setAll()
清除集合,然后添加新元素。在这种情况下,从场景图中删除
ImageView
,并仅显示
welcomepane

使用
imagepane.getChildren().add(窗格)
取而代之,然后在不再需要is时删除
welcomepane

编辑

根据你的评论

使用
PauseTransition
等待
WelcomeFXMLController

@Override
public void initialize(URL url, ResourceBundle rb) {
    PauseTransition pt = new PauseTransition(Duration.seconds(10));
    pt.setOnFinished(e -> {
        AnchorPane pane;
        try {
            System.out.println("im in controller");
            pane = FXMLLoader.load(getClass().getResource("WelcomeFXMLDoc.fxml"));
            imagepane.getChildren().setAll(pane);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    });
    pt.play();
}

如果我采用这种方法,欢迎场景将覆盖在我不想要的图像场景之上,我希望一旦图像场景完全加载,图像场景应该消失,并且应该单独加载欢迎场景,另外,如果我在图像场景中添加任何按钮,并且单击该按钮,它将打开欢迎场景,但我不想在图像场景中添加按钮。加载图像场景时,您是否有一些长时间运行的过程?因为在提供的代码中,您的图像屏幕将立即显示,所以没有显示它的意义。我想先加载图像等待10秒,然后加载欢迎场景。上面没有提供等待代码。。如果可以的话,会有帮助的full@user2459816你不认为你应该在问题中加入诸如“我希望代码实际做什么”之类的信息吗?@James\u D:请仔细阅读问题描述,“基本上我希望先打开图像FXML,然后打开欢迎屏幕。一些图像场景是如何不显示的”,你还期待什么吗?