【发布时间】: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