簡體   English   中英

帶有鼠標單擊位置標簽的Oracles JavaFX示例錯誤?

[英]Oracles JavaFX example with mouse click position label wrong?

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

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

將標簽放置在單擊鼠標的位置。。但這根本不起作用。 請參見以下打印屏幕以獲取證明:

在此處輸入圖片說明

您引用的示例代碼中PieChart和標題Label都直接放置在作為場景根的Group中。 因此,在應用轉換之前, Label的位置為(0,0)Scene左上角),因此通過將其轉換為(e.getSceneX(), e.getSceneY())將其移動到鼠標的位置。

如果您的布局不同,則相同的計算不一定會起作用。 為了更廣泛的解決方案,放在一個圖表,字幕Group ,然后調用sceneToLocal(...)Group翻譯的現場協調,以在正確的坐標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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM