在 JavaFX 中更改场景而不调整大小 window
Change Scene without resize window in JavaFX
我正在尝试更改 JavaFX 上的场景,但不更改 window 大小。但是,当我设置 stage.setScene(scene2);
时,window 大小减小,我想保持两个场景最大化。我试过 stage.setMaximized(true)
在 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 发生并且(我想在这里保持最大化):
在这里只是替换现有场景的根可能比创建一个新场景更好:
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();
}
});
在过去的 4-5 小时里,我一直在处理与您的问题类似的问题,根据@James_D 的回答,我看到了通过场景更改简单地解决调整大小问题的方法。这个答案仍然直接来自@James_D,但我觉得没有关于发生了什么的明确解释,所以我发布这个最重要的是帮助 运行 的任何人这个线程理解为什么@James_D 提供的第二个解决方案(即改变场景,而不是场景的根)有效。
注意:我 运行 回答了几个问题,指出为场景设置新根可能比更改整个场景更好,但这对我来说不起作用,所以更改场景对我来说是最好的选择。
无论如何,我很确定场景在交换时改变大小的原因是因为您没有在场景对象上明确设置场景宽度和高度属性。当新场景加载到舞台时,似乎没有专门设置大小以使场景自动调整到它包含的对象的边界。
基本上,要解决此问题,您需要做的就是在创建场景时设置宽度和高度属性,如下所示。
之前
Scene scene2 = new Scene(root2);
之后
Scene scene2 = new Scene(root2, 900, 600);//or whatever size you want
stage.setScene(scene2);//now we are set if initial scene is 900w X 600h, scene size will stay the same
我正在尝试更改 JavaFX 上的场景,但不更改 window 大小。但是,当我设置 stage.setScene(scene2);
时,window 大小减小,我想保持两个场景最大化。我试过 stage.setMaximized(true)
在 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 发生并且(我想在这里保持最大化):
在这里只是替换现有场景的根可能比创建一个新场景更好:
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();
}
});
在过去的 4-5 小时里,我一直在处理与您的问题类似的问题,根据@James_D 的回答,我看到了通过场景更改简单地解决调整大小问题的方法。这个答案仍然直接来自@James_D,但我觉得没有关于发生了什么的明确解释,所以我发布这个最重要的是帮助 运行 的任何人这个线程理解为什么@James_D 提供的第二个解决方案(即改变场景,而不是场景的根)有效。
注意:我 运行 回答了几个问题,指出为场景设置新根可能比更改整个场景更好,但这对我来说不起作用,所以更改场景对我来说是最好的选择。
无论如何,我很确定场景在交换时改变大小的原因是因为您没有在场景对象上明确设置场景宽度和高度属性。当新场景加载到舞台时,似乎没有专门设置大小以使场景自动调整到它包含的对象的边界。
基本上,要解决此问题,您需要做的就是在创建场景时设置宽度和高度属性,如下所示。
之前
Scene scene2 = new Scene(root2);
之后
Scene scene2 = new Scene(root2, 900, 600);//or whatever size you want
stage.setScene(scene2);//now we are set if initial scene is 900w X 600h, scene size will stay the same