Javafx 单选按钮不显示彩色文本

Javafx 单选按钮不显示彩色文本,javafx,radio-button,Javafx,Radio Button,我必须用一个单选按钮来制作三个单选按钮:红色、蓝色和绿色。单选按钮不会更改为这些颜色。除这些更改外,其他更改必须保持黑色字体。我注释掉了红色的setTextFill字体,没有任何效果。我还评论了黑色字体,它们没有产生效果。选举有效 import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.Radio

我必须用一个单选按钮来制作三个单选按钮:红色、蓝色和绿色。单选按钮不会更改为这些颜色。除这些更改外,其他更改必须保持黑色字体。我注释掉了红色的setTextFill字体,没有任何效果。我还评论了黑色字体,它们没有产生效果。选举有效

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ButtonRadio extends Application {

    @Override
    public void start(Stage primaryStage) {
        // primary stage

        primaryStage.setTitle("javaFX");
        // label for text

        Label labelfirst = new Label("Choose a button");

        // vBox for buttons
        VBox layout = new VBox(3);
        // radio buttons

        RadioButton radio1, radio2, radio3;
        radio1 = new RadioButton("Red");
        radio2 = new RadioButton("Blue");
        radio3 = new RadioButton("Green");
        // ToggleGroup for entering

        ToggleGroup group = new ToggleGroup();
        // radio button variables of toggle groups 

        radio1.setToggleGroup(group);
        radio2.setToggleGroup(group);
        radio3.setToggleGroup(group);
        // if statements for radio buttons and fonts red, blue, green
        if (group.getSelectedToggle() != null) {

            if (radio1.isSelected()) {
                radio1.setTextFill(Color.RED); 
                radio2.setTextFill(Color.BLACK);
            radio3.setTextFill(Color.BLACK);

        } else if (radio2.isSelected()) {
            radio2.setTextFill(Color.BLUE);
            radio1.setTextFill(Color.BLACK);
            radio3.setTextFill(Color.BLACK);
        }
        else if (radio3.isSelected())
        {
            radio3.setTextFill(Color.GREEN);
            radio1.setTextFill(Color.BLACK);
            radio2.setTextFill(Color.BLACK);
        }
        }
        // layout to put in parent
        layout.getChildren().addAll(labelfirst, radio1, radio2, radio3);
        // put in scene and stage to show
        Scene scene1 = new Scene(layout, 400, 250);
        primaryStage.setScene(scene1);
        primaryStage.show();
    }

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

}
编辑:

我尝试添加一个侦听器。它仍然不起作用

                if (radio1.isSelected()) {
                radio1.setOnAction((event) -> {
                radio1.setTextFill(Color.RED);
                });

您的
if
语句只运行一次,仅在应用程序首次运行时检查
单选按钮的选定状态

您需要收听对所选的
单选按钮的更改,并采取相应的行动。通过将侦听器添加到
ToggleGroup
selectedToggleProperty()
,可以轻松完成此操作

删除
if
块,并将其替换为以下内容:

    group.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {

        // Set the previously-selected RadioButton text to BLACK
        if (oldValue != null) ((RadioButton) oldValue).setTextFill(Color.BLACK);

        // Set the color for the newly-selected RadioButton
        if (newValue.equals(radio1)) {
            ((RadioButton) newValue).setTextFill(Color.RED);
        } else if (newValue.equals(radio2)) {
            ((RadioButton) newValue).setTextFill(Color.BLUE);

        } else if (newValue.equals(radio3)) {
            ((RadioButton) newValue).setTextFill(Color.GREEN);

        }

    });

看起来您需要一个侦听器。您添加的“侦听器”不是侦听器。。。请参阅下面我的解决方案。