有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    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);
        }
    }