【发布时间】:2016-03-31 12:05:58
【问题描述】:
我通过扩展现有控件创建了一个新控件,我想在我的 JavaFX 场景中使用这个新控件。我希望能够使用 Scene Builder 编辑我的场景,但是在将新控件添加到 FXML 文件后,我在打开 Scene Builder 时遇到了ClassNotFoundException。
例如,这是我创建的一个扩展 TextField 的类:
RegexLimitingTextField.java
public class RegexLimitingTextField extends TextField {
private String regexLimiter = ".*";
public void setRegexLimiter(String regex) {
this.regexLimiter = regex;
}
@Override
public void replaceText(int start, int end, String text) {
if (text.matches(regexLimiter))
super.replaceText(start, end, text);
}
@Override
public void replaceSelection(String replacement) {
if (replacement.matches(regexLimiter))
super.replaceSelection(replacement);
}
}
将此控件添加到我的 FXML 文件后...
sample.fxml
<?import javafx.scene.layout.GridPane?>
<?import sample.RegexLimitingTextField?>
<GridPane fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<RegexLimitingTextField fx:id="textField" text="Test" />
</GridPane>
...加载 Scene Builder 2.0 时出现此错误:
Caused by: java.lang.ClassNotFoundException: sample.RegexLimitingTextField
at java.lang.ClassLoader.findClass(ClassLoader.java:530)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2920)
at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2909)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2850)
... 23 more
为什么 Scene Builder 找不到我的新控件?我需要做什么才能让它找到并能够使用我的新控件?
如果需要,这里是其他文件:
Main.java
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setScene(new Scene(root, 200, 200));
primaryStage.show();
}
}
Controller.java
public class Controller implements Initializable {
public RegexLimitingTextField textField;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
textField.setRegexLimiter("\\w*");
}
}
【问题讨论】:
-
@James_D 成功了,谢谢。不过我有点生气。他们为什么要删除
scenebuilder-classpath-element标签?这似乎意味着每次我对我的课程进行更改时,我都必须重新构建 jar 并重新导入它。没有更简单的方法吗?
标签: java javafx classnotfoundexception scenebuilder