代码之家  ›  专栏  ›  技术社区  ›  neranjan tennekoon

当切换javaFX场景时,stage的大小正在最小化

  •  1
  • neranjan tennekoon  · 技术社区  · 4 周前

    我已将场景添加到 primaryStage 在javaFx项目中,在其Main方法中。同样在同一主类中,舞台是 primaryStage.setMaximized(true); 然后通过单击与该场景相关的fxml代码中的按钮进行准备,舞台上的现有场景将被更改。

    问题是,当场景发生变化时,舞台会自动最小化。我希望保持舞台的默认大小最大化。

    这是我项目的起始主课

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    import java.io.IOException;
    
    public class Main extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
    
        @Override
        public void start(Stage primaryStage) throws IOException {
            Parent parent = FXMLLoader.load(getClass().getResource("MainScene.fxml"));
            Scene scene = new Scene(parent);
            primaryStage.setScene(scene);
            primaryStage.setMaximized(true);
            primaryStage.show();
        }
    }
    

    以下是贯穿主类的MainScene.fxml代码

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.text.Text?>
    
    
    <AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MainSceneConn">
       <children>
          <Text layoutX="289.0" layoutY="196.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Form" />
          <Button fx:id="button" layoutX="236.0" layoutY="213.0" mnemonicParsing="false" onAction="#loadNewScene" prefHeight="25.0" prefWidth="134.0" text="New Scene" />
       </children>
    </AnchorPane>
    

    这是MainScene.fxml文件的控制器类。

    import javafx.event.ActionEvent;
    import javafx.fxml.FXMLLoader;
    import javafx.fxml.Initializable;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.stage.Stage;
    
    import java.io.IOException;
    import java.net.URL;
    import java.util.ResourceBundle;
    
    public class MainSceneConn {
        public Button button;
    
        public void loadNewScene(ActionEvent actionEvent) throws IOException {
    
            Stage stage = (Stage) button.getScene().getWindow();
    
            stage.setMaximized(true);//this line is not work :(
    
            Parent parent = FXMLLoader.load(getClass().getResource("secondScene.fxml"));
            Scene scene = new Scene(parent);
            stage.setScene(scene);
        }
    }
    

    以下是fxml文件,该文件通过控制器类包含场景

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import java.lang.*?>
    <?import java.util.*?>
    <?import javafx.scene.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    
    <AnchorPane xmlns="http://javafx.com/javafx"
                xmlns:fx="http://javafx.com/fxml"
                prefHeight="400.0" prefWidth="600.0">
    
    </AnchorPane>
    

    在我的主要方法之外,再次在 MainScene.fxml 控制器类 stage.setMaximized(true); 准备好了但也没什么不同。

    1 回复  |  直到 4 周前
        1
  •  1
  •   jewelsea    4 周前

    我不知道为什么当你在舞台上设置一个新的场景时,舞台的最大化状态会发生变化。

    您可以更改现有场景的根,而不是创建新场景,然后窗口的最大化状态将保持不变。

    import javafx.event.ActionEvent;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    
    import java.io.IOException;
    
    public class MainSceneConn {
        public Button button;
    
        public void loadNewScene(ActionEvent actionEvent) throws IOException {
            Parent parent = FXMLLoader.load(getClass().getResource("secondScene.fxml"));
    
            Scene scene = button.getScene();
            scene.setRoot(parent);
        }
    }
    

    这个 setScene 文件说明:

    如果应用程序从未设置此窗口的宽度或高度,则设置场景将导致此窗口从该场景获取其宽度或高度。最终用户调整此窗口的大小不算作应用程序设置的宽度或高度。

    我只是猜测,在代码中最大化窗口也不算作设置窗口的宽度或高度,而且在这种情况下,设置场景将“导致此窗口从该场景中获取其宽度或高度”,本质上恢复最大化设置。不过这是一种奇怪的行为。

    考虑一下,与其将窗口设置为最大化,不如将其设置为 full screen 。对于一些可能更好的应用程序,但对于其他应用程序,可能不会更好。