混合两个自定义组件javafx

问题描述 投票:0回答:1

我修改了结构以创建我面临的问题的简单示例,可以轻松重现。我正在尝试组合两个自定义组件 - 一个是自定义按钮,另一个是包含自定义按钮的水平盒。

自定义按钮的Java代码及其对应的fxml如下:

    public class Custom_Button extends Button {
    public Custom_Button() {
    super();
    try{
        FXMLLoader loader=new FXMLLoader(getClass().getResource("/eu/mylib/libraries_custom_/custom_button/Custom_Button.fxml"));
        loader.setRoot(this);
        loader.setController(this);
        loader.load();
    }catch (Exception e){
        e.printStackTrace();
    }                   
                                                                                                                                        }
                                                                                                                                           }

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>


    <fx:root prefHeight="106.0" prefWidth="168.0" stylesheets="@Custom_Button.css" text="RED" type="javafx.scene.control.Button" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1">

    </fx:root>

现在是第二个组件:

java代码:

public class Custom_Pick extends HBox {
public Custom_Pick() {
    super();
    try{
        FXMLLoader loader=new FXMLLoader(getClass().getResource("/eu/mylib/libraries_custom_/custom_pick/Custom_Pick.fxml"));
        loader.setRoot(this);
        loader.setController(this);
        loader.load();
    }catch (Exception e){
        e.printStackTrace();
    }
                                                                                                                                     }
                                                                                                                                        }

FXML:

<?xml version="1.0" encoding="UTF-8"?>


<?import javafx.scene.layout.*?>

    <?import eu.mylib.libraries_custom_.custom_button.Custom_Button?>
        <fx:root type="javafx.scene.layout.HBox" xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            prefHeight="400.0" prefWidth="600.0">
            <Custom_Button></Custom_Button>
        </fx:root>

我会尽力解释这一点。当我启动包含它们的应用程序时,这两个组件似乎都工作正常。然而,我的主要问题是场景生成器。第一个组件显示正确,这意味着它在场景生成器中具有可视化表示。但是包含第一个组件的第二个组件仅显示一个空白窗格,我不确定为什么。

图像:

项目结构:

java javafx
1个回答
0
投票

看起来我没有搜索太多,因为答案已经写在这里了 谢谢您,抱歉给您带来麻烦 场景生成器嵌套自定义节点

© www.soinside.com 2019 - 2024. All rights reserved.