如何要求窗格重新布局

时间:2021-12-13 07:17:02

It seems that in Javafx2 when you add items to a pane, then make one invisible it still takes up space in the layout. Does anyone know how to ask the pane to adjust it's layout after a child is made invisible?

在Javafx2中,当您向窗格添加项目时,似乎使一个不可见,它仍占用布局中的空间。有没有人知道在让孩子看不见之后如何让窗格调整它的布局?

Below is a sample program that demonstrates my issue. There are 3 buttons in a VBox. When you click on the top or middle button it become invisible, but it leaves a gap.

以下是演示我的问题的示例程序。 VBox中有3个按钮。当您单击顶部或中间按钮时,它将变为不可见,但会留下间隙。

public class VisibilityTest extends Application {

Button button1 = null;
Button button2 = null;
Button button3 = null;
VBox   box     = null;

@Override
public void start(Stage primaryStage) {
    button1 = new Button("Button 1");
    button2 = new Button("Button 2");
    button3 = new Button("Button 3");

    box = new VBox();
    box.getChildren().add(button1);
    box.getChildren().add(button2);
    box.getChildren().add(button3);
    button1.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent t) {
            button1.setVisible(false);
        }
    });
    button2.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent t) {
            button2.setVisible(false);
        }
    });
    button3.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent t) {
            button3.setVisible(false);
        }
    });
    Scene scene = new Scene(box, 300, 250);
    primaryStage.setScene(scene);
    primaryStage.show();
}
}

2 个解决方案

#1


0  

You need to remove the control from scene graph.

您需要从场景图中删除控件。

parent.getChildren().remove(theControl);

#2


1  

Call validate() of the higher level component.

调用更高级别组件的validate()。

#1


0  

You need to remove the control from scene graph.

您需要从场景图中删除控件。

parent.getChildren().remove(theControl);

#2


1  

Call validate() of the higher level component.

调用更高级别组件的validate()。