繁体   English   中英

从现有控制器加载的场景与中心不对齐-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小气,所以重要的修改是在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不会在场景之外显示任何内容,因此您将左下角的圆与四分之一相对应。

要在场景的左上角完全显示场景中的圆圈,应将圆圈放置在可以进行某些布局的布局中。 例如AnchorPanetopAnchorleftAnchor的圈子会的工作,但实现所期望的结果的最简单的方法设置属性是使用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM