gridpane

niwlg2el  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(303)

我在学校有一个项目,我需要在javafx中使用控制器和场景生成器做一个15个益智游戏。我有一个问题,因为我想用一个网格窗格把我的图片放在其中,并使用索引来改变位置。这是我的密码:

@FXML
private Button ouvrirTaquin;
@FXML
private GridPane gridPane;
@FXML
private ImageView c1;
@FXML
private ImageView c2;
@FXML
private ImageView c3;
@FXML
private ImageView c4;
@FXML
private ImageView c5;
@FXML
private ImageView c6;
@FXML
private ImageView c7;
@FXML
private ImageView c8;
@FXML
private ImageView c0;

Node[][] imageViewsTab = {{c1, c2, c3}, {c4, c5, c6}, {c7, c8, c0}};
Node[] imageViews = {c1,c2,c3,c4,c5,c6,c7,c8,c0};
Taquin taquin = new Taquin(imageViewsTab);

public void ouvrirTaquin(MouseEvent mouseEvent){

    gridPane.add(imageViews[0], 0, 0); //(imageView, colonne, ligne)
    gridPane.add(c2, 1, 0);
    gridPane.add(c3, 2, 0);
    gridPane.add(c4, 0, 1);
    gridPane.add(c5, 1, 1);
    gridPane.add(c6,2 , 1);
    gridPane.add(c7, 0, 2);
    gridPane.add(c8, 1, 2);
    gridPane.add(c0, 2, 2);

    if(mouseEvent.getSource() == ouvrirTaquin){
        gridPane.toFront();
    }
}

这个代码不工作,但我不知道为什么tt,但它工作时,我改变这行 gridPane.add(imageViews[0], 0, 0); -> gridPane.add(c1, 0,0); 以下是错误:

at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1862)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1729)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3563)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3865)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1851)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2584)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
    ... 29 more
Caused by: java.lang.NullPointerException
    at javafx.graphics/javafx.scene.layout.Pane.setConstraint(Pane.java:111)
    at javafx.graphics/javafx.scene.layout.GridPane.setRowIndex(GridPane.java:299)
    at javafx.graphics/javafx.scene.layout.GridPane.setConstraints(GridPane.java:556)
    at javafx.graphics/javafx.scene.layout.GridPane.add(GridPane.java:973)
    at Stages.Controller.ouvrirTaquin(Controller.java:513)
    ... 40 more

谢谢你的帮助^^(对不起我的英语呵呵)

rsaldnfx

rsaldnfx1#

当这行代码被执行时。。。

Node[] imageViews = {c1,c2,c3,c4,c5,c6,c7,c8,c0};

所有的 ImageView 变量仍然为空。
你应该初始化 imageViewsinitialize() 方法。

public void initialize() {
    imageViews = {c1,c2,c3,c4,c5,c6,c7,c8,c0};
}

参考这个问题和答案。

相关问题