在JavaFX中更改场景而不调整窗口大小(Change Scene without resize window in JavaFX)

编程入门 行业动态 更新时间:2024-10-25 05:12:08
在JavaFX中更改场景而不调整窗口大小(Change Scene without resize window in JavaFX)

我正在尝试更改JavaFX上的场景,但不更改窗口大小。 但是,当我设置stage.setScene(scene2); 窗口大小减少,我想保持两个场景最大化。 我试过在stage.setScene(scene2);之后stage.setScene(scene2); 但结果是一样的。

我该如何解决它?

我的代码:

package controller; import java.io.IOException; import javafx.animation.FadeTransition; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.util.Duration; public class App extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("../view/fxml/Loading.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.setTitle("Project"); stage.setMaximized(true); stage.show(); FadeTransition fadeIn = new FadeTransition(Duration.seconds(1), root); fadeIn.setFromValue(0); fadeIn.setToValue(1); FadeTransition fadeOut = new FadeTransition(Duration.seconds(1), root); fadeOut.setFromValue(1); fadeOut.setToValue(0); fadeIn.play(); fadeIn.setOnFinished((e) -> { fadeOut.play(); }); fadeOut.setOnFinished((e) -> { try { Parent root2 = FXMLLoader.load(getClass().getResource("../view/fxml/Welcome.fxml")); Scene scene2 = new Scene(root2); stage.setScene(scene2); } catch (IOException e1) { e1.printStackTrace(); } }); } public static void main(String[] args) { launch(args); } }

当我编译时:

然后发生fadeout / fadeIn和(这是我想保持最大化):

I'm trying to change Scenes on JavaFX, but without change the window size. However, when I set stage.setScene(scene2); the window size decreases, and I want to keep the both Scenes maximized. I've tried to stage.setMaximized(true) after the stage.setScene(scene2); but the result is the same.

How can I fix it?

My code:

package controller; import java.io.IOException; import javafx.animation.FadeTransition; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.util.Duration; public class App extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("../view/fxml/Loading.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.setTitle("Project"); stage.setMaximized(true); stage.show(); FadeTransition fadeIn = new FadeTransition(Duration.seconds(1), root); fadeIn.setFromValue(0); fadeIn.setToValue(1); FadeTransition fadeOut = new FadeTransition(Duration.seconds(1), root); fadeOut.setFromValue(1); fadeOut.setToValue(0); fadeIn.play(); fadeIn.setOnFinished((e) -> { fadeOut.play(); }); fadeOut.setOnFinished((e) -> { try { Parent root2 = FXMLLoader.load(getClass().getResource("../view/fxml/Welcome.fxml")); Scene scene2 = new Scene(root2); stage.setScene(scene2); } catch (IOException e1) { e1.printStackTrace(); } }); } public static void main(String[] args) { launch(args); } }

When I compile:

Then the fadeOut/fadeIn occurs and (It is here that I want to keep maximized):

最满意答案

在这里更换现有场景的根可能更好,而不是创建一个新场景:

fadeOut.setOnFinished((e) -> { try { Parent root2 = FXMLLoader.load(getClass().getResource("../view/fxml/Welcome.fxml")); scene.setRoot(root2); } catch (IOException e1) { e1.printStackTrace(); } });

如果您确实需要更换场景,出于某种原因,您可以将新场景的大小设置为与现有场景相同:

fadeOut.setOnFinished((e) -> { try { Parent root2 = FXMLLoader.load(getClass().getResource("../view/fxml/Welcome.fxml")); Scene scene2 = new Scene(root2, scene.getWidth(), scene.getHeight()); stage.setScene(scene2); } catch (IOException e1) { e1.printStackTrace(); } });

It's probably better here just to replace the root of the existing scene, than to create a new scene:

fadeOut.setOnFinished((e) -> { try { Parent root2 = FXMLLoader.load(getClass().getResource("../view/fxml/Welcome.fxml")); scene.setRoot(root2); } catch (IOException e1) { e1.printStackTrace(); } });

If you really do need to replace the scene, for some reason, you can set the new scene's size to the same as the existing scene:

fadeOut.setOnFinished((e) -> { try { Parent root2 = FXMLLoader.load(getClass().getResource("../view/fxml/Welcome.fxml")); Scene scene2 = new Scene(root2, scene.getWidth(), scene.getHeight()); stage.setScene(scene2); } catch (IOException e1) { e1.printStackTrace(); } });

更多推荐

本文发布于:2023-08-07 19:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466012.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   场景   大小   窗口   JavaFX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!