Javafx点击Circle并获得它的参考 [英] Javafx click on a Circle and get it's reference

查看:172
本文介绍了Javafx点击Circle并获得它的参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在舞台上有一组节点,圈子。
我希望能够点击其中一个并选择它(只需获取它的参考,这样我就可以移动它,改变颜色等。)

I have a set of Nodes, Circles, on the stage. I want to be able to click on one of them and 'select it' (just get a reference to it so I can move it around, change color etc.)

    Pane root = new Pane();
    root.getChildren().addAll( /* an array of Circle objects */ );

    Scene scene = new Scene(root, 500, 500, BACKGROUND_COLOR);

    scene.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent mouseEvent) {
            // how do I get which Circle I clicked on?
        }
    });

    stage.setTitle(TITLE);
    stage.setScene(scene);
    stage.show();


推荐答案

我只想在每个圆圈中注册一个监听器。然后你已经有了监听者注册的圈子的引用。

I would simply register a listener with each circle itself. Then you already have the reference to the circle with which the listener was registered.

这个例子推动了可用性的限制,因为它有10,000个圆圈显示全部曾经,但它演示了这种技术:

This example pushes the limit a little as to usability, because it has 10,000 circles shown all at once, but it demonstrates the technique:

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.css.PseudoClass;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class GridOfCircles extends Application {

    private static final PseudoClass SELECTED_P_C = PseudoClass.getPseudoClass("selected");

    private final int numColumns = 100 ;
    private final int numRows = 100 ;
    private final double radius = 4 ;
    private final double spacing = 2 ;

    private final ObjectProperty<Circle> selectedCircle = new SimpleObjectProperty<>(); 

    private final ObjectProperty<Point2D> selectedLocation = new SimpleObjectProperty<>();

    @Override
    public void start(Stage primaryStage) {

        selectedCircle.addListener((obs, oldSelection, newSelection) -> {
            if (oldSelection != null) {
                oldSelection.pseudoClassStateChanged(SELECTED_P_C, false);
            }
            if (newSelection != null) {
                newSelection.pseudoClassStateChanged(SELECTED_P_C, true);
            }
        });

        Pane grid = new Pane();

        for (int x = 0 ; x < numColumns; x++) {
            double gridX = x*(spacing + radius + radius) + spacing ;
            grid.getChildren().add(new Line(gridX, 0, gridX, numRows*(spacing + radius + radius)));
        }

        for (int y = 0; y < numRows ; y++) {
            double gridY = y*(spacing + radius + radius) + spacing ;
            grid.getChildren().add(new Line(0, gridY, numColumns*(spacing + radius + radius), gridY));
        }

        for (int x = 0 ; x < numColumns; x++) {
            for (int y = 0 ;y < numRows ; y++) {
                grid.getChildren().add(createCircle(x, y));
            }
        }


        Label label = new Label();
        label.textProperty().bind(Bindings.createStringBinding(() -> {
            Point2D loc = selectedLocation.get();
            if (loc == null) {
                return "" ;
            }
            return String.format("Location: [%.0f, %.0f]", loc.getX(), loc.getY());
        }, selectedLocation));

        BorderPane root = new BorderPane(new ScrollPane(grid));
        root.setTop(label);


        Scene scene = new Scene(root);
        scene.getStylesheets().add("grid.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private Circle createCircle(int x, int y) {
        Circle circle = new Circle();
        circle.getStyleClass().add("intersection");
        circle.setCenterX(x * (spacing + radius + radius) + spacing);
        circle.setCenterY(y * (spacing + radius + radius) + spacing);
        circle.setRadius(radius);

        circle.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
            selectedCircle.set(circle);
            selectedLocation.set(new Point2D(x, y));
        });

        return circle ;
    }

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

文件grid.css:

with the file grid.css:

.intersection {
    -fx-fill: blue ;
}
.intersection:selected {
    -fx-fill: gold ;
}

这篇关于Javafx点击Circle并获得它的参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆