我通过扩展现有控件创建了一个新控件,我想在我的JavaFX场景中使用这个新控件.我希望能够使用Scene Builder编辑我的场景,但在将新控件添加到FXML文件后,我ClassNotFoundException在打开Scene Builder 时遇到了问题.
例如,这是我制作的一个类,它扩展了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);
}
}
Run Code Online (Sandbox Code Playgroud)
将此控件添加到我的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>
Run Code Online (Sandbox Code Playgroud)
...加载Scene Builder 2.0时出现此错误:
Caused by: java.lang.ClassNotFoundException: …Run Code Online (Sandbox Code Playgroud) 我正在使用 Java 20、JavaFX 20 和 Maven 创建一个小型个人项目。我在创建可重用组件并通过主场景的控制器操作它们时遇到问题。
首先,我按照官方文档中列出的步骤进行操作。之后,我转到 SceneBuilder 并在 SceneBuilder 中导入自定义组件的 FXML 文件(单击小引擎图标,其中显示“库”-> JAR/FXML Manager -> 从文件系统添加库/FXML)并将其添加到就像使用任何默认组件一样的场景。然后,我为自定义组件提供了一个 fx:id 并将其添加到场景的控制器类中,以便我可以使用它,但出现以下错误。
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893)
at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:1623)
Caused by: javafx.fxml.LoadException:
/C:/Users/user/Desktop/eclipse-workspace/Project 3/target/classes/app/views/fxml/Menu.fxml:43
at javafx.fxml@20/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2722)
at javafx.fxml@20/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2700)
at javafx.fxml@20/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2563)
at javafx.fxml@20/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2531)
at app/app.Main.loadFXML(Main.java:29)
at app/app.Main.start(Main.java:17)
at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
at javafx.graphics@20/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
at javafx.graphics@20/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400) …Run Code Online (Sandbox Code Playgroud) 我有很多具有名称a1,a2,a3,...的对象我需要将它们放入List中,因此使用它们更简单,我会以某种方式使用循环吗?
我的尝试是:
List<SomeObject> list = new LinkedList<SomeObject>();
for (int i=0; i<1000; i++){
String varName = "a"+i;
list.add((SomeObject) varName);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下有人有建议吗?在循环内创建变量不是解决方案,因为它们是.fxml文档的一部分.或者给我一个如何用循环创建它的建议,因为它在.fxml中创建行并行添加循环新对象.
为了更容易理解.fxml文件看起来像
<SomeObject fx:id="a1" *other props* />
<SomeObject fx:id="a2" *other props* />
<SomeObject fx:id="a3" *other props* />
<SomeObject fx:id="a4" *other props* />
Run Code Online (Sandbox Code Playgroud)
非常感谢您的建议!