Checkbox EventHandler不更改标签中的文本(JavaFX)

时间:2017-11-21 11:20:09

标签: java checkbox javafx eventhandler

我只是在学习Java,到目前为止一切顺利。我正在学习JavaFX的基础知识,并编写了一个只显示一些Checkboxes控件和标签的程序。当用户点击其中一个(或任何一个)复选框时,我正在尝试使用名为响应的Label来更改文本。下面的代码仅显示了bananaCB复选框的EventHandler。

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事件处理程序,它按预期工作。我在上面的代码中注释了它,但它看起来很乱。我会在这里发布。

        bananaCB.setOnAction(new EventHandler<ActionEvent>() {
        @Override
       public void handle(ActionEvent event) {
            response.setText("Banana Selected");
        }
    });

2 个答案:

答案 0 :(得分:0)

我认为正确的条件是bananaCB.ischecked。

试试吧。由于方法调用错误,标签可能没有改变

答案 1 :(得分:0)

您可以将应用程序类的事件作为事件处理程序附加到多个CheckBox es,但我不推荐它。此外,您需要使用setOnAction方法实际注册处理程序:

bananaCB.setOnAction(this);
mangoCB.setOnAction(this);
...

此外,使用相同的事件处理程序并检查所有可能的来源是不好的做法 我推荐在ChangeListener属性中使用selected来代替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; } 使用不同的lambdas / anonymus类:

bananaCB = createFruitCheckBox("Banana");
mangoCB = createFruitCheckBox("Mango");
...
// This will come from the query, but for the example
$array = Array("aaa, bbb", "ccc, ddd, aaa", "eee, aaa, ccc", "ddd");

// Initilize an empty array for holding count values
$counterArray = Array();

// Loop through each "table row"
foreach ($array as $row)
{
    // Split the row on the comma
    $splitRow = explode(',', $row);

    // Loop through each item found on the row
    foreach ($splitRow as $item)
    {      
        // Trim to get rid of any leading/trailing spaces
        $arrayKey = trim($item);

        // Check if the array contains the values already, if yes increment otherwise creaet a record with a initial value of 1
        if (!array_key_exists($arrayKey, $counterArray))
            $counterArray[$arrayKey] = 1;
        else
            $counterArray[$arrayKey]++;
    }
}
相关问题