簡體   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