1

这个问题很明显,我相信它很容易解决,我想知道为什么还没有发布简单易懂的答案。我找到了解释如何创建 *new 事件、更改事件方向、-bubble 等的答案。但是,我还没有找到关于捕获信号然后将其反弹到用户类的 Q/A,然后到一个类,该类是该用户类的用户,依此类推。话虽如此,这是我的问题。

我的班级,ClassOne,接收按钮的信号,然后必须将该信号传递给ClassTwo,然后将信号传递给ClassThree,我怎么在JavaFx中做到这一点。

请注意,该类不得触发新事件,但必须触发已捕获的同一事件。

下面是对我正在尝试做的事情的语法解释。

任何帮助,我的意思是任何输入将不胜感激。

public class MyBoxedButton extends HBox {
  private Button btn;

  MyBoxedButton(){
    // Initialization of all the objects
    btn = new Button();
    //catch the event emitted by the button 
    btn.setOnAction((ActionEvent e) ->{

        // fire/emit the same received event
        ?????
    });
  }
}

/*
This class catches the event emitted by MyBoxedButton and then passes it to ClassThree, 
but this does not work, how then can I catch the event and then re-emit it.
*/
class ClassTwo(){
    private MyBoxedButton mbb;

    public ClassTwo(){
        mbb = new MyBoxedButton();
        //catch the event emitted by MyBoxedButton
        ????
        // re-emit the caught event
        ????
    }
}   

/*
This class catches the event emitted by ClassTwo.
The exersice is ment to show how the messages flow 
form one object to a next and then to a next, so on and so forth.
*/
class ClassThree(){
    private ClassTwo c2;

    public ClassThree(){
        c2 = new ClassTwo();
        //catch the event emitted by MyBoxedButton
        ????
        // re-emit the caught event
        ????
}   
4

1 回答 1

0

我认为,只要您的包装类也扩展 Node,fireEvent就可以解决问题。

MyBoxedButton并且ClassTwo必须提供一种注册监听器的方法。

我编写了SimpleEventHandlerProperty帮助器类,这使得这更容易。这是一个示例类的代码片段,用于SimpleEventHandlerProperty允许注册ActionEvent处理程序。

private final ObjectProperty<EventHandler<ActionEvent>> onNewSampleAction = new SimpleEventHandlerProperty<>(this,
        "onNewSampleAction", ActionEvent.ACTION, this::setEventHandler);


public final EventHandler<ActionEvent> getOnNewSampleAction() {
    return onNewSampleActionProperty().get();
}

public final void setOnNewSampleAction(EventHandler<ActionEvent> contentChanged) {
    onNewSampleActionProperty().set(contentChanged);
}

public ObjectProperty<EventHandler<ActionEvent>> onNewSampleActionProperty() {
    return onNewSampleAction;
}

然后可以根据需要注册一个监听器ClassTwoClassThree

辅助类在 Maven 中心可用:

<dependency>
    <groupId>org.drombler.commons</groupId>
    <artifactId>drombler-commons-fx-core</artifactId>
    <version>0.13</version>
</dependency>
于 2018-12-29T19:34:19.937 回答