【发布时间】:2020-07-11 11:54:40
【问题描述】:
在此工作了很长时间,一直在尝试所有类型的替代方案,所以我来这里寻求帮助。我需要 16 个正方形来填充 800x800 像素的区域。我已经完成了那个任务。但是,既然我希望每个正方形的中心都有一个圆,我想制作一个 StackPanes 数组。数组中的每个 stackPane 将容纳正方形,然后是圆形(因此圆形位于该正方形的顶部)。这就是我所拥有的。
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Circle;
/**
* @author Dominic Barbuto
* @date 03/28/2020
*
*/
public class DAB_ShapeGrid extends Application
{
//Scene dimensions
final int SCENE_WIDTH = 1000, SCENE_HEIGHT = 800;
//Square Dimensions
double width = 200, height = 200;
public static void main(String[] args)
{
launch(args);
}
public void start(Stage primaryStage)
{
StackPane[] stackPane = new StackPane[16];
DAB_Square[] squareArray= new DAB_Square[16];
int index = 0;
for(int posX = 0; posX < SCENE_WIDTH-200; posX += 200)
{
for(int posY = 0; posY < SCENE_HEIGHT; posY += 200)
{
squareArray[index] = new DAB_Square(posX, posY, width, height);
stackPane[index].getChildren().addAll(squareArray[index], new DAB_Circle(posX+100, posY +100, 100.0));
index++;
}
}
Pane pane = new Pane(stackPane[0], stackPane[1], stackPane[2], stackPane[3],
stackPane[4], stackPane[5], stackPane[6], stackPane[7],
stackPane[8], stackPane[9], stackPane[10], stackPane[11],
stackPane[12], stackPane[13], stackPane[14], stackPane[15]);
RadioButton radioButton = new RadioButton("Test");
VBox rightVBox = new VBox(radioButton);
BorderPane borderPane = new BorderPane();
borderPane.setLeft(pane);
borderPane.setRight(rightVBox);
Scene scene = new Scene(borderPane, SCENE_WIDTH, SCENE_HEIGHT);
primaryStage.setScene(scene);
primaryStage.setTitle("Filling the Grid With Shapes Program");
primaryStage.show();
}
}
我得到一个指向这一行的 NullPointerException:stackPane[index].getChildren().addAll(squareArray[index], new DAB_Circle(posX+100, posY +100, 100.0)); 而且我这辈子都找不到原因。
【问题讨论】:
-
您在代码中的什么时候将任何
StackPanes添加到stackPane []? -
请使用java命名约定
标签: java javafx nullpointerexception