gpt4 book ai didi

java - 如何修复Object无法转换为javafx.scene.Node

转载 作者:行者123 更新时间:2023-12-02 06:21:04 24 4
gpt4 key购买 nike

我正在尝试加载我的 fxml 文件,该文件很好并且已加载(我已经在我的主类中尝试过)。但是,当我尝试在 Controller 中设置内容时,出现错误:

incompatible types: java.lang.Object cannot be converted to javafx.scene.Node at addNewCategorie.getDialogPane().setContent(fxmlLoader.load());

我的 Controller 类:

@FXML
private BorderPane mainBorder;

@FXML
public void handle_the_addition_of_newCategorie(){
Dialog<ButtonType> addNewCategorie = new Dialog<>();
addNewCategorie.setTitle("Add Categorie");
addNewCategorie.initOwner(mainBorder.getScene().getWindow());
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("/AddNewCategorie.fxml"));
try{
addNewCategorie.getDialogPane().setContent(fxmlLoader.load());
}catch (IOException E){
System.out.println("IOEXception : "+E.getMessage());
}
addNewCategorie.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
addNewCategorie.show();
}

我的 fxml 文件:

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

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<BorderPane fx:id="mainBorder" fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml" style="-fx-background-color:#485460">
<center>
<HBox spacing="20" alignment="CENTER">
<Button text="Add new Categorie" style="-fx-background-color:#2ecc71; -fx-text-fill:white" onMouseClicked="#handle_the_addition_of_newCategorie"/>
</HBox>
</center>
</BorderPane>

最佳答案

必须改变这一点:

addNewCategorie.getDialogPane().setContent(fxmlLoader.load());

至:

addNewCategorie.getDialogPane().setContent((Node)fxmlLoader.load());

和:

fxmlLoader.setLocation(getClass().getResource("/AddNewCategorie.fxml"));

致:

fxmlLoader.setLocation(getClass().getClassLoader().getResource("/AddNewCategorie.fxml"));

关于java - 如何修复Object无法转换为javafx.scene.Node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59730056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com