栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Javafx单击一个圆并获取其参考

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Javafx单击一个圆并获取其参考

我只需要向每个圈子本身注册一个侦听器。然后,您已经具有注册侦听器的圈子的参考。

该示例在可用性方面有所提高,因为它一次显示了10,000个圆圈,但它演示了该技术:

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:

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/440843.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号