标签未显示在鼠标事件JavaFx上 [英] Label not showing on mouse event JavaFx
本文介绍了标签未显示在鼠标事件JavaFx上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个饼图,我使用本指南添加了一个鼠标监听器:
I have a pie chart where i have added a mouse listener using this guide:
然而,当我运行我的程序并点击图表它不做任何事情。
However when i run my program and click on the chart it doesnt do anything.
我试过System.out.println(caption.getText());并且标签的文字是正确的,但标签没有显示。
I have tried to System.out.println(caption.getText()); and the text of the label is correct however the label is just not showing up.
我的代码如下:
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();
}
}
谁能告诉我我做了什么错误?
Can anyone tell me what i did wrong?
推荐答案
您忘了将标题'caption'添加到根节点中的子项列表中: - )
You forgot to add the label 'caption' to the list of children in your root node :-)
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();
}
我已做出上述更改并可验证其是否有效(如下所示)如下图所示)
I have made the above change and can verify that it works (as depicted by the image below)
这篇关于标签未显示在鼠标事件JavaFx上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文