gpt4 book ai didi

java - Oracles JavaFX 示例中鼠标单击位置标签错误?

转载 作者:太空宇宙 更新时间:2023-11-04 06:35:49 24 4
gpt4 key购买 nike

http://docs.oracle.com/javafx/2/charts/pie-chart.htm Oracle 建议使用

caption.setTranslateX(e.getSceneX());
caption.setTranslateY(e.getSceneY());

在单击鼠标的位置放置一个标签。但这根本不起作用。请参阅此打印屏幕以获取证明:

enter image description here

最佳答案

code for the example you citePieChart 和标题 Label 都直接放置在作为场景根的 Group 中。因此,在应用转换之前,Label 的位置为 (0,0)(Scene 的左上角),因此通过 (e.getSceneX(), e.getSceneY()) 对其进行平移,将其移动到鼠标的位置。

如果您的布局不同,那么相同的计算不一定有效。对于更通用的解决方案,请将图表和标题放入 Group 中,然后在 Group 上调用 sceneToLocal(...) 将场景坐标转换为 Group 中的正确坐标:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class PieChartSample extends Application {

@Override
public void start(Stage stage) {

BorderPane root = new BorderPane();

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 Label caption = new Label("");
caption.setTextFill(Color.DARKORANGE);
caption.setStyle("-fx-font: 24 arial;");

Group chartWithCaption = new Group(chart, caption);

for (final PieChart.Data data : chart.getData()) {
data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED,
new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent e) {
Point2D locationInScene = new Point2D(e.getSceneX(), e.getSceneY());
Point2D locationInParent = chartWithCaption.sceneToLocal(locationInScene);

caption.relocate(locationInParent.getX(), locationInParent.getY());

caption.setText(String.valueOf(data.getPieValue()) + "%");
}
});
}

root.setCenter(chartWithCaption);

// Just some stuff to change the overall layout:
HBox controls = new HBox(5);
controls.setPadding(new Insets(10));
controls.setAlignment(Pos.CENTER);
controls.getChildren().addAll(new Label("Some other stuff here"), new TextField(), new Button("OK"));
root.setTop(controls);
root.setPadding(new Insets(0, 0, 10, 40));
root.setLeft(new Circle(25, Color.SALMON));

Scene scene = new Scene(root);
stage.setTitle("Imported Fruits");
stage.setWidth(600);
stage.setHeight(500);

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

public static void main(String[] args) {
launch(args);
}
}

关于java - Oracles JavaFX 示例中鼠标单击位置标签错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25405366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com