在javafx中重新加载场景时,“最大化”不起作用
请帮帮我。 我有以下代码在javafx中重新加载场景时,“最大化”不起作用,java,javafx,Java,Javafx,请帮帮我。 我有以下代码 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 { s
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)
没有工作,我的窗口没有显示为Maximized
为什么
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();
}
您的FXMLDocument看起来如何?