我在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);
内准备为但它没有什么不同.