简体   繁体   English

如何根据 JavaFX 中的文本调整按钮大小?

[英]How to resize a button depending on the text in JavaFX?

This might be a trivial question, but I wasn't able to find a solution so far.这可能是一个微不足道的问题,但到目前为止我无法找到解决方案。
If you create a button with a longer word as text, lets say 'Überspringen'(the german word for Skip), JavaFx will automatically truncate the Text to 'Übersprin'+EllipsisString.如果您创建一个带有较长单词作为文本的按钮,例如“Überspringen”(跳过的德语单词),JavaFx 将自动将文本截断为“Übersprin”+EllipsisString。
Button skipButton = new Button("\Überspringen");
在此处输入图片说明

Is there an easy solution to avoid the truncation?是否有避免截断的简单解决方案? I just could hardcode a new size, but in Swing the button-size was adjusted automatically.我只能硬编码一个新的大小,但在 Swing 中,按钮大小是自动调整的。

I already tried setWrapText(true) but that didn't work, I think because there is no whitespace.我已经尝试过setWrapText(true)但这没有用,我想是因为没有空格。

Edit编辑

This Minimal, Reproducible Example shows my issue: 这个最小的、可重现的示例显示了我的问题:
 public class ButtonTest extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) throws Exception { Scene scene = new Scene(getBottomPanel(),600,50); primaryStage.setScene(scene); primaryStage.show(); } private AnchorPane getBottomPanel() { HBox infraBox = new HBox(5); infraBox.setAlignment(Pos.CENTER); infraBox.getChildren().add(new Label("Input (manuell):")); infraBox.getChildren().add(new TextField()); Button shortButton = new Button("OK"); Button longButton = new Button("\Überspringen"); ButtonBar buttonBar = new ButtonBar(); buttonBar.getButtons().addAll(shortButton, longButton); AnchorPane bottomPanel = new AnchorPane(infraBox, buttonBar); bottomPanel.setPadding(new Insets(5)); AnchorPane.setLeftAnchor(infraBox, 0.0); AnchorPane.setRightAnchor(buttonBar, 5.0); return bottomPanel; } }

Normally buttons get resized so the label of the button is fully visible:通常按钮会调整大小,以便按钮的标签完全可见:

"For example, the computed size of a Button object is determined by the length of the text and the size of the font used for the label, plus the size of any image. Typically, the computed size is just big enough for the control and the label to be fully visible." “例如,Button 对象的计算大小取决于文本的长度和用于标签的字体的大小,以及任何图像的大小。通常,计算的大小刚好足够控件和标签完全可见。” ( https://docs.oracle.com/javase/8/javafx/layout-tutorial/size_align.htm ). https://docs.oracle.com/javase/8/javafx/layout-tutorial/size_align.htm )。

However when you store your button in a container (eg VBox or HBox ) that has a maximum width and height, for example, the button will be squeezed into that container, ie the label gets truncated:但是,当您将按钮存储在具有最大宽度和高度的容器(例如VBoxHBox )中时,例如,按钮将被挤压到该容器中,即标签被截断:

"By default, buttons grow only to their preferred size. However, buttons shrink to where the label is shown as three dots (...) if the minimum width is not overridden.". “默认情况下,按钮只会增长到他们喜欢的大小。但是,如果最小宽度没有被覆盖,按钮会缩小到标签显示为三个点 (...) 的位置。”。

The article I linked describes your problem:我链接的文章描述了您的问题:

"To prevent a button from becoming smaller than its preferred width, set its minimum width to its preferred width" in the section "Keeping Nodes at Their Preferred Size". “为了防止按钮变得小于其首选宽度,请将其最小宽度设置为其首选宽度”中的“将节点保持在其首选大小”部分。

If you did not set the preferred size of your button manually the computed size is used for its preferred size automatically:如果您没有手动设置按钮的首选大小,则计算出的大小将自动用于其首选大小:

"By default, UI controls compute default values for their preferred size that is based on the content of the control" “默认情况下,UI 控件根据控件的内容为其首选大小计算默认值”

Your code sample does not produce the described problem (for me):您的代码示例不会产生所描述的问题(对我而言):

示例应用程序的屏幕截图

(Sry I would like to comment on your post, but my reputation is too low) (Sry 我想评论你的帖子,但我的声誉太低了)

I don't know why, so I hope somebody of you does....我不知道为什么,所以我希望你们中有人......

Solution:解决方案:

Just replace the AnchorPane with a BorderPane like this 只需像这样用 BorderPane 替换 AnchorPane
 public class ButtonTest extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) throws Exception { Scene scene = new Scene(getBottomPanel(), 600, 50); primaryStage.setScene(scene); primaryStage.show(); } private BorderPane getBottomPanel() { HBox infraBox = new HBox(5); infraBox.setAlignment(Pos.CENTER); infraBox.getChildren().add(new Label("Input (manuell):")); infraBox.getChildren().add(new TextField()); Button shortButton = new Button("OK"); Button longButton = new Button("\Überspringen"); ButtonBar buttonBar = new ButtonBar(); buttonBar.getButtons().addAll(shortButton, longButton); /*using BorderPane instead of AnchorPane*/ BorderPane bottomPanel = new BorderPane(); bottomPanel.setPadding(new Insets(5)); bottomPanel.setLeft(infraBox); bottomPanel.setCenter(buttonBar); return bottomPanel; } }

I hope this solve your problem我希望这能解决你的问题
simply use computed size ...只需使用计算的大小...
that means :这意味着:
button.setPrefSize(Control.USE_COMPUTED_SIZE);
this method as i know set the width to the button suitable for the text据我所知,此方法将宽度设置为适合文本的按钮
Sorry for the bad English抱歉英语不好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM