JavaFX FlowPane自动调整大小 [英] JavaFX FlowPane Autosize

查看:365
本文介绍了JavaFX FlowPane自动调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是FlowPane留下了很多多余的空间,它可能是Popup,尽管我认为popup的大小是内容的大小.

The problem I'm having is that the FlowPane is leaving a lot of excess space, it could possibly be the Popup, although I think the popup size is the content size.

为了进行调试,我将文本包装在BorderPane中以显示文本的边界.

For the sake of debugging I've wrapped the text in a BorderPane to show the bounds of the text.

我关注的组件是错误弹出窗口.

The component I'm focusing on is the error popup.

CSS

.warning-popup {
    -fx-padding: 10px;
    -fx-hgap: 10px;
    -fx-vgap: 10px;
    -fx-background-color: #704745;
    -fx-border-color: #C8C8C8;
    -fx-background-radius: 2px;
    -fx-border-radius: 2px;
}


.warning-popup .text {
    -fx-fill: #000000;
}

Java代码

public static void showWarningPopup(Node owner, String message, double screenX, double screenY) {
    // create message text
    Text text = new Text(message);
    text.getStyleClass().add("text");
    // wrap text in container
    Pane textContainer = new BorderPane(text);
    textContainer.setStyle("-fx-background-color: orange;");
    // create error image
    ImageView image = new ImageView("/resources/error-14.png");
    image.getStyleClass().add("image-view");
    // create content
    FlowPane content = new FlowPane(image, textContainer);
    content.getStyleClass().add("warning-popup");
    content.autosize();
    // create and show the popup
    Popup popup = new Popup();
    popup.setHideOnEscape(true);
    popup.setAutoHide(true);
    popup.setAutoFix(true);
    popup.getContent().add(content);
    popup.show(owner, screenX, screenY);
}

谢谢您的帮助:)

推荐答案

背景

您面临的问题是因为FlowPane的默认WrapLength设置为 400 .此属性还将FlowPane的width设置为400.

The issue you are facing is because of the default WrapLength of FlowPane, which is set at 400. This property also sets the width of the FlowPane to 400.

来自文档:

FlowPane的prefWrapLength属性确定其首选宽度(对于水平)或首选高度(对于垂直).

FlowPane's prefWrapLength property establishes it's preferred width (for horizontal) or preferred height (for vertical).

解决方案

您可以使用以下方法将wrapLength减小到所需的值

You can decrease the wrapLength to the desired value by using

flowPane.setPrefWrapLength(YOUR_VALUE);

这篇关于JavaFX FlowPane自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆