Image FXML未在JavaFX中打开

时间:2017-02-17 12:11:18

标签: 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(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);
}

}

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>

ImageScreenController

public class ImageScreenController implements Initializable {

@FXML
private StackPane imagepane;

public static AnchorPane welcomepane;

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

    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();
    }

}

}

WelcomeFXMLDoc.fxml

<?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>

WelcomeFXMLController.java

public class WelcomeFXMLController implements Initializable {

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

@FXML
private AnchorPane welcomepane;

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

}

1 个答案:

答案 0 :(得分:1)

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

改为使用imagepane.getChildren().add(pane);,然后在您不再需要时删除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();
}