【问题标题】:How to resize a button depending on the text in JavaFX?如何根据 JavaFX 中的文本调整按钮的大小?
【发布时间】:2021-08-28 07:36:17
【问题描述】:

这可能是一个微不足道的问题,但到目前为止我无法找到解决方案。
如果您创建一个带有较长单词作为文本的按钮,让我们说“Überspringen”(跳过的德语单词),JavaFx 会自动将文本截断为“Übersprin”+EllipsisString。
Button skipButton = new Button("\u00dcberspringen");

是否有避免截断的简单解决方案?我只能硬编码一个新的尺寸,但在 Swing 中,按钮尺寸是自动调整的。

我已经尝试过setWrapText(true),但没用,我想是因为没有空格。

编辑

这个最小的、可重现的示例显示了我的问题:
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("\u00dcberspringen");
       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;
    }
}

【问题讨论】:

  • 应该自动工作,因此您的设置中可能有些地方不太正确(确保使用适当的布局)或者您遇到了错误 :) 不管是什么原因,与往常一样的程序:@987654322 @ 必填。
  • @kleopatra 谢谢!这个怎么样?
  • hmm ..worksforme(fx17-dev,未检查早期版本)
  • 这也适用于我,JavaFX 15.0.1。
  • 啊 .. 是的,我在 fx8 中也看到了它 - 看起来像一些同时修复的问题(在 fx11 中还可以)。您可能会考虑升级到更新的版本(当前是 fx16,许多错误修复已向后移植到 fx11)。

标签: java button javafx resize


【解决方案1】:

通常按钮会调整大小,以便按钮的标签完全可见:

“例如,Button 对象的计算大小由文本长度和用于标签的字体大小以及任何图像的大小决定。通常,计算的大小刚好足够控件和标签完全可见。” (https://docs.oracle.com/javase/8/javafx/layout-tutorial/size_align.htm)。

但是,当您将按钮存储在具有最大宽度和高度的容器(例如 VBoxHBox)中时,例如,按钮将被挤压到该容器中,即标签被截断:

“默认情况下,按钮只会增长到它们的首选大小。但是,如果不覆盖最小宽度,按钮会缩小到标签显示为三个点 (...) 的位置。”。

我链接的文章描述了您的问题:

“为了防止按钮变得小于其首选宽度,请将其最小宽度设置为其首选宽度”,在“保持节点的首选大小”一节中。

如果您没有手动设置按钮的首选尺寸,则计算的尺寸会自动用于其首选尺寸:

“默认情况下,UI 控件根据控件的内容计算其首选大小的默认值”

您的代码示例不会产生所描述的问题(对我而言):

(对不起,我想评论你的帖子,但我的声誉太低了)

【讨论】:

  • 但是您更改了代码中的某些内容?在我提供的示例中,没有设置标题...您还更改了什么?
  • 我只添加了一个标题(stage.setTitle("Test");)。这不会干扰实际的根节点。
【解决方案2】:

我不知道为什么,所以我希望你们中的某个人知道......

解决办法:

只需将 AnchorPane 替换为这样的 BorderPane
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("\u00dcberspringen");
       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;
    }
}

【讨论】:

  • 好的,这是版本问题 - fx8 中的 anchorPane 布局似乎有问题(在 fx11 中正常)
  • 啊啊啊,谢谢!你在哪里找到的?
  • 已经用不同的版本运行了你的例子 :) 不知道(也不在乎,它已经过时了)为什么它会在 fx8 中中断。
【解决方案3】:

我希望这能解决你的问题
只需使用计算大小 ...
这意味着:
button.setPrefSize(Control.USE_COMPUTED_SIZE);
据我所知,此方法将宽度设置为适合文本的按钮
抱歉英语不好

【讨论】:

  • 不幸的是,这并没有解决我的问题,但还是谢谢你。
猜你喜欢
  • 2011-04-27
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 2016-04-25
相关资源
最近更新 更多