我在javaFx项目的Main方法中添加了一个场景到primaryStage.同样在同一个主类中,stage是primaryStage.setMaximized(true); Prepared,然后通过单击与该场景相关的fxml代码中的按钮,stage上的现有场景将被更改.

问题是stage是自动最小化时,场景改变.我希望保持stage的默认大小最大化.

下面是我项目的开始主类

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();
    }
}

下面是贯穿main类的MainScen.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>

下面是MainScen.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);内准备为但它没有什么不同.

推荐答案

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

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

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会更好. 对于某些应用程序来说,这可能会更好,但对于其他应用程序,它可能不会更好.

Java相关问答推荐

如何在Docker容器中使用wireock—Webhooks阻止请求?

Springdoc Whitelabel Error Page with Spring V3

在现代操作系统/硬件上按块访问数据值得吗?

Hibernate EmptyInterceptor可以工作,但不能拦截器

Helidon 4和Http API

如何从JNI方法正确调用NSOpenPanel以在正确的线程上运行?

与不同顺序的组进行匹配,不重复组但分开

Spring Security不允许加载js

如何只修改父类ChroniclerView位置0处的第一个嵌套ChroniclerView(child)元素?

如何在JavaFX中制作鼠标透明stage

使用正则表达式从字符串中提取多个值

Java堆中的许多java.time.ZoneRegion实例.ZoneId实例不应该被缓存吗?

如何创建模块信息类文件并将其添加到JAR中?

在使用具有不同成本的谓词调用allMatch之前对Java流进行排序会带来什么好处吗?

循环不起作用只有第一个元素重复

协同 routine 似乎并不比JVM线程占用更少的资源

使用同步方法中的新线程调用同步方法

Java递归泛型是否可以被视为继承和重写的语法糖

双对象供应商

类型安全:从 JSONArray 到 ArrayList> 的未经判断的转换