我有一个图像视图在一个边界的中心.左右两个按钮.BorderPendix在一个分割窗格中.我将ImageView的大小绑定到Borderies,如下所示:

imageView.fitHeightProperty().bind(borderPane.heightProperty());
imageView.fitWidthProperty().bind(borderPane.widthProperty());
imageView.setManaged(false);

它rezise,但图像不是在中心时,setManage是假的.如果我go 掉这些线条,图像在中间是很好的. 这里有一个mwe:

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {

            SplitPane root = new SplitPane();
            BorderPane borderPane = new BorderPane();
            ImageView imageView = new ImageView();
            VBox placeHolder = new VBox();
            
            imageView.fitHeightProperty().bind(borderPane.heightProperty());
            imageView.fitWidthProperty().bind(borderPane.widthProperty());
            imageView.setManaged(false);
            
            borderPane.setCenter(imageView);
            imageView.setImage(new Image("pathToImage"));
            root.getItems().addAll(borderPane,placeHolder);
            Scene scene = new Scene(root,700,700);          
            primaryStage.setScene(scene);
            
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

enter image description here

我正在寻找一种方法来存档两者.我试着用Hbox作为按钮和imageview的容器,但这也禁用了大小调整.

推荐答案

如果你试图创建一个"可调整大小的图像视图",那么请参阅ImageViewPane类从this answerjewelsea.

然而,您当前的代码似乎更像是试图设置一个background的图像.你有:

imageView.fitHeightProperty().bind(borderPane.heightProperty());
imageView.fitWidthProperty().bind(borderPane.widthProperty())

这将导致图像是整个BorderPane的大小,而不仅仅是它的"中心".换句话说,图像的功能就像一个背景图像.你的截图似乎同意这一点(你的应用程序中的图像看起来非常背景).如果你想要一个背景图像,那么你应该设置BorderPane中的background,而不是使用ImageView.

代码:

BackgroundImage bgImage =
    new BackgroundImage(
        new Image("..."),
        BackgroundRepeat.NO_REPEAT,
        BackgroundRepeat.NO_REPEAT,
        BackgroundPosition.CENTER,
        new BackgroundSize(1, 1, true, true, false, false));
borderPane.setBackground(new Background(bgImage));

在css中:

#theBorderPaneId {
    -fx-background-image: url(...);
    -fx-background-repeat: no-repeat;
    -fx-background-position: center;
    -fx-background-size: stretch;
}

Java相关问答推荐

Spring安全实现多个SQL表身份验证

如何将kotlin代码转换为java

为什么Java中的两个日期有差异?

Java中不同包中的类之间的配置共享

Java中是否有某种类型的池可以避免重复最近的算术运算?

我无法获取我的Java Spring应用程序的Logback跟踪日志(log)输出

有没有更快的方法在N个容器中删除重复项?

对字符串长度进行排序,但颠倒了顺序(最长字符串在前)

如何在JavaFX中制作鼠标透明stage

如何在Jooq中获取临时表列引用?

我可以在@Cacheable中使用枚举吗

如何获得凌空cookies ,并设置它在下一个请求- android

无法将GSON导入到我的JavaFX Maven项目

在Java泛型中使用通配符时,如何推断类型

如何使用WebEnvironment.RANDOM_PORT获得第二个随机端口?

无法在IntStream上应用Collectors.groupingBy

在具有Quarkus Panache的PostgreSQL中将JSON数据存储为JSONB时,会将其存储为转义字符串

如何在Spring Boot中为不同的部署环境管理多个.properties文件?

spring 更新多项管理关系

ReturnedRect在升级后反转