java 如何调整标签的宽度以适应其内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30439848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to adjust Label's width to fit its content?
提问by bluevoxel
How one can adjust minimum width of the JavaFX 8 (8_45) Label control item according to its content? To this very moment I had to adjust size of my GUI components manually, in order to be sure, that their content will be visible no matter what will happen with the size of its parent (eg. HBox, Scene, Stage or whatever), ie.:
如何根据内容调整 JavaFX 8 (8_45) Label 控件项的最小宽度?此刻,我不得不手动调整 GUI 组件的大小,以确保无论父级(例如 HBox、Scene、Stage 或其他)的大小发生什么变化,它们的内容都是可见的, IE。:
Label label = new Label("Foo foo foo");
label.setMinWidth(someMinValue);
Is there a way to make Label or any other JavaFX control item to "listen" its content and adjust its width to it automatically? Thank you in advance.
有没有办法让 Label 或任何其他 JavaFX 控件项“监听”其内容并自动调整其宽度?先感谢您。
回答by Steven Van Impe
If you want to make sure your label stays big enough for the text it holds, you probably want to set its minimum size to track its preferred size:
如果你想确保你的标签对于它所包含的文本保持足够大,你可能想要设置它的最小尺寸来跟踪它的首选尺寸:
label.setMinWidth(Region.USE_PREF_SIZE);