假设我有这样的代码:我在其上创建Pane
,但createCell
和deleteCells
按钮。创建另一个窗格,单击createCell
按钮时,我会在其子项列表中添加Rectangle
。单击deleteCells
按钮后,我会从Rectangle
的孩子中删除Pane
。
public class Main extends Application implements EventHandler<ActionEvent> {
//Main.java:17)
private static int cellId=-1;
private static Cell[] cells = new Cell[100];
private BorderPane borderPane;
private Pane cellsPane;
private Pane buttonsPane;
private Pane statsPane;
Button createCellButton;
Button deleteCellsButton;
static int width= 600;
static int height=400;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Hello World");
borderPane = new BorderPane();
cellsPane = new Pane();
buttonsPane = new Pane();
statsPane = new Pane();
cellsPane.setStyle("-fx-background-color: #ffffff; -fx-border-color: black");
buttonsPane.setStyle("-fx-background-color: #f2f2f2; -fx-border-color: black");
statsPane.setStyle("-fx-background-color: #f2f2f2; -fx-border-color: black");
createCellButton = new Button();
deleteCellsButton = new Button();
createCellButton.setText("Create a cell");
deleteCellsButton.setText("Delete cells");
createCellButton.setOnAction(this);
deleteCellsButton.setOnAction(this);
buttonsPane.getChildren().add(createCellButton);
buttonsPane.getChildren().add(deleteCellsButton);
borderPane.setTop(buttonsPane);
Rectangle r = new Rectangle(10, 10, 50, 25);
r.setFill(Color.YELLOW);
cellsPane.getChildren().add(r);
borderPane.setCenter(cellsPane);
statsPane.getChildren().add(new Label("Cells Count: " + cellId +1));
borderPane.setLeft(statsPane);
Scene scene = new Scene(borderPane, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void handle(ActionEvent event) {
if (event.getSource()==createCellButton) {
cells[++cellId] = createCell();
cellsPane.getChildren().add(cells[cellId].getModel());
System.out.println("Cell created: " + cells[cellId].getAge() + ", " + cells[cellId].getColor());
}
else if (event.getSource() == deleteCellsButton) {
deleteCells(); //Main.java:88
System.out.println("Cells deleted");
}
else {
System.out.println("Unknown button");
}
}
private Cell createCell() {
return new Cell(cellId, width, height);
}
private void deleteCells() {
for (Cell cell: cells) {
System.out.println("removing " + cell.getAge());
//Main.java:102
cellsPane.getChildren().remove(cell.getModel());
System.out.println("cell " + cell.getAge() + " removed");
}
}
Cell
课程:
class Cell {
private int age;
private Color color;
private Rectangle model;
public Cell(int id, int width, int height) {
age = id;
Random rand = new Random();
int x = rand.nextInt(width+1);
int y = rand.nextInt(height+1);
color = Color.YELLOW;
model = new Rectangle(x, y, 50, 25);
model.setFill(color);
}
public Rectangle getModel() {
return model;
}
public int getAge() {
return age;
}
public Color getColor() {
return color;
}
}
但是,点击deleteCells
后,窗口中仍然可以看到之前的矩形。我如何解决它?如何在单击按钮后刷新Pane
?
修改
java.lang.NullPointerException
at sample.Main.deleteCells(Main.java:102)
at sample.Main.handle(Main.java:88)
at sample.Main.handle(Main.java:17)
System.out.println
输出:
Cell created: 0, 0xffff00ff
Cell created: 1, 0xffff00ff
Cell created: 2, 0xffff00ff
Cell created: 3, 0xffff00ff
Cell created: 4, 0xffff00ff
Cell created: 5, 0xffff00ff
Cell created: 6, 0xffff00ff
Cell created: 7, 0xffff00ff
Cell created: 8, 0xffff00ff
Cell created: 9, 0xffff00ff
Cell created: 10, 0xffff00ff
removing 0
cell 0 removed
removing 1
cell 1 removed
removing 2
cell 2 removed
removing 3
cell 3 removed
removing 4
cell 4 removed
removing 5
cell 5 removed
removing 6
cell 6 removed
removing 7
cell 7 removed
removing 8
cell 8 removed
removing 9
cell 9 removed
removing 10
cell 10 removed