[英]JavaFX Changing Scene Instead of Stage
I'm currently making a program and using JavaFX as the main GUI setting. 我正在制作一个程序并使用JavaFX作为主要的GUI设置。 Currently I'm opening and closing each stage as I go into each different method. 当我进入每个不同的方法时,我正在打开和关闭每个阶段。
This is a little bit annoying as to the user there are a lot of screens opening and closing when they go through the program. 这有点令人讨厌,因为用户在完成程序时会有很多屏幕打开和关闭。
My question is this, is there a way to instead of opening a stage every time I can move into a different part of my program, I could switch between scenes instead? 我的问题是,有没有办法,每次我可以进入我的程序的不同部分而不是打开一个阶段,我可以在场景之间切换? Would switching between scenes be the correct implementation in this scenario? 在这种情况下,场景之间切换是否正确? Would I need to pass a stage between methods? 我需要在方法之间传递一个阶段吗?
As you can probably tell I'm just getting started with JavaFX so would appreciate a bit of a lesson. 你可以告诉我,我刚刚开始使用JavaFX,所以我会非常感激一点教训。
EDIT: I have decided to update my application using JavaFXML, However I am having difficulties passing a scene into my controller. 编辑:我已经决定使用JavaFXML更新我的应用程序,但是我很难将一个场景传递给我的控制器。 Here is my Controller; 这是我的控制器;
public class MainApp extends Application {
@FXML
public Stage primaryStage;
@FXML
private AnchorPane rootLayout;
@FXML
private JobInterface jInterface;
@Override
public void start(Stage primaryStage) {
primaryStage = new Stage();
setPrimaryStage(primaryStage);
initRootLayout();
}
@FXML
public void initRootLayout(){
try {
primaryStage = getPrimaryStage();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("MainInterface.fxml"));
rootLayout = (AnchorPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
setPrimaryStage(primaryStage);
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
private void setJobLayout(){
primaryStage = getPrimaryStage();
jInterface = new JobInterface();
jInterface.initJobLayout();
primaryStage.setScene(jInterface.getScene());
}
public static void main(String[] args) {
launch(args);
}
public Stage getPrimaryStage() {
return primaryStage;
}
public void setPrimaryStage(Stage primaryStage) {
this.primaryStage = primaryStage;
}
}
Here is a method that is changing the scene using a different FXML file and attempting to pass the scene back to the controller; 这是一种使用不同的FXML文件更改场景并尝试将场景传递回控制器的方法;
public class JobInterface {
private AnchorPane rootLayout;
private Scene scene;
public void initJobLayout(){
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("JobInterface.fxml"));
rootLayout = (AnchorPane) loader.load();
scene = new Scene(rootLayout);
setScene(scene);
} catch (IOException e) {
e.printStackTrace();
}
}
public Scene getScene() {
return scene;
}
public void setScene(Scene scene) {
this.scene = scene;
}
}
The issue I'm having now is a NullPointerException on this line in the main app; 我现在遇到的问题是主应用程序中此行的NullPointerException;
primaryStage.setScene(jInterface.getScene());
Any ideas on where I am going wrong? 关于我哪里出错的任何想法?
Non-Animated 非动画
For changing scene without Animation tutorials: http://docs.oracle.com/javafx/2/api/javafx/stage/Stage.html#setScene%28javafx.scene.Scene%29 http://docs.oracle.com/javafx/2/api/javafx/scene/layout/Pane.html#getChildren%28%29 无需动画教程即可更改场景: http : //docs.oracle.com/javafx/2/api/javafx/stage/Stage.html#setScene%28javafx.scene.Scene%29 http://docs.oracle.com/ JavaFX的/ 2 / API / JavaFX的/场景/布局/ Pane.html#的getChildren%28%29
Animated 动画
For changing scene with Animation tutorials: https://blogs.oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx1 https://blogs.oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx 使用动画教程更改场景: https : //blogs.oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx1 https://blogs.oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.