gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:21:56 26 4
gpt4 key购买 nike

我在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com