首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >肋事件处理

肋事件处理
EN

Stack Overflow用户
提问于 2017-12-14 13:21:26
回答 1查看 93关注 0票数 0

我想摆脱的问题如下:

我打算设置一个setup事件处理链,以解决JavaFX缺少a命令的问题。特别是,点击菜单时,仍然会触发一个ActionEvent而不是自写的MilvaLabActionEvent。守则:

事件类

代码语言:javascript
复制
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;
    }
}

我的EventHandler:

代码语言:javascript
复制
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
        }
}

我想我要写的是这门课。

代码语言:javascript
复制
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的初始化:

代码语言:javascript
复制
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!");
        });

嗯,到现在为止,当我点击ActionEvent时,我得到了“我已经启动了一个MilvaLabMenuItem”,其他什么都没有发生。(查看已经使用调试器的东西)。我想要发生的是,很明显,MilvaLabEventHandler被称为。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-15 10:35:01

我又想出来了。不过,我声明了两个EventTypes,只需要一个。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47814406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档