java - Pane -> Hbox -> ImageView 适合高度

标签 java user-interface javafx javafx-8 javafx-2

我在 Pane 内的 HBox 中有 ImageView,并且希望在调整舞台大小时 ImageView 高度适合 HBox 高度。尝试以下代码

package sample;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        Pane pane = new Pane();
        pane.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
        HBox hBox = new HBox();
        hBox.setBackground(new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, Insets.EMPTY)));
        hBox.setPrefHeight(100);
        hBox.setPrefWidth(100);
        hBox.prefHeightProperty().bind(pane.heightProperty());
        ImageView imageView = new ImageView("http://www.calgary.ca/CA/city-manager/scripts/about-us/webparts/images/ourHistory_retina.jpg");
        imageView.fitHeightProperty().bind(hBox.heightProperty());
        imageView.setPreserveRatio(true);
        hBox.getChildren().add(imageView);
        pane.getChildren().add(hBox);
        primaryStage.setScene(new Scene(pane, 300, 275));
        primaryStage.show();
    }


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

启动时,ImageView 不适合窗口高度,它以原始大小显示。只有当我调整窗口大小以使其更大时,它才会放大,然后是原始图像大小。

我还发现,hBox.prefHeightProperty().bind(pane.heightProperty()) 工作得很好(图像后面的红色 HBox 背景的高度是相应的窗口高度)。

看来 imageView.fitHeightProperty().bind(hBox.heightProperty()) 的行为并不符合我的预期。

如何使 ImageView 适合嵌套在 Pane 中的 HBox 的高度

最佳答案

在您发布的代码中,HBox实际上变得比根高 Pane包含它(尽管它被剪切,所以你只能看到根部一侧的部分)。所以绑定(bind)在fitHeight上行为如您所愿,但布局相对于 HBox偏好的高度并没有达到你的预期。所以你需要更好地控制 HBox 的布局.

允许您最大程度控制的布局 Pane 是 GridPane 。因此,虽然可能有更简单的方法可以做到这一点,但使用 GridPane作为根并放置 HBox在 0,0 处的唯一单元格中,您可以控制 HBox 的方式已调整大小。您在这里唯一需要的额外事情是允许 HBox无限期缩小 setMinHeight(0) .

我认为以下内容提供了您想要的行为:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        GridPane root = new GridPane();

        RowConstraints rc = new RowConstraints();
        rc.setVgrow(Priority.ALWAYS);

        root.getRowConstraints().add(rc);

        root.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
        HBox hBox = new HBox();
        hBox.setBackground(new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, Insets.EMPTY)));
        hBox.setMinHeight(0);
        ImageView imageView = new ImageView("http://www.calgary.ca/CA/city-manager/scripts/about-us/webparts/images/ourHistory_retina.jpg");
        imageView.fitHeightProperty().bind(hBox.heightProperty());
        imageView.setPreserveRatio(true);
        hBox.getChildren().add(imageView);
        root.add(hBox, 0, 0);         

        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

关于java - Pane -> Hbox -> ImageView 适合高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48385754/

相关文章:

java - 服务器安全扩展功能的最佳实践,具体取决于移动应用程序的版本

java - 如何在使用 Apache POI 和 java 创建的 .docx 上添加页码

xcode - 在 cocoa 中保存/打开 'slide out menus/views' 的名称

java - 运行 Javafx 应用程序时出错

javafx - 集成 JavaFx & LWJGL 项目

java - 如何防止从 IntelliJ Jetbrains 编辑器中的声明中删除前导空格?

java - Android "values-large-land"资源未加载

java - 使用 Eclipse WindowBuilder 创建 GUI

Python tkinter 列表框粗体

javafx 向按钮添加附加操作