JavaFx ImageView 不会在 fitWidthProperty 绑定到 VBox 时调整大小

use*_*374 5 javafx imageview java-8 fxml splitpane

我正在尝试重新调整图像大小,使其适合(缩放)在 a 分配的空间中SplitPane(有关解决方法的建议:该图像在运行程序时会发生变化,它应该从文本生成)。我当前使用的 ImageView 更改了SplitPane Divider. 分隔线永远不会导致ImageView缩小超过图像的大小。

两张图片显示了这种行为

ImageViewfitWidthProperty链接到 aVBox并没有给我预期的调整大小行为。增加ImageView的大小确实有效(它在没有绑定的情况下固定),但图像永远不会增长,它只是在它周围有边框。

我的 MWE:

sample.fxml

<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<SplitPane orientation="HORIZONTAL" xmlns:fx="http://javafx.com/fxml">
    <TextArea fx:id="textArea"/>
    <VBox fx:id="vBox" alignment="CENTER">
        <HBox fx:id="hBox" alignment="CENTER">
            <ImageView fx:id="imageView"/>
        </HBox>
    </VBox>
</SplitPane>
Run Code Online (Sandbox Code Playgroud)

Controller.java

import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

public class Controller {
    @FXML
    private TextArea textArea = new TextArea();
    @FXML
    private VBox vBox = new VBox();
    @FXML
    private HBox hBox = new HBox();
    @FXML
    private ImageView imageView = new ImageView();

    public Controller() {
        imageView.fitWidthProperty().bind(vBox.widthProperty());
        imageView.fitHeightProperty().bind(hBox.heightProperty());
    }

    public void start() {
        textArea.setText("text");

        Image image = new Image("https://www.google.nl/images/srpr/logo11w.png");
        imageView.setImage(image);
    }
}
Run Code Online (Sandbox Code Playgroud)

Main.java

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(final Stage primaryStage) throws Exception {
        final Controller controller = new Controller();

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sample.fxml"));
        fxmlLoader.setController(controller);

        final Parent root = fxmlLoader.load();
        primaryStage.setTitle("MWE");
        final Scene scene = new Scene(root, 640, 480);
        primaryStage.setScene(scene);
        primaryStage.show();

        controller.start();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)

Ita*_*iha 2

您可以尝试将 ImageView 绑定fitWidthProperty()到 SplitPane 的分隔符位置属性。

与此类似的绑定应该可以工作:

imageView.fitWidthProperty().bind(Bindings.subtract(1, 
               splitPane.getDividers().get(0).positionProperty())
                                      .multiply(splitPane.widthProperty());
Run Code Online (Sandbox Code Playgroud)