JavaFX FlowPane 自动调整大小
JavaFX FlowPane Autosize
我遇到的问题是 FlowPane 留下了很多多余的部分 space,它可能是 Popup,尽管我认为弹出窗口的大小就是内容的大小。
为了调试,我将文本包裹在 BorderPane 中以显示文本的边界。
我关注的组件是错误弹出窗口。
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
。
来自文档:
FlowPane's prefWrapLength property establishes it's preferred width (for horizontal) or preferred height (for vertical).
解决方案
您可以使用
将 wrapLength 减小到所需的值
flowPane.setPrefWrapLength(YOUR_VALUE);
除了@ItachiUchiha 的回答之外,您还可以删除所有窗格并使用 Label
作为您的简单用例:
public static void showWarningPopup( Node owner, String message, double screenX, double screenY )
{
// create error image
ImageView image = new ImageView( "/resources/error-14.png" );
image.getStyleClass().add( "image-view" );
// create content
Label label = new Label( message, image );
label.setBackground(
new Background(
new BackgroundFill(
Color.ORANGE,
new CornerRadii( 10 ),
new Insets( -10 )
)
)
);
// create and show the popup
Popup popup = new Popup();
popup.setHideOnEscape( true );
popup.setAutoHide( true );
popup.setAutoFix( true );
popup.getContent().add( label );
popup.show( owner, screenX, screenY );
}
当然,您可以更喜欢使用 css 样式 类 而不是基于代码的样式。
我遇到的问题是 FlowPane 留下了很多多余的部分 space,它可能是 Popup,尽管我认为弹出窗口的大小就是内容的大小。
为了调试,我将文本包裹在 BorderPane 中以显示文本的边界。
我关注的组件是错误弹出窗口。
.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
。
来自文档:
FlowPane's prefWrapLength property establishes it's preferred width (for horizontal) or preferred height (for vertical).
解决方案
您可以使用
将 wrapLength 减小到所需的值flowPane.setPrefWrapLength(YOUR_VALUE);
除了@ItachiUchiha 的回答之外,您还可以删除所有窗格并使用 Label
作为您的简单用例:
public static void showWarningPopup( Node owner, String message, double screenX, double screenY )
{
// create error image
ImageView image = new ImageView( "/resources/error-14.png" );
image.getStyleClass().add( "image-view" );
// create content
Label label = new Label( message, image );
label.setBackground(
new Background(
new BackgroundFill(
Color.ORANGE,
new CornerRadii( 10 ),
new Insets( -10 )
)
)
);
// create and show the popup
Popup popup = new Popup();
popup.setHideOnEscape( true );
popup.setAutoHide( true );
popup.setAutoFix( true );
popup.getContent().add( label );
popup.show( owner, screenX, screenY );
}
当然,您可以更喜欢使用 css 样式 类 而不是基于代码的样式。