JavaFX接受特定对象类型的组

编程入门 行业动态 更新时间:2024-10-28 08:16:22
本文介绍了JavaFX接受特定对象类型的组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

JavaFX中是否有一种方法可以创建一个扩展 Group 的类,并将其限制为仅接受 Shape 对象作为孩子?

Is there a way in JavaFX to make a class that extends Group and limit it to accepting only Shape objects as children?

推荐答案

考虑创建一个包装类而不是子类。

Consider creating a wrapper class instead of a subclass. Something along the lines of

public class ShapeGroup { private final Group group = new Group() ; public void addShape(Shape s) { group.getChildren().add(s); } public void removeShape(Shape s) { group.getChildren().remove(s); } // other methods you want to expose, implemented similarly... public Parent asParent() { return group ; } }

现在你可以按如下方式使用:

And now you can use this as follows:

ShapeGroup shapeGroup = new ShapeGroup(); shapeGroup.addShape(new Circle(50, 50, 20)); shapeGroup.addShape(new Rectangle(20, 20, 30, 30)); // ... Scene scene = new Scene(shapeGroup.asParent()); // etc..

更多推荐

JavaFX接受特定对象类型的组

本文发布于:2023-11-08 18:47:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570136.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   类型   JavaFX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!