JavaFX元素初始化为0

时间:2016-10-21 07:33:04

标签: css javafx fxml

当我的FXML被初始化并且他们的值被听众监听时,我遇到了问题。

所以我的主窗口是BorderPane

  • TOP = menu.fxml
  • CENTER = center1.fxml
  • BOTTOM = footer.fxml

当我点击center1Controller上的其中一个按钮时,CENTER fxml应更改为center2.fxml

center2.fxml我有一个Pane Anchor.anchorLeft=60anchorTop = anchorBottom

我被创建了一个监听器:

public void addTopOffsetListener() {

    this.logonWindowPane.heightProperty().addListener(new ChangeListener<Number>() {            
        @Override
        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
            double y = (newValue.doubleValue() - logonForm.getHeight()) / 2;
            logonForm.setLayoutY(y);  
        }
    });
}

但是我的屏幕重新加载logonForm是不好的地方。当我调试时,初始化getHeight()返回0.

调整窗口大小后将其放置在正确的位置。

我在FXML和CSS上尝试修改此元素的大小。

其实我找到了解决方法。当我初始化screen2Controller时,我添加了这一行

public void initialize(URL url, ResourceBundle rb) {
  this.logonForm.resize(300, 400);
}

有我的Screen2 fxml:

<AnchorPane id="logonWindowPane" fx:id="logonWindowPane" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="genealogytree.application.fxmlcontrollers.LogonWindowController">
    <stylesheets>
        <URL value="@../styles/application_logon.css" />
    </stylesheets>
   <children>
        <Pane id="logonForm" fx:id="logonForm" prefHeight="400" prefWidth="300" minWidth="300" minHeight="400"   styleClass="projectPane"   AnchorPane.rightAnchor="60.0" />
   </children>  
</AnchorPane>

我在logonForm中放置了元素的FXML:

<Pane styleClass="logonFXForm" prefHeight="400" prefWidth="300" minWidth="300" minHeight="400"     xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="genealogytree.application.fxmlcontrollers.LogonFormController">
    <stylesheets>
        <URL value="@../styles/application_logon.css" />
    </stylesheets>
    <children>
      <JFXPasswordField focusColor="#1f7f6a" layoutX="38.0" layoutY="133.0" maxWidth="224.0" minWidth="224.0" prefHeight="48.0" prefWidth="224.0" promptText="Password" unFocusColor="#253930">
         <font>
            <Font size="14.0" />
         </font>
      </JFXPasswordField>
      <JFXTextField focusColor="#1f7f6a" layoutX="38.0" layoutY="54.0" maxWidth="224.0" minWidth="224.0" prefHeight="48.0" prefWidth="224.0" promptText="Login" unFocusColor="#253930">
         <font>
            <Font size="14.0" />
         </font>
      </JFXTextField>
      <JFXButton buttonType="RAISED" graphicTextGap="8.0" layoutX="61.0" layoutY="224.0" prefHeight="48.0" prefWidth="178.0" ripplerFill="#32ae8f" styleClass="jfxButton" text="CONNECT" textAlignment="CENTER">
         <font>
            <Font name="System Bold" size="18.0" />
         </font>
      </JFXButton>
      <Hyperlink layoutX="38.0" layoutY="305.0" prefHeight="25.0" prefWidth="250.0" text="Do not have an Account ? " />
      <Hyperlink layoutX="38.0" layoutY="340.0" prefHeight="25.0" prefWidth="250.0" text="Forgotten Password ? " />

   </children>
</Pane>

但是还有另一种方法可以解决我在FXML或CSS中的问题吗?

0 个答案:

没有答案