我试图在窗口改变大小时调整按钮文本的大小,但我不知道该怎么做。我使用的是NetBeans、JavaFX和场景生成器。看一下图片:按钮变大了,但字体大小保持不变。


发布于 2015-10-22 19:09:14
您可以使用setFont方法。例如
button.setFont(Font.font(40));更改文字样式的步骤
button.setFont(Font.font("Calibri",FontWeight.BOLD,20));发布于 2015-10-22 19:35:43
您可以更改scaleX属性和scaleY属性。
假设您正在使用outerPane调整大小。
final double origHeight = outerPane.getPrefHeight();
final double origWidth = outerPane.getPrefWidth();
button.scaleXProperty().bind(outerPane.widthProperty().divide(origWidth));
button.scaleYProperty().bind(outerPane.heightProperty().divide(origHeight));您可以根据设置应用程序的方式将代码粘贴到不同的位置。
我通过将此代码粘贴到FXMLDocumentController中进行了测试,替换了已经存在的空的initialze():
@FXML
private Button button;
@FXML
private AnchorPane outerPane;
@Override
public void initialize(URL url, ResourceBundle rb) {
final double origHeight = outerPane.getPrefHeight();
final double origWidth = outerPane.getPrefWidth();
button.scaleXProperty().bind(outerPane.widthProperty().divide(origWidth));
button.scaleYProperty().bind(outerPane.heightProperty().divide(origHeight));
}保存并编译,然后重新打开SceneBuilder,并在右侧的代码选项卡中为调整大小为outerPane的窗格设置按钮,将fx:id设置为button。
发布于 2017-06-20 11:37:55
您应该将一个值绑定到字体大小,并使用styleProperty()应用它,如下所示:
DoubleProperty fontSize22 = new SimpleDoubleProperty(22);
fontSize22.bind(homeButton.widthProperty().add(homeButton.heightProperty()).divide(10));
homeButton.styleProperty().bind(Bindings.concat("-fx-font-size: ", fontSize22.asString(), "px;"));https://stackoverflow.com/questions/33277498
复制相似问题