JavaFX将ActionListener添加到按钮 [英] Javafx adding ActionListener to button

查看:190
本文介绍了JavaFX将ActionListener添加到按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

button.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent e) {
        label.setText("Accepted");
    }
});

在上面的代码中,我们定义了按下按钮时将发生的情况.一切都很好,但是我想创建新的ActionListener,然后将其添加到我的按钮中. 通常,在JButton中,我可以像这样添加ActionListener:

In the code above we are defining what will happen when we press the button. This is all good but I wanna create new ActionListener and then add it to my button. Normally in JButton I can just add ActionListener like this:

button.addActionListener(someControllerClass.createButtonListener());

在上面的代码中,createButtonListener()返回ActionListener.

In code above createButtonListener() returns ActionListener.

我的问题是:JButton addActionListener等效于什么?

My question is: What is the equivalent of JButton addActionListener ?

推荐答案

我认为这是我应该做的.创建处理程序:

I think this is how I should do. Creating the handler:

public EventHandler<Event> createSolButtonHandler()
{
    btnSolHandler = new EventHandler<Event>() {

        @Override
        public void handle(Event event) {
            System.out.println("Pressed!");
            biddingHelperFrame.getBtnSag().setVisible(false);
        }
    };
    return btnSolHandler;
}

将处理程序添加到按钮:

Adding Handler to button:

btnSol.addEventHandler(MouseEvent.MOUSE_CLICKED, biddingHelperFrameController.createSolButtonHandler());

这篇关于JavaFX将ActionListener添加到按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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