JavaFX:初始化单选按钮选择状态
我需要保存多个单选按钮的选择状态,以便在稍后返回场景时可以看到选择了哪个单选按钮。这与用户数据无关,而是要看它是否被选中。现在我知道如何让它工作,但有很多混乱的复制和粘贴。每个切换组都有类似的情况:JavaFX:初始化单选按钮选择状态,java,javafx,radio-button,Java,Javafx,Radio Button,我需要保存多个单选按钮的选择状态,以便在稍后返回场景时可以看到选择了哪个单选按钮。这与用户数据无关,而是要看它是否被选中。现在我知道如何让它工作,但有很多混乱的复制和粘贴。每个切换组都有类似的情况: @FXML private RadioButton rb1; @FXML private RadioButton rb2; public static int[] status = new int[600]; // to save it if (rb1.getSelect(true)){ st
@FXML private RadioButton rb1;
@FXML private RadioButton rb2;
public static int[] status = new int[600];
// to save it
if (rb1.getSelect(true)){
status[0] = 1;
} else {
status[0] = 0;
}
// to load it
if (status[0] == 1){
rb1.setSelected(true);
} else {
rb2.setSelected(true);
}
问题是我设计了一个调查,有300多个问题,答案是二元的。所以我有600多个不同的单选按钮。以这种方式实施它需要几个小时
有什么聪明的方法吗?我很感激你的建议。提前谢谢 下面是一个SCVExample,它包含了基于我的评论的最简单的实现:它定义了一个模型(
Survey
和Question
),然后将GUI绑定到此模型
public class Radios extends Application {
class Survey {
private ObservableList<Question> questions = FXCollections.observableArrayList();
public ObservableList<Question> getQuestions() {
return questions;
}
}
class Question {
private StringProperty text = new SimpleStringProperty("");
private BooleanProperty answer = new SimpleBooleanProperty(false);
public Question(String text) {
setText(text);
}
public boolean isAnswer() {
return answer.get();
}
public BooleanProperty answerProperty() {
return answer;
}
public void setAnswer(boolean answer) {
this.answer.set(answer);
}
public String getText() {
return text.get();
}
public StringProperty textProperty() {
return text;
}
public void setText(String text) {
this.text.set(text);
}
}
@Override
public void start(Stage primaryStage) throws Exception {
// Model
Survey survey = new Survey();
for (int i = 0; i<300; i++) {
Question question = new Question("Do you like number " + i + "?");
question.answerProperty().addListener((obs, oldval,newval) -> {
System.out.println("Question: " + question.getText() + " answer changed from " + oldval + " to " + newval);
});
survey.getQuestions().add(question);
}
// View
VBox root = new VBox();
root.setSpacing(10);
for (Question question : survey.getQuestions()) {
VBox vBox = new VBox();
vBox.setSpacing(5);
HBox answerHBox = new HBox();
answerHBox.setSpacing(20);
vBox.getChildren().addAll(new Label(question.getText()), answerHBox);
RadioButton yes = new RadioButton("Yes");
RadioButton no = new RadioButton("No");
ToggleGroup toggleGroup = new ToggleGroup();
yes.setToggleGroup(toggleGroup);
no.setToggleGroup(toggleGroup);
answerHBox.getChildren().addAll(yes, no);
yes.setSelected(question.isAnswer());
no.setSelected(!question.isAnswer());
toggleGroup.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
question.setAnswer(newValue.equals(yes));
});
root.getChildren().add(vBox);
}
Scene scene = new Scene(new ScrollPane(root), 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
通常,GUI不应该存储任何状态,但它应该表示一个模型,模型的状态应该决定GUI元素的状态。类似于:您有一个
调查
类,其中包含问题
实例。每个问题
实例都有一个布尔属性
(答案),并在GUI上表示为一个单选按钮
。每个radiobutton的checked属性现在可以双向绑定到其中一个问题AnswerProperty。然后只存储调查
(在内存中,持久化-如您所愿)。@DVarga但我必须创建300个问题实例,每个实例都有getter和setter?您可以举一个如何将BooleanProperty绑定到RadioButton的例子吗?可以将两个RadioButton绑定到一个问题上(因为我对每个问题都有两个可能的答案),或者是否有一个选项可以将ToggleGroup本身绑定到问题上?为您更新了答案。
Question: Do you like number 1? answer changed from false to true
Question: Do you like number 3? answer changed from false to true
Question: Do you like number 6? answer changed from false to true
Question: Do you like number 8? answer changed from false to true
Question: Do you like number 8? answer changed from true to false
Question: Do you like number 8? answer changed from false to true