我有一个基本的JavaFX应用程序,就这样打开
public class MyApplication extends Application {
private Stage stage;
public static void main(String[] args) {
Console.setDebug();
launch(args);
}
@Override
public void start(Stage primaryStage) {
// set stage as primary
stage = primaryStage;
stage.setFullscreen(true);
stage.show();
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
public void handle(WindowEvent we) {
System.out.println("Closing application...");
}
});
}
}
现在改变屏幕我有这个功能,我打电话给
private void replaceScene(String resource, IControlledScreen controller) {
try {
controller.setApp(this);
FXMLLoader loader = new FXMLLoader(getClass().getResource(resource));
loader.setController(controller);
Pane screen = (Pane) loader.load();
Scene scene = new Scene(screen);
stage.setScene(scene);
stage.setFullscreen(true);
} catch (Exception e) {
System.out.println("Cannot load resource " + resource);
System.out.println(e.getMessage());
}
}
示例调用将是
public void goToHome() {
replaceScene("/fxml/HomeView.fxml", new HomeController());
}
现在当我运行应用程序时,第一个屏幕处于全屏模式,然后当我更改屏幕时,屏幕调整为窗口大小,然后再次更改为全屏???我试过添加
stage.setFullscreen(false);
在我打电话之前
stage.setFullscreen(true);
但这也有效。如何在不调整大小的情况下更改屏幕/场景?
也可以使用代码切换全屏模式,如果我希望用户能够选择全屏模式,可以这样做吗?
答案 0 :(得分:2)
这是解决方案:
private void replaceScene(String resource, IControlledScreen controller) {
try {
controller.setApp(this);
FXMLLoader loader = new FXMLLoader(getClass().getResource(resource));
loader.setController(controller);
Pane screen = (Pane) loader.load();
stage.getScene().setRoot(screen);
} catch (Exception e) {
System.out.println("Cannot load resource " + resource);
System.out.println(e.getMessage());
}
}
答案 1 :(得分:0)
感谢Mailkov的代码,几乎工作,但不得不稍微改变,以应对第一次屏幕加载。现在使用
private void replaceScene(String resource, IControlledScreen controller) {
try {
controller.setApp(this);
FXMLLoader loader = new FXMLLoader(getClass().getResource(resource));
loader.setController(controller);
Pane screen = (Pane) loader.load();
if (stage.getScene() == null) {
Scene scene = new Scene(screen);
stage.setScene(scene);
} else {
stage.getScene().setRoot(screen);
}
} catch (Exception e) {
System.out.println("Cannot load resource " + resource);
System.out.println(e.getMessage());
}
}