Javafx:在应用程序中创建新阶段

时间:2016-01-28 10:49:44

标签: java user-interface javafx stage

我需要一些实际上相当简单的代码(我认为)来帮助工作。我有一个JFrame启动一个应用程序来播放视频(每次两次,不同的视频),然后显示几个jButton来回答视频问题。我的问题是我得到了第一个要播放的视频,但后来我无法创建一个新的舞台。也许你可以帮助我。这是我在面板中的代码:

public void ausführen(int i, int antwort){

    switch (antwort) {
        case 0:
            //Question or Video                
               switch (i) {
                case 0:
                    //Film1
                    mp = new MediaPanel(); //this is the application
                    mp.run();
                    setVisible(false);
//this timer waits for the video to finish
                    task = new java.util.TimerTask(){
                    @Override
                    public void run(){
                        setVisible(true);                            
                        ausführen(1,0);                            

                    }
                    };
                    t = new java.util.Timer();
                    t.schedule(task, 7000);                                                          
                    break;
                case 1:       //Film2                 
                    mp.restart(new Stage(),"other video");
                    setVisible(false);                        
                    task = new java.util.TimerTask(){
                    @Override
                    public void run(){
                        setVisible(true);                            
                        ausführen(2,0);                                                           
                        dispose();                            
                    }
                    };
                    t = new java.util.Timer();
                    t.schedule(task, 7000);
//next case opens a jPanel with jButtons with the questions (works)

这是MediaPanel(应用程序)中的代码:

public class MediaPanel extends Application implements Runnable
{

    private String film = "video1";

    public void setFilm(String f){
        film = f;
    }   

        @Override
    public void run(){
        launch();
    }    
    public void start( Stage stage ) throws Exception
    {
        Group root = new Group();
        stage.setTitle("MoviePlayer");
        Media media = new Media("video1");
        MediaPlayer player = new MediaPlayer(media);
        MediaView view = new MediaView(player);

        view.setPreserveRatio(true);

        root.getChildren().add(view);
        Scene scene = new Scene(root, media.getWidth(), media.getHeight(), Color.BLACK);

        stage.setScene(scene);
        stage.show();
        stage.setFullScreen(false);

        player.play();

        java.util.TimerTask task = new java.util.TimerTask(){
            @Override
            public void run(){
                System.out.print("Still running"); //I wanted to see if this method is called
                Platform.exit();
                }                                              
            };

        java.util.Timer t = new java.util.Timer();
        t.schedule(task, 5000);

    }

    public void restart( Stage stage, String film ) //I created this method to avoid calling launch() twice
    {
        System.out.print("restart");
        Group root = new Group();
        stage.setTitle("MoviePlayer");
        Media media = new Media(film);
        MediaPlayer player = new MediaPlayer(media);
        MediaView view = new MediaView(player);


        view.setPreserveRatio(true);

        root.getChildren().add(view);
        Scene scene = new Scene(root, media.getWidth(), media.getHeight(), Color.BLACK);

        stage.setScene(scene);
        stage.show();
        stage.setFullScreen(true);

        player.play();

        java.util.TimerTask task = new java.util.TimerTask(){
            @Override
            public void run(){
                Platform.exit();
                }                                              
            };

        java.util.Timer t = new java.util.Timer();
        t.schedule(task, 5000);             
    }


    }

我得到错误:“线程中的异常”Timer-2“java.lang.IllegalStateException:不在FX应用程序线程上; currentThread = Timer-2”。 Alternativley我在MediaWindow中创建了一个新的MediaPanel,但这不起作用,因为一个人可能不会多次调用launch()。

我知道我的代码不是很好,但这只是一个非常简单的GUI /应用程序。

我希望你们其中一个人可以帮助我!提前谢谢!

0 个答案:

没有答案