我写了一个基本控件及其皮肤。 label
显示在皮肤的 HBox
中。如果没有足够的空间,该标签应将其文本换行。
public class LabelWrap extends Application {
public static void main(String[] args) {
launch(LabelWrap.class);
}
@Override
public void start(Stage stage) throws Exception {
BasicControl basicControl = new BasicControl();
BorderPane borderPane = new BorderPane();
borderPane.setPrefWidth(150);
borderPane.setCenter(basicControl);
stage.setScene(new Scene(borderPane));
stage.centerOnScreen();
stage.show();
}
private static class BasicControl extends Control {
@Override
protected Skin<?> createDefaultSkin() {
return new BasicControlSkin(this);
}
}
private static class BasicControlSkin extends SkinBase<BasicControl> {
protected BasicControlSkin(BasicControl control) {
super(control);
VBox box = new VBox();
Label label = new Label("This text should wrap because it is too long");
label.setWrapText(true);
box.getChildren().add(label);
getChildren().add(box);
}
}
}
但是标签没有换行(显示省略号),因为我的控件的首选宽度计算不正确:
我想得到的是:
我如何配置皮肤来计算皮肤首选高度以获得所需的行为(我不想显示省略号)?
注意事项:
- 我不想在标签或其他外观组件上设置明确的最大尺寸:
label.setMaxWidth(150)
。唯一的显式宽度集应该是start 方法
中的根BorderPane
。这个宽度(150)可以是可变的,控件可以在不同的地方使用。 - 这个基本控件当然是对真实控件的简化。真实的显示几个
Label
,里面有可变文本。 - 如果我增加窗口高度直到它有足够的空间,标签就会正确换行
- 此代码在 OSX 10.10.2 上的 java 1.8.0_40-b27 上运行
最佳答案
AFAIK,要将 text
包裹在 label
中,您应该为此标签定义一个 width
,因为引用 setWrapText(Boolean)文档:
public final void setWrapText(boolean value)
Sets the value of the property wrapText.
Property description: If a run of text exceeds the width of the Labeled, then this variable indicates whether the text should wrap onto another line.
这里的语句exceeds the width of Labeled 表明你已经为你的label
定义了一个width
>,这就是为什么在没有定义 width
时不能使用它的原因。
所以你的代码应该是:
Label label = new Label("This text should wrap because it is too long");
label.setMaxWidth(150);
label.setWrapText(true);
另一种选择是使用 Text element代替 Label
并使用 setWrappingWidth()
方法,如下所示:
Text t = new Text("This text should wrap because it is too long" );
t.setWrappingWidth(150);
你会得到这样的结果:
结论:
要包装文本(在 Label
或 Text
元素中),您必须定义一个宽度,以便当我们超过时文本将返回一个新行这个宽度。
编辑:
并且为了让它更动态一点并避免为标签设置宽度,如果您正在为 borderPane
设置 PrefWidth
,您可以使用 static double WIDTH
将获取此 PrefWidth
并将其设置为标签的 MaxWidth
,示例代码如下:
public class LabelWrap extends Application {
static double WIDTH;
public static void main(String[] args) {
launch(LabelWrap.class);
}
@Override
public void start(Stage stage) throws Exception {
BasicControl basicControl = new BasicControl();
BorderPane borderPane = new BorderPane();
borderPane.setPrefWidth(150);
borderPane.setCenter(basicControl);
//get the PrefWidth value in the WIDTH attribute
WIDTH = borderPane.getPrefWidth();
stage.setScene(new Scene(borderPane));
stage.centerOnScreen();
stage.show();
}
private static class BasicControl extends Control {
@Override
protected Skin<?> createDefaultSkin() {
return new BasicControlSkin(this);
}
}
private static class BasicControlSkin extends SkinBase<BasicControl> {
protected BasicControlSkin(BasicControl control) {
super(control);
VBox box = new VBox();
Label label = new Label("This text should wrap because it is too long");
//set the WIDTH value to the label MaxWidth
label.setMaxWidth(WIDTH);
label.setWrapText(true);
box.getChildren().add(label);
this.getChildren().add(box);
}
}
}
关于java - 包含包装标签的控制皮肤的首选高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30188659/