【问题标题】:Costum Event Handling自定义事件处理
【发布时间】:2018-05-28 14:31:18
【问题描述】:

我想摆脱的问题如下:

我打算设置一个 costum 事件处理链作为 JavaFX 缺乏动作命令的解决方法。 尤其是,一个菜单项在单击它时仍然会触发ActionEvent,而不是自写的MilvaLabActionEvent。 代码:

事件类

package jpt.gui.items;

import javafx.event.ActionEvent;

public class MilvaLabActionEvent extends ActionEvent {
    private static final long serialVersionUID = 6757067652205246280L;

    private String actionCommand ="";

    public MilvaLabActionEvent(String actionCommand2) {
        setActionCommand(actionCommand2);
    }

    public MilvaLabActionEvent() {}

    public String getActionCommand() {
        return actionCommand;
    }

    public void setActionCommand(String actioncommand) {
        this.actionCommand = actioncommand;
    }
}

我的事件处理器:

package jpt.gui.items;

import javafx.event.EventHandler;
import jpt.MilvaLabGlobal;
import jpt.MilvaLabKonst;
import jpt.handle.MilvaLabDateiHandle;
import jpt.handle.MilvaLabEinHandle;
import jpt.handle.MilvaLabHilfeHandle;
import jpt.handle.MilvaLabMilvaHandle;
import jpt.handle.MilvaLabRvAnwendungHandle;
import jpt.handle.MilvaLabrvTextHandle;
import jpt.log4j.MilvaLabLogger;

public class MilvaLabEventHandler implements EventHandler<MilvaLabActionEvent>{

    @Override
    public void handle(MilvaLabActionEvent event) {     
        // the command string of the menu item
        final String sCmd = event.getActionCommand();

        if (sCmd.charAt(0) == 'M')
        {//doing something here
        }
}

我想我要写的服装 MenuItem-Class。

package jpt.gui.items;

import javafx.event.Event;
import javafx.scene.control.MenuItem;

public class MilvaLabMenuItem extends MenuItem {
    private String actionCommand;

    public MilvaLabMenuItem(String sText) {
        this.setText(sText);
    }
    @Override
    public void fire() {
        Event.fireEvent(this, new MilvaLabActionEvent(getActionCommand()));

    }

    public String getActionCommand() {
        return actionCommand;
    }
    public void setActionCommand(String actionCommand) {
        this.actionCommand = actionCommand;
    }
}

以及服装MenuItem的初始化:

final MilvaLabMenuItem jmi = new MilvaLabMenuItem("I am a menuItem");
        jmi.addEventHandler(evtype, new MilvaLabEventHandler());
        jmi.setOnAction((event) -> {
            System.out.print("I have fired an ActionEvent!");
        });

嗯,到目前为止,当我单击 MilvaLabMenuItem 时,我得到“我已经触发了一个 ActionEvent”,没有其他任何反应。 (已经使用调试器查看了那个东西)。 我想要发生的是,显然,MilvaLabEventHandler 被调用。

【问题讨论】:

    标签: event-handling javafx-8


    【解决方案1】:

    我又想通了。 我声明了两个EventTypes,但只需要一个。

    这帮助我找到了解决方案,不过,他们使用 Nodes 而不是 MenuItems。 How to emit and handle custom events?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 2014-12-19
      相关资源
      最近更新 更多