如何创建自定义JavaFX组件库?
我一直在玩弄JavaFX有一段时间了,现在我想分享一些我构建的组件。为了便于使用,我希望能够将组件作为库分发,这样其他用户就可以将我的库导入到JavFX项目中。然而,事实证明,构建一个库文件以导入到SceneBuilder(SB)比我预期的要困难得多。我试着跟着,但一定是做错了什么。我想使用.fxml文件和.jar文件创建自己的自定义组件 我尝试构建此组件: TestPane.java如何创建自定义JavaFX组件库?,java,javafx,scenebuilder,Java,Javafx,Scenebuilder,我一直在玩弄JavaFX有一段时间了,现在我想分享一些我构建的组件。为了便于使用,我希望能够将组件作为库分发,这样其他用户就可以将我的库导入到JavFX项目中。然而,事实证明,构建一个库文件以导入到SceneBuilder(SB)比我预期的要困难得多。我试着跟着,但一定是做错了什么。我想使用.fxml文件和.jar文件创建自己的自定义组件 我尝试构建此组件: TestPane.java package pack; import javafx.event.ActionEvent; import
package pack;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.AnchorPane;
import java.io.IOException;
public class TestPane extends AnchorPane {
public TestPane(){
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("TestPane.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
@FXML protected void onClick(ActionEvent ae){
System.out.println("Click");
}
}
TestPane.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<fx:root type="AnchorPane" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
<Button fx:id="button" layoutX="6.0" layoutY="10.0" mnemonicParsing="false" text="Button" onAction="#onClick" />
</fx:root>
LibTest.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import pack.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="199.0" prefWidth="126.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40">
<children>
<TestPane layoutX="34.0" layoutY="82.0" />
</children>
</AnchorPane>
我错过了什么?是什么导致了这个问题?我该如何修复它?这个问题是由编译器(或者VM,我不知道正确的时间)不知道如何创建TestPane类型的对象引起的 请注意,从错误日志中可以看出,错误发生在
D:/WorkspaceDir/ProjectDir/bin/test/LibTest.fxml:11
。如果我们查看LibTest.fxml,第11行,就会看到这是尝试创建TestPane的行
该问题的解决方案是将库文件添加到Java项目的构建路径中。这将使您的项目了解如何创建TestPane类型的对象。右键单击项目->
生成路径->
配置生成路径。。。然后,在库选项卡下,单击添加JAR。。。(或者添加外部jar…,如果.jar文件不在您的工作区内)并查找导出的.jar文件
另外,如果要创建SceneBuilder库,请不要在包名称中使用大写字母。我以前在导入在其包中使用大写名称的库时遇到过问题。您解决了吗?这是非常酷的,它甚至可以做到,许多(大多数?)SDK甚至不支持像这样的定制组件。是的,我让它工作了。见下面的答案。或者你想知道什么具体的事情?
<?xml version="1.0" encoding="UTF-8"?>
<?import pack.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="199.0" prefWidth="126.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40">
<children>
<TestPane layoutX="34.0" layoutY="82.0" />
</children>
</AnchorPane>
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/1645995473.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: TestPane is not a valid type.
/D:/WorkspaceDir/ProjectDir/bin/test/LibTest.fxml:11
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2778)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2708)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2531)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
at test.LibTest.start(LibTest.java:15)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/198061478.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1079803749.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
... 1 more
Exception running application test.LibTest