javafx Maximized中的重新加载场景不起作用时

请帮我.
我有以下代码.

public class Main extends Application {

    private static Locale locale = new Locale("de", "DE");
    private Scene scene;
    public static Stage stage;

@Override
    public void start(Stage primaryStage) throws Exception {
        stage = primaryStage;

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
        ResourceBundle bundle = ResourceBundle.getBundle("bundles.lang", locale);
        fxmlLoader.setResources(bundle);

        Parent root = fxmlLoader.load();

        scene = new Scene(root);
        stage.setMaximized(true);
        stage.setScene(scene);

            stage.show();
}
    public void reload() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
        fxmlLoader.setResources(ResourceBundle.getBundle("bundles.lang", locale));

        Parent root = fxmlLoader.load();

        scene = new Scene(root);
        stage.setMaximized(true);
        stage.setScene(scene);

        stage.show();

    }
}

在我的控制器类

public class FXMLDocumentController implements Initializable {
@FXML
    AnchorPane root;

    @FXML
    private void handleChinese(final ActionEvent event) throws IOException {

        Main.setLocale(new Locale("zh", "CN")); // change to english
        //JavaFXApplication4.stage.close();
        Main reload = new Main();
        reload.reload();
    }

    @FXML
    private void handleRussian(final ActionEvent event) throws IOException {

        Main.setLocale(new Locale("de", "DE")); // change to english
        Main reload = new Main();
        reload.reload();
    }

它有效!但是当更改语言时,我的窗口无法正确显示,这意味着stage.setMaximized(true)不起作用,我的窗口也未显示为最大化.

为什么stage.setMaximized(true)无法正常工作?

解决方法:

无需创建新场景,只需将root设置为上一个场景即可.

public void reload() throws IOException {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml")); 
    Parent root = fxmlLoader.load();
    stage.getScene().setRoot(root);
//        Scene scene = new Scene(root);
//        stage.setMaximized(true);
//        stage.setScene(scene);
//
//        stage.show();
}
上一篇:更改JavaFX MediaPlayer的播放速度


下一篇:Idea+Maven部署打包JavaFX项目遇到的坑