通过箭头键导航时,在ToggleGroup中选择一个RadioButton。

huangapple go评论70阅读模式
英文:

Selecting a RadioButton in a ToggleGroup whenever it is navigated to with the arrow keys

问题

我有一个带有一系列共享ToggleGroupRadioButton的程序。以下是一个简化版本:

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        ToggleGroup toggleGroup = new ToggleGroup();

        RadioButton button1 = new RadioButton();
        button1.setText("按钮1");
        button1.setOnAction(this::printSelectedRadioButton);
        button1.setToggleGroup(toggleGroup);

        RadioButton button2 = new RadioButton();
        button2.setText("按钮2");
        button2.setOnAction(this::printSelectedRadioButton);
        button2.setToggleGroup(toggleGroup);

        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        root.getChildren().addAll(button1, button2);

        primaryStage.setScene(new Scene(root, 100, 100));
        primaryStage.show();
    }

    private void printSelectedRadioButton(ActionEvent actionEvent) {
        RadioButton radioButton = (RadioButton) actionEvent.getSource();
        System.out.println(radioButton.getText());
    }

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

通过箭头键导航时,在ToggleGroup中选择一个RadioButton。

当我点击一个RadioButton时,会触发一个ActionEvent并调用printSelectedRadioButton()方法。然而,一旦我点击了一个RadioButton,如果我使用箭头键导航到另一个按钮,ActionEvent不会触发,方法也不会被调用。我希望导航到特定按钮具有与单击它相同的效果。我该如何做到这一点?

英文:

I have a program with a series of RadioButtons that share a ToggleGroup. The following is a simplified version:

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        ToggleGroup toggleGroup = new ToggleGroup();

        RadioButton button1 = new RadioButton();
        button1.setText("Button 1");
        button1.setOnAction(this::printSelectedRadioButton);
        button1.setToggleGroup(toggleGroup);

        RadioButton button2 = new RadioButton();
        button2.setText("Button 2");
        button2.setOnAction(this::printSelectedRadioButton);
        button2.setToggleGroup(toggleGroup);

        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        root.getChildren().addAll(button1, button2);

        primaryStage.setScene(new Scene(root, 100, 100));
        primaryStage.show();
    }

    private void printSelectedRadioButton(ActionEvent actionEvent) {
        RadioButton radioButton = (RadioButton) actionEvent.getSource();
        System.out.println(radioButton.getText());
    }

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

通过箭头键导航时,在ToggleGroup中选择一个RadioButton。

When I click on a RadioButton, an ActionEvent is fired and the printSelectedRadioButton() method is called. However, once I have clicked on a RadioButton, if I navigate to the other one using the arrow keys, the ActionEvent is not fired and the method is not called. I want navigating to a particular button to have the same effect as clicking on it. How would I do this?

答案1

得分: 2

RadioButton#setOnAction仅在点击时起作用如果您想获取选中的单选按钮您需要向ToggleGroup添加***ChangeListener***

ToggleGroup toggleGroup = new ToggleGroup();
toggleGroup.selectedToggleProperty().addListener((observableValue, oldToggle, newToggle) -> {
    if (toggleGroup.getSelectedToggle() != null) {
        System.out.println("选中的单选按钮:" + toggleGroup.getSelectedToggle());
    }
});
英文:

RadioButton#setOnAction works only on click. If you want to get selected radio button you have to add ChangeListener to ToggleGroup.

    ToggleGroup toggleGroup = new ToggleGroup();
    toggleGroup.selectedToggleProperty().addListener((observableValue, oldToggle, newToggle) -> {
        if (toggleGroup.getSelectedToggle() != null) {
            System.out.println("selected radio button: " + toggleGroup.getSelectedToggle());
        }
    });

huangapple
  • 本文由 发表于 2020年8月15日 03:53:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63419262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定