JavaFX 矩形数组不起作用
JavaFX Rectangel Array does'nt work
我处理一个问题大约 2 个小时,但我仍然无法解决它。
我的任务是编程 49 个矩形正方形。
所以我尝试使用一个数组来简化它,但它不想工作:'(
不使用数组也可以..
感谢您的帮助!
源代码:
package Game;
import javafx.scene.shape.Rectangle;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Game extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
VBox vb = new VBox();
VBox vb2 = new VBox();
HBox hb = new HBox();
Rectangle q[] = new Rectangle[4];
q[0].setFill(Color.RED);
q[1].setFill(Color.BLUE);
q[2].setFill(Color.GREEN);
q[3].setFill(Color.YELLOW);
vb.getChildren().addAll(q[0],q[1]);
vb2.getChildren().addAll(q[2],q[3]);
hb.getChildren().addAll(vb,vb2);
Scene sz1 = new Scene(hb);
primaryStage.setScene(sz1);
primaryStage.show();
}
}
my compiler :
> Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
atcom.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Launcher
Impl.java:389)
atcom.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.jav
a:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
atcom.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.ja
va:917)
atcom.sun.javafx.application.LauncherImpl.lambda$launchApplication5(Launc
herImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at Spiel.FensterSpiel.start(FensterSpiel.java:29)
atcom.sun.javafx.application.LauncherImpl.lambda$launchApplication12(Laun
cherImpl.java:863)
atcom.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl
.java:326)
atcom.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:
295)
at java.security.AccessController.doPrivileged(Native Method)
atcom.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.j
ava:294)
atcom.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.ja
va:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
atcom.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:19
1)
... 1 more
你得到一个NullPointerException
是因为你初始化了一个数组,却忘了初始化数组中的对象! q[0]
-q[3]
都是null
。您不能在 null
对象上调用 setFill()
方法。
改变
Rectangle q[] = new Rectangle[4];
q[0].setFill(Color.RED);
q[1].setFill(Color.BLUE);
q[2].setFill(Color.GREEN);
q[3].setFill(Color.YELLOW);
类似于
Rectangle q[] = new Rectangle[4];
// initialize the rectangles
q[0] = new Rectangle();
q[1] = new Rectangle();
q[2] = new Rectangle();
q[3] = new Rectangle();
q[0].setFill(Color.RED);
q[1].setFill(Color.BLUE);
q[2].setFill(Color.GREEN);
q[3].setFill(Color.YELLOW);
希望对您有所帮助。
我处理一个问题大约 2 个小时,但我仍然无法解决它。 我的任务是编程 49 个矩形正方形。 所以我尝试使用一个数组来简化它,但它不想工作:'(
不使用数组也可以..
感谢您的帮助!
源代码:
package Game;
import javafx.scene.shape.Rectangle;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Game extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
VBox vb = new VBox();
VBox vb2 = new VBox();
HBox hb = new HBox();
Rectangle q[] = new Rectangle[4];
q[0].setFill(Color.RED);
q[1].setFill(Color.BLUE);
q[2].setFill(Color.GREEN);
q[3].setFill(Color.YELLOW);
vb.getChildren().addAll(q[0],q[1]);
vb2.getChildren().addAll(q[2],q[3]);
hb.getChildren().addAll(vb,vb2);
Scene sz1 = new Scene(hb);
primaryStage.setScene(sz1);
primaryStage.show();
}
}
my compiler :
> Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
atcom.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Launcher
Impl.java:389)
atcom.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.jav
a:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
atcom.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.ja
va:917)
atcom.sun.javafx.application.LauncherImpl.lambda$launchApplication5(Launc
herImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at Spiel.FensterSpiel.start(FensterSpiel.java:29)
atcom.sun.javafx.application.LauncherImpl.lambda$launchApplication12(Laun
cherImpl.java:863)
atcom.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl
.java:326)
atcom.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:
295)
at java.security.AccessController.doPrivileged(Native Method)
atcom.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.j
ava:294)
atcom.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.ja
va:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
atcom.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:19
1)
... 1 more
你得到一个NullPointerException
是因为你初始化了一个数组,却忘了初始化数组中的对象! q[0]
-q[3]
都是null
。您不能在 null
对象上调用 setFill()
方法。
改变
Rectangle q[] = new Rectangle[4];
q[0].setFill(Color.RED);
q[1].setFill(Color.BLUE);
q[2].setFill(Color.GREEN);
q[3].setFill(Color.YELLOW);
类似于
Rectangle q[] = new Rectangle[4];
// initialize the rectangles
q[0] = new Rectangle();
q[1] = new Rectangle();
q[2] = new Rectangle();
q[3] = new Rectangle();
q[0].setFill(Color.RED);
q[1].setFill(Color.BLUE);
q[2].setFill(Color.GREEN);
q[3].setFill(Color.YELLOW);
希望对您有所帮助。