控件的首选大小是多少?

What is the preferred size of a control?

我第一次尝试使用 JavaFX,我想了解一点布局管理。我们如何访问控件的首选大小?

在下面的示例中,我尝试将最大宽度设置为比首选宽度大 200 像素。也就是说,我想让按钮随着框架宽度的增加而增长(达到最大值)。

然而,当我 运行 代码时,首选宽度为 -1,因此将 200 添加到首选宽度得到最大宽度 199。

import javafx.application.Application;
import javafx.event.*;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.geometry.Insets;

public class BorderPaneSSCCE extends Application
{
    @Override
    public void start(Stage primaryStage)
    {
        Button button = new Button( "Button at PreferredSize" );
        button.setMaxWidth( button.getPrefWidth() + 200 );
        System.out.println(button.prefWidth(-1) + " : " + button.getPrefWidth());

        button.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event)
            {
                System.out.println("Width: " + button.getWidth());
            }
        });

        HBox root = new HBox();
        HBox.setHgrow(button, Priority.ALWAYS);
        root.getChildren().add(button);

        Scene scene = new Scene(root);
        primaryStage.setTitle("Java FX");
        primaryStage.setScene(scene);
        primaryStage.show();
        System.out.println(button.prefWidth(-1) + " : " + button.getPrefWidth());
    }

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

当我 运行 代码并单击按钮时,我看到:Width: 139.0

调整框架的宽度后,按钮尽可能大,然后单击按钮,我看到:Width: 199.0

我希望看到 Width: 339.0(即 139 + 200)

那么,我们如何访问控件的首选size/width?

getPrefWidth() 仅在 显示/呈现 Node 后有效。

您可以先显示 Button 或尝试使用 Node.prefWidth(-1)

-1Region.USE_COMPUTED_SIZE 的值,它告诉组件计算出它自己的最佳大小。这是可行的,因为每个 Node 都有一个名为 prefWidth(double height) 的方法,它是布局管理器用于计算其子级布局的双 prefHeight(double width)(min/max 大小相同)。要获得 pref 大小,您需要使用

button.prefWidth([-1 or Region.USE_COMPUTED_SIZE])

(docs)

getPrefWidth() returns USE_COMPUTED_SIZE 标志(默认为 -1)。

您可以使用 prefWidth(-1) 获取内部计算的首选宽度。但是,在布局窗格(示例中的 HBox)布置节点之前,不会计算首选宽度。这是第一次显示舞台时发生的情况。

如果您希望最大宽度取决于首选宽度,您有多种选择。一种是在设置最大宽度之前使用 setPrefWidth() 将首选宽度设置为固定值。

另一个是实现自定义布局算法 - 在节点或布局窗格上。这是使用自定义按钮的示例。

// button whose maxWidth is always prefWidth + 200
Button button = new Button() {
    @Override
    protected double computeMaxWidth(double height)
    {
        return this.prefWidth(height) + 200.0;
    }
};