如何通过JavaFX中的事件传递参数表? [英] How to pass paremeter with an event in javafx?

查看:242
本文介绍了如何通过JavaFX中的事件传递参数表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的示例,在此示例中,我希望将参数"text"与事件一起传递(单击我的按钮"bla"时).我该怎么办?

I have this following example, where I want to pass my paremeter "text" together with my event (when my button "bla" is clicked). How can I do it?

  EventHandler<MouseEvent> handler = new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            System.out.println(text);
        }
    };

    public void test(){
    //...

    Text text = "123567";

    bla.setOnMousePressed(handler);

    //...
    }

小问题:

当我有以下内容时:

   object1.setOnMouseClicked( event -> {
        System.out.println("HELLO");
    });

   object2.setOnMouseClicked( event -> {
        System.out.println("HELLO");
    });

两个对象,对它们"setOnMouseClicked"执行相同的操作.有什么语法可以合并它们?

two objects, doing the same thing when "setOnMouseClicked" on them. is there any syntax to merge them?

推荐答案

您可以采用多种方法. 如果您要传递的参数是事件源或目标的一部分,则可以:

There is multiple ways you can go at it. If the parameter you want to pass along is part of the event source or target you can:

  • 使用getSource或getTarget
  • 检查来源或目标的类别
  • 将源/目标投射到该类
  • 通过该类的getter和setter访问参数

但是,如果参数与事件无关,则必须编写一个自定义事件:

However if the parameter has nothing to do with the event, you will have to write a custom event:

class CustomEvent extends Event {

    private String parameter;

    public static final EventType<CustomEvent> CUSTOM = new EventType(ANY, "CUSTOM");
    public CustomEvent(String parameter) {
        super(CustomEvent.CUSTOM);
        this.parameter = parameter;
    }

    public String getParameter() {
        return this.parameter;
    }

}

现在要使用该事件,您首先必须将其触发.

Now to use that event you will first have to fire it.

您可以使用

objectThatWillFireThisEvent.fireEvent(new CustomEvent("Get this parameter guys!"));

因此,您现在触发了带有参数的事件.现在简单地设置EventHandler,将其添加到将触发事件的对象的类中:

So you now fired an event with a parameter. Now to set the EventHandler simple add this to the class of the object that will fire the event:

public final void setOnCustomEvent(
        EventHandler<? super CustomEvent> value) {
    this.addEventHandler(CustomEvent.CUSTOM, value);
}

现在您可以设置事件处理程序:

Now you can set the event handler:

objectThatWillFireTheEvent.setOnCustomEvent( event -> {
    System.out.println(event.getParameter());
});

或者,如果需要,您可以使用编写问题中发布的事件处理程序的方式(如果您不想使用lambda).

Alternatively if want you can use the way to write event handlers that you posted in your question (if you don't want to use lambdas).

或者您可以只调用您编写的应处理该参数的函数:

Or you can just call a function that you wrote, that should handle that parameter:

objectThatWillFireTheEvent.setOnCustomEvent( event -> myFunction(event.getParameter) );

我希望我没有打错字.但是,如果某些问题不起作用或您还有其他问题,请随时提出! :)

I hope I didn't make any typos. But if something does not work or you have any more questions, don't hesitate to ask! :)

除此之外,我还建议您使用Google Cast(如果您还不知道的话)以及有关自定义事件的更多信息(因为我的回答只是一个起点,如果您想对自定义事件做更多疯狂的事情,最好阅读一下:D)

In addition to that I would advice you to google casting (if you do not know that already) and more on custom events (since my answer is only a starting point and if you want to do more crazy shit with custom events it's better to read up on that :D)

这是您在评论中表示的意思吗?

Is this what you meant in your comment?

由于注释会破坏布局和可读性

Since comments will destroy layout and readability

String text = "This is text!";
Button button = new Button();
object.setOnMouseClicked( event -> {
    function1(text);
    function2(button);
});

这篇关于如何通过JavaFX中的事件传递参数表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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