【问题标题】:Radio Button not showing colored text单选按钮不显示彩色文本
【发布时间】:2019-05-01 02:05:12
【问题描述】:

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

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);
                });

【问题讨论】:

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

标签: javafx radio-button


【解决方案1】:

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

您需要听取所选RadioButton变化并采取相应行动。这很容易通过向ToggleGroupselectedToggleProperty() 添加一个监听器来完成。

删除您的 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);

        }

    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 2017-04-19
    • 2015-08-23
    相关资源
    最近更新 更多