Javafx:在应用程序中创建新阶段
我需要一些帮助和一些实际上相当简单的代码(我认为)的工作。我有一个JFrame,它启动一个应用程序来播放一个视频(两次,每次不同的视频),然后显示几个JButton来回答视频中的问题。我的问题是我得到了第一个视频,但是我不能创造一个新的舞台。也许你能帮我。这是我在面板中的代码:Javafx:在应用程序中创建新阶段,java,user-interface,javafx,stage,Java,User Interface,Javafx,Stage,我需要一些帮助和一些实际上相当简单的代码(我认为)的工作。我有一个JFrame,它启动一个应用程序来播放一个视频(两次,每次不同的视频),然后显示几个JButton来回答视频中的问题。我的问题是我得到了第一个视频,但是我不能创造一个新的舞台。也许你能帮我。这是我在面板中的代码: public void ausführen(int i, int antwort){ switch (antwort) { case 0: //Question or V
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(应用程序)中的代码:
我得到错误:“线程中的异常”Timer-2“java.lang.IllegalStateException:不在FX应用程序线程上;currentThread=Timer-2”。
Alternativley我在MediaWindow中创建了一个新的MediaPanel,但这也不起作用,因为人们可能不会多次调用launch()
我知道我的代码不是很好,但这应该是一个非常简单的GUI/应用程序
我希望你们中的一个能帮助我!提前谢谢你 主要问题的可能重复似乎是对javafx应用程序生命周期的误解。您不应该自己创建
应用程序
类的实例。让Application.launch
这样做。如果您在Swing应用程序中嵌入JavaFX,那么您根本不应该有Application
类。有关在Swing中使用JavaFX的示例,请参见。非常好,感谢您的帮助!
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);
}
}