我正在制作一个简单的食谱应用程序来练习JavaFX,我遇到了一个问题。我似乎无法导入这个类:如何正确导入我的自定义类到这个FXML文件中?
package application;
import javafx.beans.property.SimpleStringProperty;
public class Recipe {
private final SimpleStringProperty Name = new SimpleStringProperty("");
public Recipe() {
this("");
}
public Recipe(String recipeName) {
setRecipeName(recipeName);
}
public String getRecipeName() {
return Name.get();
}
public void setRecipeName(String rName) {
Name.set(rName);
}
}
进入这个FXML视图文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.cell.*?>
<?import javafx.collections.*?>
<?import fxmltableview.*?>
<?import java.lang.String?>
<?import application.Recipe ?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TableView prefHeight="400.0" prefWidth="600.0">
<columns>
<TableColumn prefWidth="599.0" text="Column One" >
<cellValueFactory><PropertyValueFactory property="Name" />
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<Recipe Name="Test Name"/>
</FXCollections>
</items>
</TableView>
</children>
</AnchorPane>
我一直就行收到一个错误。任何帮助是极大的赞赏。
嗨, 我不知道,如果解决您的问题(我想不会,SRY),但命名您的变量“名称”(首字母大写)被认为是不好风格,并可能被编译器误解。 (至少据我所知...) – GoatyGuy
是的,这并没有真正帮助我,但你是对的,我改变它为recipeName,这是更明显的,但...我仍然无法得到它工作。编辑:没关系,这是命名约定。名称显然指的是...中的保留字段,我不知道,但现在起作用。 – JLH