【问题标题】:JavaFX RadioButton's not working correctly?JavaFX RadioButton 不能正常工作?
【发布时间】:2015-12-07 08:43:46
【问题描述】:

这是我程序的 UI 部分:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class TestClass extends Application {

Scene scene;
String fileName;
Button button;
Label fileLabel, dotText;
TextField fileNameText;
RadioButton totalRadio, aRadio, bRadio, cRadio;
ToggleGroup sumGroup;


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

@Override
public void start(Stage primaryStage) {
    GridPane layout = new GridPane();
    layout.setVgap(5);
    layout.setHgap(5);

    //Adds button
    button = new Button("Sum file");
    button.setOnAction(e -> {
        fileName = "C:\\Users\\Luke\\Desktop\\" + fileNameText.getText() + ".txt";
    });
    layout.setConstraints(button, 1, 1);

    //Adds label
    fileLabel = new Label("File name: ");
    layout.setConstraints(fileLabel, 0, 0);

    //Adds TextField
    fileNameText = new TextField();
    fileNameText.setPrefWidth(100);
    layout.setConstraints(fileNameText, 1, 0);

    //Adds Label
    dotText = new Label(".txt");
    layout.setConstraints(dotText, 2, 0);

    //Makes Button group
    sumGroup = new ToggleGroup();

    //Adding all radio buttons
    totalRadio = new RadioButton("Total");
    totalRadio.setToggleGroup(sumGroup);
    totalRadio.setSelected(true);
    layout.setConstraints(totalRadio, 1, 2);

    aRadio = new RadioButton("A Branch");
    totalRadio.setToggleGroup(sumGroup);
    layout.setConstraints(aRadio, 1, 3);

    bRadio = new RadioButton("B Branch");
    totalRadio.setToggleGroup(sumGroup);
    layout.setConstraints(bRadio, 1, 4);

    cRadio = new RadioButton("C Branch");
    totalRadio.setToggleGroup(sumGroup);
    layout.setConstraints(cRadio, 1, 5);

    //Adds components to scene
    layout.getChildren().addAll(fileLabel, fileNameText, button, dotText, totalRadio, aRadio, bRadio, cRadio);
    scene = new Scene(layout, 230, 185);

    primaryStage.setScene(scene);
    primaryStage.show();
    }
}

我的问题是单选按钮无法正常工作。当您单击一个按钮时,除非您再次单击它,否则它将保持选中状态,并且在按下另一个按钮时不会取消选择。我查看了许多其他线程,但其中大多数只是关于如何将 RadioButtons 添加到 ToggleGroup 等。我有 Toggle 组,并添加了它们,所以有什么问题? (我正在学习 JavaFX,我通常做 GUI 的摇摆。)

【问题讨论】:

    标签: java javafx radio-button


    【解决方案1】:

    您多次设置了 totalRadio.setToggleGroup 但未在 aRadio、bRadio、cRadio 上设置。

    改为:

    aRadio = new RadioButton("A Branch");
    aRadio.setToggleGroup(sumGroup);
    layout.setConstraints(aRadio, 1, 3);
    
    bRadio = new RadioButton("B Branch");
    bRadio.setToggleGroup(sumGroup);
    layout.setConstraints(bRadio, 1, 4);
    
    cRadio = new RadioButton("C Branch");
    cRadio.setToggleGroup(sumGroup);
    layout.setConstraints(cRadio, 1, 5);
    

    【讨论】:

    • 啊,谢谢。当我复制和粘贴时,这样的细节通常会在我脑海中浮现。我认为我的问题更多是设置类型的交易!
    猜你喜欢
    • 2013-03-30
    • 2010-11-30
    • 2021-10-02
    • 2017-08-06
    • 2013-11-22
    • 1970-01-01
    • 2013-09-10
    • 2017-04-05
    • 2013-07-25
    相关资源
    最近更新 更多