如何为java中已创建的新组件创建FXML文件,而不是将其添加到场景构建器? [英] How to create an FXML file for an already created new component in java than add it to scene builder?

查看:173
本文介绍了如何为java中已创建的新组件创建FXML文件,而不是将其添加到场景构建器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javaFX的新手。我在java中创建了一个自定义的搜索框(扩展TextField),检查图像:

I'm new to javaFX. I created a customized Search box (extends TextField) in java, check image:

我用测试类测试了它并且它正常工作。

I tested it with a test class and it's working.

我现在想知道是否可以创建FXML文件而不是将此组件添加到场景构建器中?怎么做 ?在此先感谢。

I want to know now if it's possible to create its FXML file than add this component to scene builder ? how to do it ? Thanks in advance.

推荐答案

如何将组件从JAR导入SceneBuilder

您可以将组件放入Jar中并将其导入SceneBuilder。您无需为组件创建FXML文件即可将其添加到SceneBuilder库面板。

You can put your component in a Jar and import it into SceneBuilder. You don't need to create an FXML file for your component to add it to SceneBuilder Library Panel.

请参阅将自定义组件添加到JavaFX用户指南的库部分。


要从JAR或FXML文件导入自定义GUI组件:


  1. 选择导入JAR / FXML 来自库面板菜单的文件命令,或
    直接从系统的本机文件
    manager(Explorer或Finder)拖动JAR或FXML文件并将其放入库面板

  1. 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 panel

在打开对话框窗口中,导航到要导入的JAR或FXML
文件的位置。导入对话框,类似于显示rel =noreferrer>图8-4 。 JAR文件的内容是
检查,并且所有确定为
合适自定义组件的Java类都显示在对话框窗口中。解析
FXML文件的内容,以确保添加
的组件是有效且自包含的。

In 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 Figure 8-4, 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.

单击导入组件。导入的项目将添加到库面板的自定义
部分。它们可以立即使用,即使在重新启动Scene Builder之后,它们也会在库中保留

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的代码组件。

Note, SceneBuilder also supports importing of FXML based components rather than just straight code components. This answer only discusses importing of code only components which do not contain FXML.

示例导入的组件使用

这是我自定义的搜索字段组件使用上述方法导入SceneBuilder。

Here is a custom search field component that I imported into SceneBuilder using the method outlined above.

顶部搜索面板位于Scene Builder设计窗格中,底部搜索面板是使用Scene Builder预览功能并搜索快乐的结果。

The top search panel is in the Scene Builder design pane, the bottom search panel is the result of using the Scene Builder preview function and searching for happiness.

示例SceneBuilder生成的代码

由SceneBuilder生成的fxml文件基于设计包括在这里。注意,这只是我用SceneBuilder创建的测试场景,用于测试已导入的组件 - 它不是组件导入过程本身的一部分。

The fxml file which was generated by SceneBuilder based on the design is included here. Note, this was just a test scene I created with SceneBuilder to test the already imported component - it was not part of the component import process itself.

<?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>

示例(可导入)组件代码

导入的搜索框的代码是:

The code for the search box which was imported is:

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
        );
    }
}

组件先决条件

为了使流程有效,您需要确保以下几点:

In order for the process to work, there are a few things you need to ensure:


  1. 您的组件类扩展了Node。

  2. 您的组件类有一个无参数构造函数。

  3. 您的组件类和没有参数构造函数是public。

  4. 您的组件类在一个包中(例如org.jewelsea) - 它不能没有包设置。

  5. 您的组件类包装在一个JAR文件中,该文件已导入到SceneBuilder中,如上所述。

  1. Your component class extends Node.
  2. Your component class has a no argument constructor.
  3. Your component class and no argument constructor are public.
  4. Your component class is in a package (e.g. org.jewelsea) - it can't have no package set.
  5. Your component class is packaged in a JAR file which has been imported into SceneBuilder as described above.

疑难解答

如果您在导入JAR时遇到问题,在尝试进行JAR导入后,您可以使用下面记录的JAR分析功能来帮助进行故障排除(这可能有助于或可能仅提供一些隐藏的信息让你更加困惑。)

If you are having issues importing the JAR, after you have attempted a JAR import, you can use the JAR analysis function documented below to help troubleshoot (which might help or might just provide some cryptic information to confuse you more).

这篇关于如何为java中已创建的新组件创建FXML文件,而不是将其添加到场景构建器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆