JavaFX – BorderPane / StackPane在子项更改后未调整大小

JavaFX BorderPane中添加和删除内容时,我遇到了调整大小问题.在手动调整窗口大小之前,不会调整BorderPane内容的大小.我写了一个小测试应用程序来模拟这种行为.该应用程序构建一个BorderPane,其中包含嵌入在BorderPane中心的StackPane中的矩形.在BorderPane的底部有一个包含文本和分隔符的VBox和HBox.有一个菜单项可以从BorderPane的底部删除内容(MoveText – > Right),并将类似的内容添加到BorderPane的正确位置.

当文本添加到BorderPane的右侧位置时,矩形与文本重叠.换句话说,BorderPane中心的内容与BorderPane权限中的内容重叠.

我看到以下链接 –
https://stackoverflow.com/questions/5302197/javafx-bug-is-there-a-way-to-force-repaint-this-is-not-a-single-threading-pr
调用requestLayout似乎没有帮助.我也尝试在图中的各个节点上调用impl_updatePG和impl_transformsChanged.我从这个主题得到了这个想法 – https://forums.oracle.com/forums/thread.jspa?threadID=2242083

public class BorderPaneExample extends Application
{
   private BorderPane root;
   private StackPane centerPane;

   @Override
   public void start(Stage primaryStage) throws Exception
   {
      root = new BorderPane();
      root.setTop(getMenu());
      root.setBottom(getBottomVBox());
      centerPane = getCenterPane();
      root.setCenter(centerPane);

      Scene scene = new Scene(root, 900, 500);
      primaryStage.setTitle("BorderPane Example");
      primaryStage.setScene(scene);
      primaryStage.show();
   }

   private MenuBar getMenu()
   {
      MenuBar menuBar = new MenuBar();

      MenuItem rightMenuItem = new MenuItem("Right");
      rightMenuItem.setOnAction(new EventHandler<ActionEvent>() {
         @Override
         public void handle(ActionEvent actionEvent) {
            root.setRight(getRightHBox());
            root.setBottom(null);
            root.requestLayout();
         }
      });
      MenuItem bottomMenuItem = new MenuItem("Bottom");
      bottomMenuItem.setOnAction(new EventHandler<ActionEvent>() {
         @Override
         public void handle(ActionEvent actionEvent) {
            root.setRight(null);
            root.setBottom(getBottomVBox());
         }
      });

      Menu menu = new Menu("Move text");
      menu.getItems().add(rightMenuItem);
      menu.getItems().add(bottomMenuItem);
      menuBar.getMenus().addAll(menu);

      return menuBar;
   }

   private HBox getRightHBox()
   {
      HBox hbox = new HBox();

      VBox vbox = new VBox(50);
      vbox.setPadding(new Insets(0, 20, 0, 20));
      vbox.setAlignment(Pos.CENTER);

      vbox.getChildren().addAll(new Text("Additional Info 1"),
        new Text("Additional Info 2"), new Text("Additional Info 3"));
      hbox.getChildren().addAll(new Separator(Orientation.VERTICAL), vbox); 

      return hbox;
   }

   private VBox getBottomVBox()
   {
      VBox vbox = new VBox();

      HBox hbox = new HBox(20);
      hbox.setPadding(new Insets(5));
      hbox.setAlignment(Pos.CENTER);

      hbox.getChildren().addAll(new Text("Footer Item 1")
            , new Text("Footer Item 2"), new Text("Footer Item 3"));
      vbox.getChildren().addAll(new Separator(), hbox);

      return vbox;
   }

   private StackPane getCenterPane()
   {
      StackPane stackPane = new StackPane();
      stackPane.setAlignment(Pos.CENTER);

      final Rectangle rec = new Rectangle(200, 200);
      rec.setFill(Color.DODGERBLUE);
      rec.widthProperty().bind(stackPane.widthProperty().subtract(50));
      rec.heightProperty().bind(stackPane.heightProperty().subtract(50));

      stackPane.getChildren().addAll(rec);

      return stackPane;
   }

   public static void main(String[] args)
   {
      Application.launch(args);
   }
}

任何建议,将不胜感激.
谢谢

最佳答案
为了得到答案:

StackPane需要设置其最小大小才能进行裁剪.所以如果你明确地添加了stackPane.setMinSize(0,0);到getCenterPane()方法,它应该解决你的问题.

所以你的getCenterPane()方法现在看起来像这样:

private StackPane getCenterPane()
{
  StackPane stackPane = new StackPane();
  stackPane.setMinSize(0, 0);
  stackPane.setAlignment(Pos.CENTER);

  final Rectangle rec = new Rectangle(200, 200);
  rec.setFill(Color.DODGERBLUE);
  rec.widthProperty().bind(stackPane.widthProperty().subtract(50));
  rec.heightProperty().bind(stackPane.heightProperty().subtract(50));

  stackPane.getChildren().addAll(rec);

  return stackPane;
}

转载注明原文:JavaFX – BorderPane / StackPane在子项更改后未调整大小 - 代码日志