首页 文章

在FXML文件中设置自定义控件的Button“onAction”方法?

提问于
浏览
1

我创建了一个包含标签,文本字段和按钮的新自定义控件 . 如何在FXML文件中设置自定义控件的Button“onAction”方法?

示例代码:

<BorderPane fx:controller="fxmlexample.MyController" 
xmlns:fx="http://javafx.com/fxml">
 <top>
    <MyCustomComponent onButtonAction="#myCustomButtonAction">
    </MyCustomComponent>
 </top>

2 回答

  • 0

    好的,我找到了解决方案 . 我完全按照谢尔盖格里涅夫在这个帖子中提出的那样做了 - JavaFX 2.0 - create action handler for custom component in FXML . 我做的唯一更改是在类构造函数中使用我的按钮的字段名称而不是"this" .

  • 0

    只要 MyCustomComponent 包含名为 setOnButtonAction 的事件处理程序的setter,就可以使用 onButtonAction="#myCustomButtonAction" .

    class MyCustomComponent {
    
        private Button button;
    
        public void setOnButtonAction(EventHandler<ActionEvent> handler) {
            this.button.setOnAction(handler);
        }
    
        public EventHandler<ActionEvent> getOnButtonAction() {
            return this.button.getOnAction();
        }
    
        ...
    }
    

相关问题