JavaFX自定义控件(TextField)无法正常工作 [英] JavaFX custom control (TextField) not working
问题描述
我正在尝试使用JavaFX和SceneBuilder 1.1进行自定义控件。
I am trying to make a custom control with JavaFX and SceneBuilder 1.1.
我有这段代码:
FXML
<?import libreria.javaFX.componentes.componenteTextField.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
<children>
<CustomComponent fx:id="pastaTxt" layoutX="69.0" layoutY="87.0" prefWidth="200.0" />
</children>
</AnchorPane>
CustomComponent.java
CustomComponent.java
package libreria.javaFX.componentes.componenteTextField;
import javafx.scene.control.TextField;
public class CustomComponent extends TextField {
public CustomComponent() {
super();
// TODO Auto-generated constructor stub
}
public CustomComponent(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
}
当我尝试从SceneBuilder打开它时,它告诉我:
When I try to open it from SceneBuilder it tells me this:
缺少的类型是:[CustomComponent]
Missing types are: [CustomComponent]
它让我有机会指定Classpath(也不解决问题)。
and it gives me the chance to specify the Classpath (which doesn't fix the problem either).
我也尝试将类放在import语句中,如下所示:
I tried putting the class at the import statement too, like this:
<?import libreria.javaFX.componentes.componenteTextField.CustomComponent?>
但是它给出了 ClassNotFoundException
。
关于为什么会发生这种情况的任何想法?
Any ideas about why is this happening?
更多信息
我用这些类完成了一个新项目:
I have done a new project with just these classes:
代码如下:
CustomControl.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import custom.CustomControl?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?scenebuilder-classpath-element ../../bin/custom?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
<children>
<CustomControl layoutX="51.0" layoutY="100.0" prefWidth="200.0" />
</children>
</AnchorPane>
CustomControl.java
package custom;
import javafx.scene.control.TextField;
public class CustomControl extends TextField {
public CustomControl() {
super();
}
public CustomControl(String arg0) {
super(arg0);
}
}
我仍有同样的问题。我用对话框指定了类路径,一切似乎都适合我但是我在打开SceneBuilder时遇到了同样的错误。
And I still have the same problem. I specify the classpath with the dialog, everything seems right to me but I have the same errors opening the SceneBuilder.
最新信息
尝试接近解决方案,我们在Eclipse下尝试了这个项目。结果是Eclipse显示窗口正常,但SceneBuilder继续这些错误。我希望这条线索有所帮助。
如果有人在Scene Builder下完成了这种自定义控件定义,请告诉我们并给我们一个例如,它对我们的项目非常有用。
推荐答案
这是由于未指定正确的类路径引起的,这允许java运行时运行场景构建器加载控件类。
This is caused by not specifying the correct classpath, which allows the java runtime running scene builder to load the control classes.
如果您正在运行eclipse并且您的类具有命名空间 custom.MyControl
然后指定bin目录而不是自定义目录。在maven项目中,您需要指定目标/类目录。
If you are running eclipse and your class has the namespace custom.MyControl
then specify the bin directory and not the custom directory. In a maven project you need to specify the target/classes directory.
在此处查看我自己项目中的示例: https://bitbucket.org/atill/estimate/src/22390a2ca034b55f1916e46435b714e5c489b90e/src /main/resources/projmon/gui/workTree.fxml?at=master
See an example in my own project here: https://bitbucket.org/atill/estimate/src/22390a2ca034b55f1916e46435b714e5c489b90e/src/main/resources/projmon/gui/workTree.fxml?at=master
相对文件路径通常由场景构建器创建,因此移动文件将会中断类路径,你需要重新指定它。
A relative file path is often created by scene builder so moving files will break the class path and you will need to respecify it.
这篇关于JavaFX自定义控件(TextField)无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!