我的FXML页面未加载,我无法弄清楚为什么

时间:2020-06-10 03:45:52

标签: javafx fxml fxmlloader

我在弄清楚为什么我要用JFX FXML加载器加载的页面为何无法加载时遇到问题。 关于我的FXML加载类,我的Java代码符合标准,但是当我执行该代码时,当我单击“更多详细信息”按钮时,“更多详细信息”页面不会加载并显示...这是加载的代码段“更多详细信息”页面...

 // Now set up the stage and place the "post_details_update" on it.

Stage unilink_home_stage = (Stage) btnl_posting_more_details.getScene().getWindow();

unilink_home_stage.close();

// This closes the Login dialog

// now call the post_details_update.fxml

try {

//Parent post_details = FXMLLoader.load(  getClass().getResource("/unilink_home/post_details/post_details_update.fxml"));

Parent post_details = FXMLLoader.load(  getClass().getResource("/view/unilink_home/post_details/post_details_update.fxml"));

// Note: the path is not relative to the package that this file is in

// but the CLASSPATH from the src/

Stage post_details_stage = new Stage();

post_details_stage.setTitle("Welcome to UniLink");

post_details_stage.setScene(new Scene(post_details));  

post_details_stage.show();

    } //close the try block

    catch(Exception e) 

    {

        //System.out.print(e);

        //e.printStackTrace();

    System.out.println( e.getMessage() );

    }// close the catch block

文件夹结构为... enter image description here

并且FXML页面是...。“ post_details_update.fxml”:

<?import javafx.geometry.Insets?>

<?import javafx.scene.control.Button?>

<?import javafx.scene.control.Label?>

<?import javafx.scene.control.ListView?>

<?import javafx.scene.control.ToolBar?>

<?import javafx.scene.image.Image?>

<?import javafx.scene.image.ImageView?>

<?import javafx.scene.layout.AnchorPane?>

<?import javafx.scene.layout.ColumnConstraints?>

<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.layout.RowConstraints?>

<?import javafx.scene.layout.VBox?>







<AnchorPane 

fx:id="post_details" 

xmlns:fx="http://javafx.com/fxml/1" 

xmlns="http://javafx.com/javafx/11.0.1"

fx:controller="controller/unilink_home/post/details/unilink_home_post_detailsController"

>

   <children>

      <VBox prefHeight="401.0" prefWidth="850.0">

         <children>

            <ToolBar prefHeight="40.0" prefWidth="850.0">

              <items>

                <Button mnemonicParsing="false" text="Back to Main Window" />

                  <Label text="Review Post Details">

                     <padding>

                        <Insets left="250.0" />

                     </padding>

                  </Label>

              </items>

            </ToolBar>

            <GridPane prefHeight="183.0" prefWidth="849.0">

              <columnConstraints>

                <ColumnConstraints hgrow="SOMETIMES" maxWidth="474.0" minWidth="10.0" prefWidth="215.0" />

                  <ColumnConstraints hgrow="SOMETIMES" maxWidth="541.0" minWidth="10.0" prefWidth="101.0" />

                <ColumnConstraints hgrow="SOMETIMES" maxWidth="541.0" minWidth="10.0" prefWidth="534.0" />

              </columnConstraints>

              <rowConstraints>

                <RowConstraints maxHeight="144.0" minHeight="10.0" prefHeight="144.0" vgrow="SOMETIMES" />

                <RowConstraints maxHeight="71.0" minHeight="10.0" prefHeight="39.0" vgrow="SOMETIMES" />

              </rowConstraints>

               <children>

                  <ImageView fitHeight="135.0" fitWidth="222.0" pickOnBounds="true" preserveRatio="true">

                     <GridPane.margin>

                        <Insets left="50.0" />

                     </GridPane.margin>

                     <image>

                        <Image url="@../../../../images/under_construction.png" />

                     </image>

                  </ImageView>

                  <GridPane prefHeight="131.0" prefWidth="277.0" GridPane.columnIndex="1">

                    <columnConstraints>

                      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />

                    </columnConstraints>

                    <rowConstraints>

                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

                    </rowConstraints>

                     <children>

                        <Label text="Post ID:" />

                        <Label layoutX="10.0" layoutY="25.0" text="Title:" GridPane.rowIndex="1" />

                        <Label layoutX="10.0" layoutY="72.0" text="Description:" GridPane.rowIndex="2" />

                        <Label layoutX="10.0" layoutY="89.0" text="Creator:" GridPane.rowIndex="3" />

                        <Label layoutX="10.0" layoutY="99.0" text="Date Created:" GridPane.rowIndex="4" />

                        <Label layoutX="10.0" layoutY="105.0" text="Status:" GridPane.rowIndex="5" />

                     </children>

                  </GridPane>

                  <GridPane layoutX="225.0" layoutY="10.0" prefHeight="131.0" prefWidth="277.0" GridPane.columnIndex="2">

                     <columnConstraints>

                        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />

                     </columnConstraints>

                     <rowConstraints>

                        <RowConstraints maxHeight="47.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

                        <RowConstraints maxHeight="46.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

                        <RowConstraints maxHeight="46.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

                     </rowConstraints>

                  </GridPane>

                  <Button mnemonicParsing="false" text="Upload Image" GridPane.rowIndex="1">

                     <GridPane.margin>

                        <Insets left="70.0" />

                     </GridPane.margin>

                  </Button>

                  <Button mnemonicParsing="false" text="Close Post" GridPane.columnIndex="2" GridPane.rowIndex="1">

                     <GridPane.margin>

                        <Insets left="20.0" />

                     </GridPane.margin>

                  </Button>

                  <Button mnemonicParsing="false" text="Delete Post" GridPane.columnIndex="2" GridPane.rowIndex="1">

                     <GridPane.margin>

                        <Insets left="180.0" />

                     </GridPane.margin>

                  </Button>

                  <Button mnemonicParsing="false" text="Save" GridPane.columnIndex="2" GridPane.rowIndex="1">

                     <GridPane.margin>

                        <Insets left="350.0" />

                     </GridPane.margin>

                  </Button>

               </children>

            </GridPane>

            <ListView prefHeight="200.0" prefWidth="200.0" />

         </children>

      </VBox>

   </children>

</AnchorPane>

“ unilink_home_post_detailsController.java”在此阶段只是一个外壳...

package controller.unilink_home.post.details;





import javafx.fxml.FXML;


import javafx.scene.layout.AnchorPane;


import model.connectionTest;


import javafx.scene.control.MenuItem;


import javafx.scene.control.Button;


import javafx.scene.control.Label;


import javafx.scene.control.ChoiceBox;


import javafx.collections.FXCollections;


import javafx.collections.ObservableList;


//import javafx.event.Event;





//Import the classes for database query


import java.sql.Connection;


import java.sql.PreparedStatement;


import java.sql.ResultSet;


import model.table_setup_view_postings;





import javafx.collections.FXCollections;


import javafx.collections.ObservableList;


import javafx.fxml.Initializable;


import javafx.scene.control.ListCell;


import javafx.scene.control.ListView;


import javafx.util.Callback;





import java.net.URL;


import java.util.ResourceBundle;





import model.posting;





public class unilink_home_post_detailsController implements Initializable


{


//database connection related variables,


private String query;


//This is the SQL query string





PreparedStatement ps;


//PreparedStatement ps2;


// This is the prepared statement for the SQL query





private Connection db_connect;


// this is the database connection string for SQL query results





final String DB_NAME = "testDB";


// This is the database name








@FXML // fx:id="post_details"


private AnchorPane post_details; // Value injected by FXMLLoader





//===================================================================================================================


  /*


  * This method is the class constructor and sets up the controller.


  */


public void unilink_home_post_detailsController()


{





}// close public void unilink_home_post_detailsController()





//===================================================================================================================


  /*


  * This method will initialiise the UI


  */





   //@FXML


   @Override


   public void initialize(URL location, ResourceBundle resources)


{





}// close  public void initialize(URL location, ResourceBundle resources)








}// close public class unilink_home_post_detailsController

与此: 1.代码将编译。 2.当我单击按钮“更多详细信息” ...时,什么都没有发生,而且我也对如何调试它感到挑战,因为在带有Java JFX的Eclipse中,移入/移入按钮为灰色,并且未显示变量,因此我可以看到这些易损件的状态吗?

任何人都可以帮忙,因为我对JFX有点陌生,而且我读到FXML加载程序可能很精巧...

此致。

1 个答案:

答案 0 :(得分:0)

我发现问题出在我以前指向控制器的路径中……以某种方式我用“ /”而不是“”。当我进行更改时,它起作用了。谢谢那些回答的人。