在创建 2d javafx 形状数组时遇到问题,因为它无法分配值
Having issues creating a 2d array of javafx shapes as it cannot have values assigned
我已经找了一段时间了,但找不到解决这个问题的办法。基本上,在 Circle[][] board = new Circle[rows][column];
行下方的代码中,当我尝试为这些圆圈分配任何值时,因为它们为空,所以我遇到了问题。我一直在尝试添加像这样的大括号 Circle[][] board = new Circle(0)[rows][column];
来解决问题,但随后它说 Array type expected;发现 javafx.scene.shape.Circle 我为此 class 添加了我的代码以及我的错误代码。如果需要任何其他代码,请告诉我,并提前感谢您的帮助。
代码:
package com.company;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class SetupBoard {
public void main(Stage stage) {
int rows = 8;
int column = 8;
Circle[][] board = new Circle[rows][column];
int rowcounter = 0;
int columncounter = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < column; j++) {
board[i][j].setRadius(0);
}
}
while (rowcounter < 3) {
if (columncounter == 9) {
columncounter = 0;
}
if (columncounter == 8) {
columncounter = 1;
}
while (columncounter < 8) {
board[rowcounter][columncounter].setRadius(20);
columncounter = columncounter + 2;
}
rowcounter++;
}
rowcounter = 5;
columncounter = 1;
while (rowcounter < 8) {
if (columncounter == 9) {
columncounter = 0;
}
if (columncounter == 8) {
columncounter = 1;
}
while (columncounter < 8) {
board[rowcounter][columncounter].setRadius(20);
columncounter = columncounter + 2;
int centrex;
int centrey;
centrex = 75 + (50 * rowcounter);
centrey = 125 + (50 * columncounter);
board[rowcounter][columncounter].setStroke(Color.BLUE);
board[rowcounter][columncounter].setFill(Color.BLUE);
board[rowcounter][columncounter].setCenterX(centrex);
board[rowcounter][columncounter].setCenterY(centrey);
}
rowcounter++;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < column; j++) {
//System.out.print(board[i][j]);
//Group root = new Group((board[i][j]));
//Scene scene = new Scene(root, 600, 600);
//stage.setScene(scene);
//stage.show();
}
System.out.println("");
}
}
}
和错误:
> Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access00(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent4(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.NullPointerException
at com.company.SetupBoard.main(SetupBoard.java:19)
at com.company.Screens.MainWindow.BtnPress_start(MainWindow.java:25)
... 58 more*
Circle[][] board = new Circle[rows][column];
这里你刚刚初始化了数组。
这意味着你告诉了 JVM "hey! Look, I need this space for my elements to be stored here!"。
JVM 然后通过给你一个 指针 来响应 space。每个矩阵位置代表空 space.
内的一个点
____________________
| null | null | null |
|______|______|______|
| null | null | null |
|______|______|______|
| null | null | null |
|______|______|______|
for (int i = 0; i < rows; i++) {
for (int j = 0; j < column; j++) {
board[i][j].setRadius(0);
}
}
此处您试图调用 setRadius
一个 null
引用,这是空 space 的默认有效值。
在这样做之前,您需要以相同的方式初始化 Circle
(s)
for (int i = 0; i < rows; i++) {
for (int j = 0; j < column; j++) {
board[i][j] = new Circle();
}
}
我已经找了一段时间了,但找不到解决这个问题的办法。基本上,在 Circle[][] board = new Circle[rows][column];
行下方的代码中,当我尝试为这些圆圈分配任何值时,因为它们为空,所以我遇到了问题。我一直在尝试添加像这样的大括号 Circle[][] board = new Circle(0)[rows][column];
来解决问题,但随后它说 Array type expected;发现 javafx.scene.shape.Circle 我为此 class 添加了我的代码以及我的错误代码。如果需要任何其他代码,请告诉我,并提前感谢您的帮助。
代码:
package com.company;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class SetupBoard {
public void main(Stage stage) {
int rows = 8;
int column = 8;
Circle[][] board = new Circle[rows][column];
int rowcounter = 0;
int columncounter = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < column; j++) {
board[i][j].setRadius(0);
}
}
while (rowcounter < 3) {
if (columncounter == 9) {
columncounter = 0;
}
if (columncounter == 8) {
columncounter = 1;
}
while (columncounter < 8) {
board[rowcounter][columncounter].setRadius(20);
columncounter = columncounter + 2;
}
rowcounter++;
}
rowcounter = 5;
columncounter = 1;
while (rowcounter < 8) {
if (columncounter == 9) {
columncounter = 0;
}
if (columncounter == 8) {
columncounter = 1;
}
while (columncounter < 8) {
board[rowcounter][columncounter].setRadius(20);
columncounter = columncounter + 2;
int centrex;
int centrey;
centrex = 75 + (50 * rowcounter);
centrey = 125 + (50 * columncounter);
board[rowcounter][columncounter].setStroke(Color.BLUE);
board[rowcounter][columncounter].setFill(Color.BLUE);
board[rowcounter][columncounter].setCenterX(centrex);
board[rowcounter][columncounter].setCenterY(centrey);
}
rowcounter++;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < column; j++) {
//System.out.print(board[i][j]);
//Group root = new Group((board[i][j]));
//Scene scene = new Scene(root, 600, 600);
//stage.setScene(scene);
//stage.show();
}
System.out.println("");
}
}
}
和错误:
> Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access00(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent4(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.NullPointerException
at com.company.SetupBoard.main(SetupBoard.java:19)
at com.company.Screens.MainWindow.BtnPress_start(MainWindow.java:25)
... 58 more*
Circle[][] board = new Circle[rows][column];
这里你刚刚初始化了数组。
这意味着你告诉了 JVM "hey! Look, I need this space for my elements to be stored here!"。
JVM 然后通过给你一个 指针 来响应 space。每个矩阵位置代表空 space.
____________________
| null | null | null |
|______|______|______|
| null | null | null |
|______|______|______|
| null | null | null |
|______|______|______|
for (int i = 0; i < rows; i++) {
for (int j = 0; j < column; j++) {
board[i][j].setRadius(0);
}
}
此处您试图调用 setRadius
一个 null
引用,这是空 space 的默认有效值。
在这样做之前,您需要以相同的方式初始化 Circle
(s)
for (int i = 0; i < rows; i++) {
for (int j = 0; j < column; j++) {
board[i][j] = new Circle();
}
}