在 javafx 中更新动态网格窗格。绘制后,它不会使用更新的参数(行,列)再次更新

Updating dynamic gridpane in javafx. Once it's drawn, it doesn't update again with the updated parameters (row,column)

我正在尝试使用 sceneBuilder 在 javafx 应用程序中设计公交车座位结构。其中 drawBusGridPane,它位于 AnchorPane(总线结构锚窗格)之下。这个锚窗格是使用 scenebuilder 绘制的。 DrawBus GridPane 使用代码在控制器 class 中绘制。当按下 Loadinfo 按钮时绘制公交车座椅结构。

@FXML
private void Loadinfo(ActionEvent event) throws IOException, ParseException {

   String co=(String) coachBox.getSelectionModel().getSelectedItem();
   
   getbusinfo(getBusID(co));
   
   coachLbl.setText(co);
   SubRouteLbl.setText(source+"-"+destination);
   BusNameLbl.setText(busname);
   
   
  
  
  
  //Adding Gridpane to the anchorpane of Bus Seat structure
  int row=Integer.parseInt(totalrows);
  int col=Integer.parseInt(totalcolumns);
  System.out.println("row: "+row);
  System.out.println("col: "+col);
  
  
  
  BusStructure.getChildren().add(drawBus(row,col)); 
  AnchorPane.setRightAnchor(drawBus(row,col),0.0);
  AnchorPane.setTopAnchor(drawBus(row,col),0.0);
  AnchorPane.setBottomAnchor(drawBus(row,col),0.0);
  AnchorPane.setLeftAnchor(drawBus(row,col),0.0);
  
      

}

这是我调用 drawBus 的代码。 totalrows & totalcolumns 是从 json 文件中获取的数据。

public static String totalcolumns;  
public static String totalrows;

public static GridPane drawBus(int rows, int col){
    GridPane table = new GridPane();

    for(int i=0; i<rows; i++){
        
        for(int j=0;j<col; j++)
        {
        Button seat=new Button();
        seat.setAlignment(Pos.CENTER);
        seat.setPrefSize(80, 31);
        seat.setText(i+","+j);
        seat.setStyle("-fx-background-color: MediumSeaGreen");

        //add them to the GridPane
        table.add(seat, j, i); //  (child, columnIndex, rowIndex)


        // margins 
        GridPane.setMargin(seat, new Insets(2));
        GridPane.setMargin(seat, new Insets(2));
        GridPane.setMargin(seat, new Insets(2));
     }
    table.setAlignment(Pos.CENTER);

    
    }
    return table;
}

每次 coachNumber 从选择框更改 BusInfo 更新并且 totalrowstotalcolumns 更改。为了检查更改,我在 Loadinfo 函数中打印了值。行和列正确更改。但是 BusStructure(Gridpane) 仍然保留,因为它在第一次单击“加载信息”按钮时会更新。

有关详细信息,我附上了我的 UI 图片。

第一次绘制公交座椅结构时的第一张图

第二张图片,其中车厢号从选择框更改但公交车结构保持不变

您永远不会从 AnchorPane 中删除旧的 GridPane。这意味着您覆盖了 drawBus 方法的所有结果。可能您加载的第一辆巴士是最大的,在上面添加较小的巴士后场景看起来一样。

要解决此问题,请替换 GridPane

此外,您在设置锚点时会创建新的 GridPane。这对添加到场景中的 GridPane 没有影响。同样为同一个座位设置 3 倍的保证金并不能提供好处。要为不同的边分配不同的值,请使用带有 4 个参数的 Insets 构造函数。您还可以使用 hgapvgap(如果需要,向 GridPane 添加填充)为 GridPane 设置子级的 horizontal/vertical 距离:

public static GridPane drawBus(int rows, int col){
    GridPane table = new GridPane();
    table.setHgap(4);
    table.setVgap(4);
    table.setAlignment(Pos.CENTER);

    for(int i=0; i<rows; i++){

        for(int j=0;j<col; j++) {
            Button seat=new Button();
            seat.setAlignment(Pos.CENTER);
            seat.setPrefSize(80, 31);
            seat.setText(i+","+j);
            seat.setStyle("-fx-background-color: MediumSeaGreen");

            //add them to the GridPane
            table.add(seat, j, i); //  (child, columnIndex, rowIndex)
        }
    }
    return table;
}
private GridPane currentTable;

@FXML
private void Loadinfo(ActionEvent event) throws IOException, ParseException {

    String co=(String) coachBox.getSelectionModel().getSelectedItem();

    getbusinfo(getBusID(co));

    coachLbl.setText(co);
    SubRouteLbl.setText(source+"-"+destination);
    BusNameLbl.setText(busname);

    //Adding Gridpane to the anchorpane of Bus Seat structure
    int row=Integer.parseInt(totalrows);
    int col=Integer.parseInt(totalcolumns);
    System.out.println("row: "+row);
    System.out.println("col: "+col);

    GridPane newTable = drawBus(row,col);

    if (currentTable == null) {
        BusStructure.getChildren().add(newTable);
    } else {
        // replace table
        BusStructure.getChildren().set(BusStructure.getChildren().indexOf(currentTable), newTable);
    }

    AnchorPane.setRightAnchor(newTable, 0.0);
    AnchorPane.setTopAnchor(newTable, 0.0);
    AnchorPane.setBottomAnchor(newTable, 0.0);
    AnchorPane.setLeftAnchor(newTable, 0.0);

    // save reference to table to determine which node to replace
    currentTable = newTable;
}
if(!BusStructure.getChildren().isEmpty())
  {
      BusStructure.getChildren().remove(0);
  }
  BusStructure.getChildren().add(drawBus(row,col)); 
  AnchorPane.setRightAnchor(drawBus(row,col),0.0);
  AnchorPane.setTopAnchor(drawBus(row,col),0.0);
  AnchorPane.setBottomAnchor(drawBus(row,col),0.0);
  AnchorPane.setLeftAnchor(drawBus(row,col),0.0);

添加这个 if 条件解决了我的问题。