标签未显示在鼠标事件JavaFx上

时间:2012-12-14 14:14:10

标签: java mouseevent javafx pie-chart

我有一个饼图,我使用本指南添加了一个鼠标监听器:

Oracle guide to pie chart

然而,当我运行我的程序并点击图表时,它什么也没做。

我尝试过System.out.println(caption.getText());并且标签的文字是正确的,但标签没有出现。

我的代码如下:

public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Imported Fruits");
        stage.setWidth(500);
        stage.setHeight(500);

        ObservableList<PieChart.Data> pieChartData =
                FXCollections.observableArrayList(
                new PieChart.Data("Grapefruit", 13),
                new PieChart.Data("Oranges", 25),
                new PieChart.Data("Plums", 10),
                new PieChart.Data("Pears", 22),
                new PieChart.Data("Apples", 30));
        final PieChart chart = new PieChart(pieChartData);
        chart.setTitle("Imported Fruits");

        ((Group) scene.getRoot()).getChildren().add(chart);

        final Label caption = new Label("");
        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()) + "%");
                        caption.setVisible(true);
                     }
                });
        }

        stage.setScene(scene);
        stage.show();
    }

}

谁能告诉我我做错了什么?

2 个答案:

答案 0 :(得分:6)

您忘记添加标签&#39;标题&#39;到根节点中的子项列表: - )

public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Imported Fruits");
        stage.setWidth(500);
        stage.setHeight(500);

        ObservableList<PieChart.Data> pieChartData =
                FXCollections.observableArrayList(
                new PieChart.Data("Grapefruit", 13),
                new PieChart.Data("Oranges", 25),
                new PieChart.Data("Plums", 10),
                new PieChart.Data("Pears", 22),
                new PieChart.Data("Apples", 30));
        final PieChart chart = new PieChart(pieChartData);
        chart.setTitle("Imported Fruits");
        final ObservableList<Node> children = ((Group) scene.getRoot()).getChildren();

        children.add(chart);

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

        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()) + "%");
                        caption.setVisible(true);
                     }
                });
        }

        stage.setScene(scene);
        stage.show();
    }

我做了以上更改,可以验证它是否有效(如下图所示)

enter image description here

答案 1 :(得分:0)

不需要,你只需添加标题,这里: 改变这一行:((Group)scene.getRoot())。getChildren()。addAll(chart); to :((Group)scene.getRoot())。getChildren()。addAll(chart,caption);