复选框事件处理程序不更改标签中的文本 (JavaFX)
Checkbox EventHandler not changing text in label (JavaFX)
我只是在学习 Java,目前一切进展顺利。我正在学习 JavaFX 的基础知识,并编写了一个仅显示一些复选框控件和标签的程序。当用户单击一个(或任何一个)复选框时,我试图获取名为响应的标签以更改文本。下面的代码仅显示了 bananaCB 复选框的事件处理程序。
public class Main extends Application implements EventHandler {
private Label title;
private Label response;
private Label selected;
private CheckBox bananaCB;
private CheckBox mangoCB;
private CheckBox papayaCB;
private CheckBox grapfruitCB;
private String fruits;
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Favorite Fruit");
title = new Label("What fruits do you like?");
response = new Label("");
selected = new Label("");
bananaCB = new CheckBox("Banana");
mangoCB = new CheckBox("Mango");
papayaCB = new CheckBox("Papaya");
grapfruitCB = new CheckBox("Grapfruit");
//Setup the stage and Scene
FlowPane flowPaneRoot = new FlowPane(Orientation.VERTICAL,5,5);
flowPaneRoot.setAlignment(Pos.CENTER);
//We add all of our controls to flowPaneRoot
flowPaneRoot.getChildren().add(title);
flowPaneRoot.getChildren().addAll(bananaCB,mangoCB,papayaCB,grapfruitCB);
flowPaneRoot.getChildren().add(response);
flowPaneRoot.getChildren().add(selected);
//Attach eventListeners to our checkboxes.
Scene scene = new Scene(flowPaneRoot,400,250);
primaryStage.setScene(scene);
primaryStage.show();
}
public void showAll(){
fruits = "";
}
public static void main(String[] args) {
launch(args);
}
@Override
public void handle(Event event) {
Object fruitSelected = event.getSource();
if (bananaCB.equals(fruitSelected)) {
if (bananaCB.isSelected()) {
response.setText("Banana Selected");
} else {
response.setText("Banana cleared");
}
}
}
}
我确实尝试了 setOnAction 事件处理程序,它按预期工作。我在上面的代码中将其注释掉了,但它弄乱了它的外观。我会 post 放在这里。
bananaCB.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
response.setText("Banana Selected");
}
});
我认为合适的条件是bananaCB.ischecked。
试试吧。有可能标签没有改变是因为错误的方法调用
您可以将应用程序 class 的事件作为事件处理程序附加到多个 CheckBox
es,但我不推荐这样做。此外,您需要使用 setOnAction
方法实际注册处理程序:
bananaCB.setOnAction(this);
mangoCB.setOnAction(this);
...
此外,使用相同的事件处理程序并检查所有可能的来源是不好的做法。
在这种情况下,我建议对 selected
属性使用 ChangeListener
s,并为每个 CheckBox
:
使用不同的 lambdas/anonymus classes
private static CheckBox createFruitCheckBox(final String text) {
CheckBox result = new CheckBox(text);
ChangeListener<Boolean> listener = (observable, oldValue, newValue) ->
response.setText(newValue ? text + " Selected" : text + " cleared");
result.selectedProperty().addListener(listener);
return result;
}
bananaCB = createFruitCheckBox("Banana");
mangoCB = createFruitCheckBox("Mango");
...
我只是在学习 Java,目前一切进展顺利。我正在学习 JavaFX 的基础知识,并编写了一个仅显示一些复选框控件和标签的程序。当用户单击一个(或任何一个)复选框时,我试图获取名为响应的标签以更改文本。下面的代码仅显示了 bananaCB 复选框的事件处理程序。
public class Main extends Application implements EventHandler {
private Label title;
private Label response;
private Label selected;
private CheckBox bananaCB;
private CheckBox mangoCB;
private CheckBox papayaCB;
private CheckBox grapfruitCB;
private String fruits;
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Favorite Fruit");
title = new Label("What fruits do you like?");
response = new Label("");
selected = new Label("");
bananaCB = new CheckBox("Banana");
mangoCB = new CheckBox("Mango");
papayaCB = new CheckBox("Papaya");
grapfruitCB = new CheckBox("Grapfruit");
//Setup the stage and Scene
FlowPane flowPaneRoot = new FlowPane(Orientation.VERTICAL,5,5);
flowPaneRoot.setAlignment(Pos.CENTER);
//We add all of our controls to flowPaneRoot
flowPaneRoot.getChildren().add(title);
flowPaneRoot.getChildren().addAll(bananaCB,mangoCB,papayaCB,grapfruitCB);
flowPaneRoot.getChildren().add(response);
flowPaneRoot.getChildren().add(selected);
//Attach eventListeners to our checkboxes.
Scene scene = new Scene(flowPaneRoot,400,250);
primaryStage.setScene(scene);
primaryStage.show();
}
public void showAll(){
fruits = "";
}
public static void main(String[] args) {
launch(args);
}
@Override
public void handle(Event event) {
Object fruitSelected = event.getSource();
if (bananaCB.equals(fruitSelected)) {
if (bananaCB.isSelected()) {
response.setText("Banana Selected");
} else {
response.setText("Banana cleared");
}
}
}
}
我确实尝试了 setOnAction 事件处理程序,它按预期工作。我在上面的代码中将其注释掉了,但它弄乱了它的外观。我会 post 放在这里。
bananaCB.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
response.setText("Banana Selected");
}
});
我认为合适的条件是bananaCB.ischecked。
试试吧。有可能标签没有改变是因为错误的方法调用
您可以将应用程序 class 的事件作为事件处理程序附加到多个 CheckBox
es,但我不推荐这样做。此外,您需要使用 setOnAction
方法实际注册处理程序:
bananaCB.setOnAction(this);
mangoCB.setOnAction(this);
...
此外,使用相同的事件处理程序并检查所有可能的来源是不好的做法。
在这种情况下,我建议对 selected
属性使用 ChangeListener
s,并为每个 CheckBox
:
private static CheckBox createFruitCheckBox(final String text) {
CheckBox result = new CheckBox(text);
ChangeListener<Boolean> listener = (observable, oldValue, newValue) ->
response.setText(newValue ? text + " Selected" : text + " cleared");
result.selectedProperty().addListener(listener);
return result;
}
bananaCB = createFruitCheckBox("Banana");
mangoCB = createFruitCheckBox("Mango");
...