从现有控制器加载的场景与中心不对齐 - JavaFX

Scene loaded from existing controller not align with center - JavaFX

我正在编写一个包含两个不同场景的程序,每个场景都使用 FXML 文件设计,并且每个场景都有自己的控制器。我的初始场景工作正常,它出现在屏幕中央,但是当我尝试从第一个场景的控制器加载第二个场景时,我发现它没有像其他场景一样出现在屏幕中央有一个。

谁能帮帮我?吓死我了! 这是一些代码:

@Override
public void start(Stage primaryStage) {
    try {
        //Create an FXMLLoader
        FXMLLoader rootLoader = new FXMLLoader(getClass().getResource("MainScene.fxml"));

        //Instantiate a new controller with parameter and sets it to the FXMLLoader
        MainController mainController = new MainController(primaryStage);
        rootLoader.setController(mainController);

        //Sets the layout
        Group root = rootLoader.load();
        Scene startScene = new Scene(root);

        //Sets the stage's scene
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.setScene(startScene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

这是主要的。

public void initialize() {

    FXMLLoader playSceneLoader = new FXMLLoader(getClass().getResource("PlayScene.fxml"));

    try {
        Group playSceneLayout = playSceneLoader.load();
        playScene = new Scene(playSceneLayout, Screen.getPrimary().getBounds().getMaxX(), Screen.getPrimary().getBounds().getMaxY());
        playScene.setFill(Color.TRANSPARENT);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

这是创建程序初始场景的第一个控制器。

    <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.Group?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>


<Group xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <GridPane prefHeight="300.0" prefWidth="500.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #A47888#A47888;">
               <children>
                  <Label fx:id="playLabel" layoutX="90.0" layoutY="129.0" onMouseClicked="#startLabelClicked" text="Play" textFill="#77354c">
                     <font>
                        <Font name="AvidOmnes Light" size="41.0" />
                     </font>
                  </Label>
               </children>
            </AnchorPane>
            <AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #77354C#77354C;" GridPane.columnIndex="1">
               <children>
                  <Label fx:id="titleLabel" layoutX="77.0" layoutY="129.0" text="Titolo" textFill="#a47888">
                     <font>
                        <Font name="AvidOmnes Light" size="40.0" />
                     </font>
                  </Label>
                  <Label fx:id="closeLabel" layoutX="232.0" layoutY="6.0" onMouseClicked="#closeRoot" text="X" textFill="#a47888" />
               </children>
            </AnchorPane>
         </children>
      </GridPane>
   </children>
</Group>

这是初始页面的FXML文件。

    <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.Group?>
<?import javafx.scene.shape.Circle?>


<Group xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.PlayController">
   <children>
      <Circle fx:id="startCircle" fill="DODGERBLUE" onMouseClicked="#startCircleClicked" radius="100.0" stroke="BLACK" strokeType="INSIDE" />
   </children>
</Group>

这是第二个场景

您只得到 Circle 的一部分是绝对正常的,因为您没有定义中心坐标,然后默认位于 Group 的位置 (0, 0)。

为了在屏幕的左上角看到它,您必须在单击 Label 后立即最大化 Stage

这里有一个例子:(因为你对我即兴创作的代码的信息很吝啬,所以重要的修改在startLabelClicked

public class MainController {

    private Stage mainStage;
    private Scene playScene;

    public MainController(Stage pMainStage) {
        mainStage = pMainStage;
    }

    @FXML
    public void initialize() {

        FXMLLoader playSceneLoader = new FXMLLoader(getClass().getResource("PlayScene.fxml"));

        try {
            Parent playSceneLayout = playSceneLoader.load();
            playScene = new Scene(playSceneLayout, Screen.getPrimary().getBounds().getMaxX(), Screen.getPrimary().getBounds().getMaxY());
            playScene.setFill(Color.TRANSPARENT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    private void startLabelClicked() {
        mainStage.setScene(playScene);
        // This line will help you to extend your stage, which contains your scene.
        mainStage.setMaximized(true);
    }

    @FXML
    private void closeRoot() {
        System.out.println("Closing.");
        Platform.exit();
    }
}

在不调整圆的xy属性的情况下,圆心位于场景的左上角。由于 javafx 不显示场景之外的任何内容,因此您只剩下右下角的四分之一圆圈。

要在场景的左上角完整地显示一个圆圈,你应该把圆圈放在一个做一些布局的布局中。例如。 AnchorPane 为圆设置 topAnchorleftAnchor 属性可以工作,但实现所需结果的最简单方法是使用左上对齐的 StackPane

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.shape.Circle?>

<StackPane alignment="TOP_LEFT" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.PlayController">
   <children>
      <Circle fx:id="startCircle" fill="DODGERBLUE" onMouseClicked="#startCircleClicked" radius="100.0" stroke="BLACK" strokeType="INSIDE" />
   </children>
</StackPane>