我想将自定义组件添加到 intelliJ 中的 javafx 场景构建器中。我目前使用的是java 8.1。我听说 java 10 可以向场景构建器添加组件,因为它有 java 2.0。我安装了java 10,但我不知道如何切换到该版本的java。因此,如果您知道如何解决这个问题,我很想听听。
提前致谢,
莱纳吉
编辑: 我明白为什么这个问题与其他帖子类似,因为直接问题是将自定义组件直接添加到场景生成器。然而,这个问题存在的根本原因是,我使用的是intelliJ中的编辑器,而该编辑器是针对javafx 1.0左右的,而javafx 2.0的编辑器支持添加自定义组件的功能,该编辑器位于java 10。现在真正的问题是:如何更新 intelliJ 中的场景构建器?我已经安装了 java 10,但我的项目仍在使用 java 8.1,我不知道如何更改它。我希望这能解释为什么我的问题是相关的,而不是重复的。感谢您阅读这个比最初的问题更长的解释;p
最佳答案
I want to be able to make a java class, extending Pane or so, then assign children and actions to it, such as a slidebar with a textfield next to it, showing the value. I know that in the scene builder app, that is possible by importing a jar file with the class and dependencies straight into the scene builder, but I don't know how to do that in the scene builder in intelliJ.
使用场景生成器
的方式没有区别。您只需在用户控件中添加属性(如果属性不是只读的,则使用 getter 和 setter)。在这种情况下,属性将出现在 Scene Builder
检查器中,您将能够通过它们或直接编辑 FXML
文件来操作它们。
这是一个示例(java 8,但这无关紧要)
mycontrol.fxml
<fx:root type="javafx.scene.layout.HBox" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
<Label fx:id="label"/>
<Slider fx:id="slider" HBox.hgrow="ALWAYS"/>
</fx:root>
public class MyControl extends HBox {
@FXML
private Label label;
@FXML
private Slider slider;
private StringProperty labelName = new SimpleStringProperty();
private DoubleProperty sliderPos = new SimpleDoubleProperty();
public MyControl() {
try {
FXMLLoader l = new FXMLLoader(getClass().getResource("mycontrol.fxml"));
l.setController(this);
l.setRoot(this);
l.load();
}
catch (IOException e) {
e.printStackTrace();
}
}
@FXML
private void initialize() {
slider.setMin(0.0);
slider.setMax(100.0);
label.textProperty().bindBidirectional(labelName);
slider.valueProperty().bindBidirectional(sliderPos);
}
public String getLabelName() {
return labelName.get();
}
public StringProperty labelNameProperty() {
return labelName;
}
public void setLabelName(String labelName) {
this.labelName.set(labelName);
}
public double getSliderPos() {
return sliderPos.get();
}
public DoubleProperty sliderPosProperty() {
return sliderPos;
}
public void setSliderPos(double sliderPos) {
this.sliderPos.set(sliderPos);
}
}
这就是这个用户控件的使用场景
样本.fxml
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.121" fx:controller="sample.Controller">
<MyControl labelName="Test name" sliderPos="25.0" />
</AnchorPane>
关于javafx - 将自定义组件添加到 intelliJ 中的 FXML 场景构建器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51064426/