解决方法
您可以将组件放在Jar中并将其导入SceneBuilder.您无需为组件创建FXML文件即可将其添加到SceneBuilder库面板.
请参阅JavaFX用户指南的Adding Custom Components to the Library部分.
To import custom GUI components from a JAR or FXML file:
Select Import JAR/FXML file command from the Library panel’s menu,or
drag the JAR or FXML file directly from your system’s native file
manager (Explorer or Finder) and drop it into the Library panelIn the Open dialog window,navigate to the location of the JAR or FXML
file that you want to import. The Import Dialog,similar to what is
shown in 07001,is displayed. The JAR file’s contents are
inspected and all the Java classes that are determined as being
suitable custom components are displayed in the dialog window. The
FXML file’s contents are parsed to make sure that the component being
added is valid and self-contained.From the Import dialog window,select or unselect items from the list
of items that you are able to import.Click Import Components. Imported items are added to the Custom
section of the Library panel. They can be used immediately and they
persist in the Library even after Scene Builder is restarted
注意,SceneBuilder还支持导入基于FXML的组件,而不仅仅是直接代码组件.本回答仅讨论仅导入不包含FXML的代码组件.
示例导入的组件使用情况
这是我使用上述方法导入SceneBuilder的自定义搜索字段组件.
顶部搜索面板位于Scene Builder设计窗格中,底部搜索面板是使用Scene Builder预览功能和搜索快乐的结果.
此处包含由SceneBuilder根据设计生成的fxml文件.注意,这只是我用SceneBuilder创建的测试场景,用于测试已导入的组件 – 它不是组件导入过程本身的一部分.
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.text.*?> <?import org.jewelsea.*?> <?import javafx.geometry.*?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> <children> <Label text="Search Field Import Test"> <font> <Font size="16.0" /> </font> </Label> <SearchField /> </children> <padding> <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> </padding> </VBox>
样本(可导入)组件代码
package org.jewelsea; import javafx.geometry.Insets; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; public class SearchField extends StackPane { private final TextField textField; private final Button searchButton; private final Label searchResults; public SearchField() { textField = new TextField(); textField.setPromptText( "Search Text" ); searchButton = new Button("Search"); searchResults = new Label(); VBox layout = new VBox( 20,new HBox( 10,textField,searchButton ),searchResults ); layout.setPadding(new Insets(10)); searchButton.setOnAction(event -> searchResults.setText( "Search result for " + textField.getText() ) ); getChildren().setAll( layout ); } }
组件先决条件
为了使流程有效,您需要确保以下几点:
>您的组件类扩展了Node.
>您的组件类具有无参数构造函数.
>您的组件类和没有参数构造函数是公共的.
>您的组件类在一个包中(例如org.jewelsea) – 它不能没有包.
>您的组件类打包在一个JAR文件中,该文件已导入到SceneBuilder中,如上所述.
故障排除
如果您在尝试导入JAR时遇到问题,则在尝试进行JAR导入后,可以使用下面记录的JAR分析功能来帮助进行故障排除(这可能有助于或可能仅提供一些含糊不清的信息以使您更加困惑).