为什么 JavaFX PieChart 标签在向每个饼图切片添加事件后不显示? (Java 8)

Why does JavaFX PieChart label not show after after adding Events to each pie slice? (Java 8)

我正在学习本教程(底部名为 "Processing Mouse Events for a Pie Chart" 的部分):

https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/pie-chart.htm

而我的代码如下:

@FXML
public void initialize() {
    pieChart.setTitle("Breakdown of Customers by City");
    pieChart.setLegendSide(Side.LEFT);

    final Task<ObservableList<PieChart.Data>> task = new NumClientsAtLocationTask(new CustomerAccountDaoSelect());
    new Thread(task).start();
    task.setOnSucceeded(ae -> {

        pieChart.setData(task.getValue());

        final Label caption = new Label("");
        caption.setTextFill(Color.DARKORANGE);
        caption.setStyle("-fx-font: 24 arial;");

        for (final PieChart.Data data : pieChart.getData()) {
            data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED,
                    ae2 -> {
                        caption.setTranslateX(ae2.getSceneX());
                        caption.setTranslateY(ae2.getSceneY());
                        caption.setText(String.valueOf(data.getPieValue()));
                    });
        }
    });

}


class NumClientsAtLocationTask extends Task<ObservableList<PieChart.Data>> {
    private DaoSelect<CustomerAccount> customerAccountDaoSelect;

    NumClientsAtLocationTask(DaoSelect<CustomerAccount> customerAccountDaoSelect) {
        this.customerAccountDaoSelect = customerAccountDaoSelect;
    }

    @Override
    protected ObservableList<PieChart.Data> call() throws Exception {
        List<CustomerAccount> customerAccounts = customerAccountDaoSelect.select(RunListeners.FALSE);

        Map<String, List<CustomerAccount>> customerMap =
                customerAccounts
                        .stream()
                        .collect(Collectors.groupingBy(CustomerAccount::getCity));

        int london = customerMap.get("London").size();
        int phoenix = customerMap.get("Phoenix").size();
        int newYork = customerMap.get("New York").size();

        ObservableList<PieChart.Data> results = FXCollections.observableArrayList(
                new PieChart.Data("London", london),
                new PieChart.Data("Phoenix", phoenix),
                new PieChart.Data("New York", newYork));

        updateValue(results);
        return results;
    }
}

图表在其默认形式下显示良好,但当我在切片上单击鼠标时,标签没有出现。如果我将标签打印到控制台,它会显示正确的值,只是不会像教程中那样显示在屏幕上。有什么想法吗?

example/tutorial 很差,遗漏了一个重要的细节。您需要先将 Label 'caption' 添加到 Scene

如果您纯粹使用该教程中的示例代码,您将包括 ((Group) scene.getRoot()).getChildren().add(caption);:

final Label caption = new Label("");
((Group) scene.getRoot()).getChildren().add(caption); // Add me
caption.setTextFill(Color.DARKORANGE);
caption.setStyle("-fx-font: 24 arial;");

for (final PieChart.Data data : chart.getData()) {
    data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED,
            new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent e) {
                    caption.setTranslateX(e.getSceneX());
                    caption.setTranslateY(e.getSceneY());
                    caption.setText(String.valueOf(data.getPieValue()) + "%");
                }
            });
}