簡體   English   中英

加載fxml文件並設置其Label文本時的JavaFX lssue

[英]JavaFX lssue when loading fxml file and seting its Label text

昨天我也提出了類似的問題,但我認為它的解釋不充分,因此我想再問一次,但對代碼進行了一些更改。 如果我寫的太多了,我很抱歉,但是我想讓一切都可以理解。

因此,我制作了一個神奇寶貝模擬器,您可以在其中捕捉和訓練神奇寶貝。 我有一個主要的fxml文件,其中包含用於訪問不同的fxml文件(如漁獲物,戰斗,商店, 袋子等)的按鈕昨天,我在做袋子,其中存放了所有物品。

一切正常,並且窗口在它們之間正確切換。 問題出在我試圖向包裝袋中的每個項目添加標簽時,該標簽被設計為向用戶顯示他對每個項目的了解。 所以我創建了沒有文本的所有標簽,所以它們是空的。

他們將從我從數據庫獲得的信息中得到滿足,這東西也可以正常工作,我連接到數據庫並獲得了商品的擔保。 當我想在我的行李箱窗口中顯示該物品的准確性時,就會出現問題。

為什么? 因為正如您可以想象的那樣,我希望當您單擊購物袋按鈕時,購物袋文件中加載的所有標簽都充滿了每個項目的准確性。 標簽是在bag fxml控制器上定義的,因此,如果我想用一些文本填充它們,則不能從使用其他控制器的主窗口中執行此操作,我需要在bag控制器中進行操作。

這是我試圖使其工作的代碼(位於主控制器中):

@FXML
    void mochila (ActionEvent event) throws IOException, SQLException {
        AnchorPane pane = FXMLLoader.load(getClass().getResource("mochila.fxml"));
        anchorPaneContent.getChildren().setAll(pane);
        anchorPane2.setStyle("-fx-background-color: #3f3f3f;");
        cm.getCantidad();
    }

getCantidad是我的包控制器中具有的功能,就是這樣:

public void getCantidad() {
        lblpokeballCount.setText("Cantidad: "+pokeballcount);
        lblsuperballCount.setText("Cantidad: "+superballcount);
        lblultraballCount.setText("Cantidad: "+ultraballcount);
        lblmasterballCount.setText("Cantidad: "+masterballcount);
    }

因此,當我嘗試從主控制器運行此功能時,它將返回空指針異常。 這意味着標簽沒有初始化,但是當我第一次AnchorPane pane = FXMLLoader.load(getClass().getResource("mochila.fxml")); 應該從文件中加載所有資源嗎? 因為我在我的包文件中讀取了一個按鈕,所以在單擊時會運行相同的功能,並且它可以正常工作,因為我是從同一控制器/文件中調用它的。

所以現在我真的不知道該怎么辦,這對學校來說是個問題,但是我的編程老師從未接觸過javafx,所以他們甚至都不知道我在做什么。 你是我唯一的希望。 我試圖了解這個職位: 職位

但是我完全不了解它對所有這些東西都是新的。 伙計們,如果您能幫助我,我將不勝感激,謝謝!

編輯:

@FXML
    void mochila (ActionEvent event) throws IOException, SQLException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
        anchorPaneContent.getChildren().setAll(loader);
        controladorMochila controller = loader.<controladorMochila>getController();
        controller.getCantidad();
    }

anchorPaneContent是主窗格內的錨定窗格。 所有按鈕都在主窗格中,根據您單擊的按鈕,另一個fxml文件的anchorpanecontent將會更改。 我試圖像上面提到的帖子那樣做。 但是我不能做anchorPaneContent.getChildren().setAll(loader); 因為它說:節點setAll不適用於參數(FXMLLoader)

您正在嘗試將FXMLLoader添加到錨定窗格,這將不起作用,因為FXMLLoader不是可視組件(這是加載FXML文件的東西)。

另外,您嘗試從FXMLLoader獲取控制器,而無需實際加載FXML文件。 這將不起作用,因為在FXML文件中指定了控制器類(因此FXMLLoader在加載文件之前才知道要創建哪種控制器)。

您需要加載FXML文件並將結果添加到錨點窗格:

@FXML
void mochila (ActionEvent event) throws IOException, SQLException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
    anchorPaneContent.getChildren().setAll(loader.load());
    controladorMochila controller = loader.<controladorMochila>getController();
    controller.getCantidad();
}

或者,如果您想更明確一點:

@FXML
void mochila (ActionEvent event) throws IOException, SQLException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
    Parent pane = loader.load();
    anchorPaneContent.getChildren().setAll(pane);
    controladorMochila controller = loader.<controladorMochila>getController();
    controller.getCantidad();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM