有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java窗格>Hbox>图像视图配合高度

我在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不适合windows的高度,它以其原始大小显示。只有当我调整窗口大小使其比原始图像大时,它才会放大

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

所以看来imageView.fitHeightProperty().bind(hBox.heightProperty())的行为不像我预期的那样

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


共 (0) 个答案