button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
label.setText("Accepted");
}
});
在上面的代码中,我们定义了按下按钮时会发生什么。这一切都很好,但我想创建新的 ActionListener,然后将其添加到我的按钮。通常在 JButton 中我可以像这样添加 ActionListener:
button.addActionListener(someControllerClass.createButtonListener());
在上面的代码中,createButtonListener() 返回 ActionListener。
我的问题是:JButton addActionListener 的等价物是什么?
原文由 MertG 发布,翻译遵循 CC BY-SA 4.0 许可协议
我觉得这是我应该做的。创建处理程序:
将处理程序添加到按钮: