如何从另一个FXML文件调用MediaView

时间:2017-03-13 22:50:07

标签: java javafx fxml

我正在使用JavaFX和FXML文件。

当某些事情发生时,我试图执行第二个FXML文件,这是一个关于在正确的位置获取一些部分的游戏,当你正确的时候,它应该出现一个新窗口并播放视频。我正在使用第二个FXML文件并在答案正确时调用新场景。

这是代码。

public class MainWindowController implements Initializable {

private String ID1 =  "BASURA" , ID2 = "TRASH";
private Boolean First = true;
@FXML private MediaView mv;
private MediaPlayer mp;
private Media me;

@Override
public void initialize(URL location, ResourceBundle resources) {
    String path = new File("src/application/vid.mp4").getAbsolutePath();
    me = new Media(new File(path).toURI().toString());
    mp = new MediaPlayer(me);
    mv.setMediaPlayer(mp);
    mp.setAutoPlay(true);
    DoubleProperty width = mv.fitWidthProperty();
    DoubleProperty height = mv.fitHeightProperty();

    width.bind(Bindings.selectDouble(mv.sceneProperty(), "width "));
    height.bind(Bindings.selectDouble(mv.sceneProperty(), "height"));
}

@FXML public void Verification(ActionEvent event) throws Exception{
    String ButtonClicked = ((Button)event.getSource()).getText();
    if(First){
        if(ButtonClicked.contains("1")){
            ID1 = ButtonClicked;
            First = false;
        }
        else{
            System.out.println("Presionar primero un botón del lado izquierdo");
        }
    }
    else{
        ID2 = ButtonClicked;
    }
    if(ID1.contains(ID2) && !First){
        //Aqui se meterá la nueva ventana con el video de cómo se hace la montura de esa pieza.
        System.out.println("Finjan que muestro un video");

        try {
            Stage primaryStage = new Stage();
            Parent root = FXMLLoader.load(getClass().getResource("/application/Video.fxml"));

                Scene scene = new Scene(root);
                scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                primaryStage.setScene(scene);
                primaryStage.setFullScreen(true);
                primaryStage.show();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        First = true;
    }
    else if(!ID1.contains(ID2) && !First){
        //Ventana de mensaje de que no coinciden los botones INTENTAR DE NUEVO
        System.out.println(ID1);
        System.out.println(ID2);
        System.out.println(First);
        System.out.println("Fallaste rotundamente");
    }
}

我收到了这个错误

javafx.fxml.LoadException:/C:/Users/davidgue/workspace/ComponentsPositionFX/bin/application/MainWindowView.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:15)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at application.MainWindowController.initialize(MainWindowController.java:36)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more

我解决了这个问题,当我设置MediaPlayer(mp)时。 mp只存在于名为Video的第二个FXML文件中,当我在主FXML中创建一个MediaView并调用它时,一切正常,但我希望它在第二个FXML文件/窗口中。那么,问题是如何引用仅存在于第二个FXML文件中的MediaView?

0 个答案:

没有答案