Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javafx中从场景中移除对象_Javafx - Fatal编程技术网

在javafx中从场景中移除对象

在javafx中从场景中移除对象,javafx,Javafx,我正在挖掘文档,看看是否有删除方法,我只要在谷歌上搜索一下就可以得到这个链接 有一个简单的删除选项 例如:.getChildren().remove(对象) 这似乎对我不起作用 您提供的代码对我很好 在按住ALT键的同时单击以添加圆,并通过单击圆来删除圆 我之所以使用ALT键添加圆圈,是因为在下面的代码中,场景和圆圈都处理鼠标单击。因此,代码必须知道事件来自何处。当然,这只是一个例子 import javafx.application.Application; import javafx.ev

我正在挖掘文档,看看是否有删除方法,我只要在谷歌上搜索一下就可以得到这个链接

有一个简单的删除选项 例如:.getChildren().remove(对象)


这似乎对我不起作用

您提供的代码对我很好

在按住ALT键的同时单击以添加圆,并通过单击圆来删除圆

我之所以使用ALT键添加圆圈,是因为在下面的代码中,场景和圆圈都处理鼠标单击。因此,代码必须知道事件来自何处。当然,这只是一个例子

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class ChangeListenerSample extends Application {

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

    @Override
    public void start(final Stage primaryStage) throws Exception {
        final Group root = new Group();
        primaryStage.setResizable(false);
        final Scene scene = new Scene(root, 400,80);
        primaryStage.setScene(scene);

        scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

            @Override public void handle(final MouseEvent event)
            {
                if (!event.isAltDown())
                    return;

                final Circle circle = new Circle(event.getSceneX(), event.getSceneY(),30);
                circle.setFill(Color.YELLOW);
                root.getChildren().add(circle);

                circle.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

                    @Override public void handle(final MouseEvent arg0)
                    {
                        root.getChildren().remove(circle);
                    }
                });
            }
        });

        primaryStage.show();
    }

}
导入javafx.application.application;
导入javafx.event.EventHandler;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Circle;
导入javafx.stage.stage;
公共类ChangeListenerSample扩展应用程序{
公共静态void main(最终字符串[]args){
发射(args);
}
@凌驾
public void start(final Stage primaryStage)引发异常{
最终组根=新组();
primaryStage.SetResizeable(假);
最终场景=新场景(根,400,80);
初级阶段。场景(场景);
scene.addEventHandler(MouseEvent.MOUSE_单击,新建EventHandler()){
@重写公共无效句柄(最终MouseeEvent事件)
{
如果(!event.isAltDown())
返回;
最终循环=新循环(event.getSceneX(),event.getSceneY(),30);
圆形。设置填充(颜色。黄色);
root.getChildren().add(圆);
circle.addEventHandler(MouseEvent.MOUSE_单击,新建EventHandler()){
@重写公共无效句柄(最终MouseEvent arg0)
{
root.getChildren().remove(圆);
}
});
}
});
primaryStage.show();
}
}

您能为我们提供更多信息吗?e、 g.来源code@GGrec,代码如下: