javafx - 在MenuButton上执行setText的问题

时间:2016-12-08 05:19:15

标签: javafx fxml

所以我在这里有这个代码:

// import statements are ommitted

public class HomePage implements Initializable{


@FXML
private volatile MenuButton fullUserName;

public void initialize(URL url, ResourceBundle resourceBundle)
{

}

public void loadHomePage(Stage primaryStage) throws IOException {
    Parent HomePage = FXMLLoader.load(LoginScreen.class.getResource("HomePage.fxml"));

    Timer animTimer = new Timer();
    animTimer.scheduleAtFixedRate(new TimerTask() {
        int i=0;

        @Override
        public void run() {
            if (i<100) {
                primaryStage.setX(primaryStage.getX() - 4);
                primaryStage.setY(primaryStage.getY() - 1);
                primaryStage.setWidth(primaryStage.getWidth()+8.11);
                primaryStage.setHeight(primaryStage.getHeight()+3.19);
            } else {
                this.cancel();
            }
            i++;
        }

    }, 0, 10);

    Timeline timeline = new Timeline(new KeyFrame(Duration.millis(10 * 101), ev -> {
        primaryStage.setResizable(true);
        primaryStage.setScene(new Scene(HomePage));
        fullUserName.setText("User"); // Got NullPointerException at this line
    }));
    timeline.play();

}

public void start(Stage primaryStage) {
    try {
        Pane root = new Pane();
        Scene scene = new Scene(root,400,400);
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.show();
        loadHomePage(primaryStage);
    } catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
}

HomePage.fxml:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="9.0" minWidth="16.0" prefHeight="720.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.HomePage">
<top>
  <GridPane maxHeight="-Infinity" minHeight="-Infinity" prefHeight="32.5" style="-fx-background-color: #aeccc9;" BorderPane.alignment="CENTER">
    <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="11.0" />
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="27.5" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="11.5" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="1.7976931348623157E308" minWidth="10.0" />
        <ColumnConstraints hgrow="ALWAYS" maxWidth="-Infinity" minWidth="-Infinity" />
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="15.0" />
    </columnConstraints>
    <rowConstraints>
      <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
    </rowConstraints>
     <children>
        <MenuButton fx:id="fullUserName" mnemonicParsing="false" GridPane.columnIndex="4">
          <items>
            <MenuItem mnemonicParsing="false" text="Logout" />
            <MenuItem mnemonicParsing="false" text="Exit" />
          </items>
           <graphic>
              <ImageView fitHeight="22.0" fitWidth="22.0" pickOnBounds="true" preserveRatio="true">
                 <image>
                    <Image url="@Cool-50.png" />
                 </image>
              </ImageView>
           </graphic>
           <padding>
              <Insets bottom="-1.5" left="-1.5" right="-1.5" top="-1.5" />
           </padding>
        </MenuButton>
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Title" GridPane.columnIndex="3">
           <font>
              <Font name="Arial Rounded MT Bold" size="22.0" />
           </font>
        </Text>
     </children>
  </GridPane>
</top>
</BorderPane>

正如您所看到的,我在执行NullPointerException时得到fullUserName.setText("User");,这意味着“fullUserName”菜单按钮未初始化。

程序是否可以等到fullUserName(或任何其他FXML变量)被初始化,以便我可以执行该语句?

0 个答案:

没有答案