我正试图通过计数取消选择随机单选按钮。我有10个单选按钮全部被选中,然后我想每次取消选择一个,直到计数等于0.我不知道最好的方法是什么,并且将非常感谢帮助。
伪代码
I have a score variable:
Score = a randomly generated number between 1 and 10
While score > 0
Then
Deselect a random radio button
Remove one by score
End While
添加单选按钮的代码:
@FXML
RadioButton lane1Pin1, lane1Pin2, lane1Pin3, lane1Pin4, lane1Pin5, lane1Pin6, lane1Pin7, lane1Pin8, lane1Pin9,lane1Pin10;
更新
我添加了数组列表:
<fx:define>
<ArrayList fx:id="radios">
<fx:reference source="lane1Pin1"/>
<fx:reference source="lane1Pin2"/>
<fx:reference source="lane1Pin3"/>
</ArrayList>
</fx:define>
@FXML
RadioButton lane1Pin1, lane1Pin2, lane1Pin3, lane1Pin4, lane1Pin5, lane1Pin6, lane1Pin7, lane1Pin8, lane1Pin9,lane1Pin10;
@FXML
List<RadioButton> radios = new ArrayList<>(Arrays.asList(lane1Pin1, lane1Pin2, lane1Pin3, lane1Pin4, lane1Pin5, lane1Pin6, lane1Pin7, lane1Pin8, lane1Pin9, lane1Pin10));
public void main(String[] args) {
for (RadioButton button : radios) {
button.setDisable(true);
button.setOpacity(1);
}
}
但是场景中看不到单选按钮
答案 0 :(得分:0)
你可以这样说:
Random r = new Random();
List<RadioButton> radioButtons = new ArrayList<>();
//add 10 RadioButtons
//Score = a randomly generated number between 1 and 10
int score = r.nextInt(radioButtons.size());
//While score > 0
while(score > 0){
//Then
int random = r.nextInt(radioButtons.size());
//Deselect a random radio button
radioButtons.get(random).selectedProperty().setValue(false);
radioButtons.get(random).setDisable(true);
//Remove one by score
radioButtons.remove(random);
--score;
//End while
}
我希望你需要的是
答案 1 :(得分:0)
假设您想要为用户执行一些动画,一个接一个地使用Timeline
并在一个EventHandler<ActionEvent>
中使用KeyFrame
来取消选择。这个事件处理程序遍历一个被洗牌的RadioButton
列表,并在每次执行时取消选择一个:
示例:
@Override
public void start(Stage primaryStage) throws Exception {
final int count = 10;
VBox root = new VBox();
List<RadioButton> radios = new ArrayList<>(count);
// create buttons
for (int i = 0; i < count; i++) {
RadioButton button = new RadioButton();
button.setSelected(true);
// prevent user interaction with button
button.setDisable(true);
// keep button visually the same as an enabled one
button.setOpacity(1);
radios.add(button);
}
root.getChildren().addAll(radios);
Random random = new Random();
Collections.shuffle(radios, random);
// determine number of radios to deselect
final int unselectCount = 1 + random.nextInt(count);
final Iterator<RadioButton> iterator = radios.iterator();
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(2), event -> iterator.next().setSelected(false)));
timeline.setCycleCount(unselectCount);
timeline.play();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
在包含无线电的控制器中有10个字段,可以像这样创建列表:
List<RadioButton> radios = new ArrayList<>(Arrays.asList(lane1Pin1, lane1Pin2, lane1Pin3, lane1Pin4, lane1Pin5, lane1Pin6, lane1Pin7, lane1Pin8, lane1Pin9, lane1Pin10));
或者您可以使用我的方法my answer to this other question从fxml创建List
,但这不能从SceneBuilder完成。
根据fxml的不同,您可能仍需要停用RadioButton
s:
for (RadioButton button : radios) {
// prevent user interaction with button
button.setDisable(true);
// keep button visually the same as an enabled one
button.setOpacity(1);
}
但在这种情况下,创建父母并将无线电添加为儿童并不是必需的。