创建对象并将它们添加到组

creating objects and adding them to group

简介

我想创建一些矩形,但我总是看到 nullPointerExeption,或者我无法提供尽可能多的参数。 我无法制作 Rectangle (width,height,x,y,paint,paint(stroke)) 所以我决定使用 RectangleBuilder 对象。

这是我的做法。

    @FXML
public void group() {
   // RB.applyTo(r);
    for (double i = 0; i < 5; i++) {

        pane.getChildren().add(new Group(g));
        r.setStroke(Color.BLACK);
        r.setY(i * 25 + 80);
        r.setX(100.0);

        r.setStroke(Color.BLACK);
        r.getParent();
        RB.y(r.getY());
        RB.x(r.getX());
        RB.height(r.getHeight());
        RB.width(r.getWidth());
        RB.stroke(r.getStroke());
        RB.fill(r.getFill());
        g.getChildren().add(RB.build());
    }

}

这里是对象:

@FXML Pane pane = new Pane(); @FXML Rectangle r = new Rectangle(); Group g = new Group(); protected RectangleBuilder RB = null;

我真的不知道我应该使用 RB.getX 还是 RB.applyto(r)。

问题

位于:RB.x(r.getX()):等等

或 RB.applyto(r);当我删除这一行的评论标记时。

当我尝试获取有关 r 矩形的信息时,问题出在 RB 中。 我可以很容易地打开程序,但是当我点击带有操作 "group" 的按钮时,它给了我错误:

错误数据

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1456)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
    at javafx.event.Event.fireEvent(Event.java:171)
    at javafx.scene.Node.fireEvent(Node.java:6867)
    at javafx.scene.control.Button.fire(Button.java:179)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
    at com.sun.javafx.scene.control.skin.SkinBase.handle(SkinBase.java:336)
    at com.sun.javafx.scene.control.skin.SkinBase.handle(SkinBase.java:329)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
    at javafx.event.Event.fireEvent(Event.java:171)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3369)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3209)
    at javafx.scene.Scene$MouseHandler.access00(Scene.java:3164)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1582)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2267)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:530)
    at com.sun.glass.ui.View.notifyMouse(View.java:924)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access0(WinApplication.java:17)
    at com.sun.glass.ui.win.WinApplication.run(WinApplication.java:67)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:75)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:279)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1453)
    ... 44 more
Caused by: java.lang.NullPointerException
    at group.FXMLDocumentController.group(FXMLDocumentController.java:45)
    ... 54 more

不知道有没有那么重要

顺便说一句,第 45 行是:

 RB.y(r.getY());

不要使用 RectangleBuilder,它已被弃用并将被删除。

你真正的问题是什么?我的意思是,我看到您的代码中有很多错误。这是一个关于如何在 JavaFX 中创建矩形并将它们添加到组中的示例:

public class RectangleDemo extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        Pane pane = new Pane();

        Group group = new Group();

        for( int i = 0; i < 5; i++) {

            Rectangle rectangle = new Rectangle( 100.0, i*25.0 + 80.0, 20.0, 20.0);
            rectangle.setStroke(Color.BLACK);
            rectangle.setFill(Color.LIGHTGRAY);

            group.getChildren().add( rectangle);

        }

        pane.getChildren().add(group);

        primaryStage.setScene(new Scene( pane, 640, 480));
        primaryStage.show();
    }

}