gpt4 book ai didi

java - 我是否必须阻止循环操作处理程序调用

转载 作者:行者123 更新时间:2023-11-30 02:03:26 25 4
gpt4 key购买 nike

无法找到找到现有答案的正确方法,因此这里是描述我的情况的草图示例:

public class MyRButton {
RadioButton rb;
MyRButton (RadioButton _rb) {
rb = new RadioButton(_rb);
rb.setOnAction(this::handleSelectedAction);
}

handleSelectedAction(ActionEvent _selected) {
// DO if RadioButton rb is selected directly (by mouse etc.)
// Some external actions are able to reset isSelected() state of the
// RadioButton during action handling, so to make sure it's still
// selected after method processing:
rb.setSelected(true); // HERE IS THE DOUBT IF THIS OPERATOR CALLS
// handleSelectedAction(ActionEvent _selected) RECURSIVELY
}
}

我必须用禁用/启用操作处理程序指令包围 rb.setSelected(true) 吗?

    handleSelectedAction(ActionEvent _selected) {
// DO if RadioButton rb is selected directly (by mouse etc.)
rb.setOnAction(null);
rb.setSelected(true);
rb.setOnAction(this::handleSelectedAction);
}

原始代码运行良好,但我怀疑handleSelectedAction方法是否永久在后台运行。

最佳答案

好的,伙计们,根据 kleopatra 的要求,我编写了一个可以运行的简短示例:

    package rbexample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class RBExample extends Application {
RadioButton rBtn;
Button btn;

@Override
public void start(Stage primaryStage) {
rBtn = new RadioButton();
rBtn.setText("Select Me");
rBtn.setOnAction(this::handleRBSelectedAction);
btn = new Button();
btn.setText("Push Me");
btn.setOnAction(this::handleBPushedAction);

VBox root = new VBox(2);
root.getChildren().add(rBtn);
root.getChildren().add(btn);
Scene scene = new Scene(root, 150, 50);
primaryStage.setTitle("RBExample");
primaryStage.setScene(scene);
primaryStage.show();
}

private void handleRBSelectedAction(ActionEvent event) {
if (rBtn.isSelected()) {
System.out.println("RB Selected directly");
}
}
private void handleBPushedAction(ActionEvent event) {
rBtn.setSelected(true);
System.out.println("RB Selected by button");
}

public static void main(String[] args) {
launch(args);
}
}

从此示例中可以看出,如果操作,则不会调用 RadioButton 事件处理程序

rBtn.setSelected(true);

在外部执行(在本例中是从按钮操作处理程序)。因此我们不需要禁用并重新启用 RadioButton 事件处理程序。

关于 RadioButton 事件处理程序中需要 setSelected(true) 以确保 RadioButton 真正被选中,这取决于其余代码是否有一些后台进程可以拦截直接 RadioButton 状态更改。

关于java - 我是否必须阻止循环操作处理程序调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52039196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com