在SceneBuilder中引用JavaFX项

时间:2018-06-28 06:05:49

标签: java javafx nullpointerexception

我得到一个nullpointerexception,我不明白为什么。这是我的代码

public class AlertPane {
    @FXML
    public static Label msgLbl;
    @FXML
    private ImageView confirmBtn;
    @FXML
    private Pane alertPane;


    @FXML
    public void initialize() {
        alertPane.setVisible(true);
    }

    public static void display(String text) {
        try {
            Stage alertWindow = new Stage();
            Parent root = FXMLLoader.load(AlertPane.class.getResource("Alert.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(AlertPane.class.getResource("application.css").toExternalForm());
            alertWindow.setScene(scene);
            alertWindow.initStyle(StageStyle.UNDECORATED);
            alertWindow.initModality(Modality.APPLICATION_MODAL);
            alertWindow.show();
            msgLbl.setText(text); // THROWS A NULLPOINTEREXCEPTION
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

我已经声明了Alert.fxml文件的控制器,该文件是场景构建器内部的类本身(AlertPane)。

这是Alert.fxml文件:

<AnchorPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.AlertPane">
   <children>
      <Pane fx:id="alertPane" prefHeight="266.0" prefWidth="426.0" style="-fx-background-color: white;">
         <children>
            <Label fx:id="msgLbl" layoutY="86.0" prefHeight="94.0" prefWidth="426.0" style="-fx-alignment: center;" wrapText="true">
               <font>
                  <Font name="SAO UI TT Regular" size="19.0" />
               </font>
            </Label>
            <ImageView id="ico_alert" fx:id="confirmBtn" fitHeight="50.0" fitWidth="50.0" layoutX="362.0" layoutY="202.0" pickOnBounds="true" preserveRatio="true" style="-fx-cursor: hand;">
               <image>
                  <Image url="@../../../../Desktop/prog/mavis_lib/icons/ico_confirm.png" />
               </image></ImageView>
         </children>
      </Pane>
   </children>
</AnchorPane>

0 个答案:

没有答案