如何根据 JavaFX 中的文本调整按钮的大小?
How to resize a button depending on the text in JavaFX?
这可能是一个微不足道的问题,但到目前为止我还没有找到解决方案。
如果您创建一个带有较长单词作为文本的按钮,假设为“Überspringen”(Skip 的德语单词),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;
}
}
通常按钮会调整大小,以便按钮标签完全可见:
"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." (https://docs.oracle.com/javase/8/javafx/layout-tutorial/size_align.htm).
但是,当您将按钮存储在具有最大宽度和高度的容器(例如 VBox
或 HBox
)中时,按钮将被挤压到该容器中,即标签被截断:
"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.".
我链接的文章描述了您的问题:
"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".
如果您没有手动设置按钮的首选尺寸,计算出的尺寸会自动用作其首选尺寸:
"By default, UI controls compute default values for their preferred size that is based on the content of the control"
您的代码示例没有产生所描述的问题(对我而言):
(对不起我想评论你的post,但是我的声望太低了)
希望这能解决你的问题
简单地使用计算大小...
这意味着:
button.setPrefSize(Control.USE_COMPUTED_SIZE);
据我所知,此方法将宽度设置为适合文本的按钮
抱歉英语不好
我不知道为什么,所以我希望你们中有人知道....
解决方案:
只需用这样的 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("\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;
}
}
这可能是一个微不足道的问题,但到目前为止我还没有找到解决方案。
如果您创建一个带有较长单词作为文本的按钮,假设为“Überspringen”(Skip 的德语单词),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;
}
}
通常按钮会调整大小,以便按钮标签完全可见:
"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." (https://docs.oracle.com/javase/8/javafx/layout-tutorial/size_align.htm).
但是,当您将按钮存储在具有最大宽度和高度的容器(例如 VBox
或 HBox
)中时,按钮将被挤压到该容器中,即标签被截断:
"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.".
我链接的文章描述了您的问题:
"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".
如果您没有手动设置按钮的首选尺寸,计算出的尺寸会自动用作其首选尺寸:
"By default, UI controls compute default values for their preferred size that is based on the content of the control"
您的代码示例没有产生所描述的问题(对我而言):
(对不起我想评论你的post,但是我的声望太低了)
希望这能解决你的问题
简单地使用计算大小...
这意味着:
button.setPrefSize(Control.USE_COMPUTED_SIZE);
据我所知,此方法将宽度设置为适合文本的按钮
抱歉英语不好
我不知道为什么,所以我希望你们中有人知道....
解决方案:
只需用这样的 BorderPane 替换 AnchorPanepublic 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;
}
}