热门动态更新字体大小 [英] Hot to update dynamically font size
问题描述
我有这个css文件,用于设置JavaFX应用程序中的默认字体大小和类型:
I have this css file which sets the default font size and type in JavaFX application:
.root {
-fx-font: 13px Tahoma;
}
scene.getStylesheets().add(getClass().getResource("/styles/Styles.css").toExternalForm());
我想动态更新根组件的Java代码的大小和字体类型(全部组件)。我怎么做?
I want to update the size and the font type from the Java code dynamically of the root component (all components). How I can do this?
注意:
此代码将所有组件的Font类型和大小更新到JavaFX应用程序中。
Note: This code updates the Font type and size of all components into the JavaFX application.
推荐答案
请考虑查看官方JavaFX 文档。在那里你找到了回答你问题的代码示例:
Please consider taking a look at the official JavaFX documentation. There you find the code example which answers your question:
Text t = new Text("That's the text");
t.setFont(Font.font ("Verdana", 20));
更新
在应用程序控制器中获取根窗格的实例,例如 AnchorPane
并使用 setId()
函数为整个窗格设置新样式(我的 actionChange
与窗格上的按钮连接,触发事件/更改):
In your application controller get an instance of your root pane, e.g. AnchorPane
and use the setId("")
function to set new style for the whole pane (my actionChange
is connected with a button on the pane, which triggers the event/change):
public class AppController implements Initializable {
@FXML
private AnchorPane mainPane;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
@FXML
public void actionChange() {
mainPane.setId("fancytext");
}
}
按下按钮时,窗格的样式为改变。我只是用font-size作为例子。之前,您需要在CSS文件中指定新样式:
When pressing the button, the style for the pane is changed. I just used the font-size as an example. Before, you need to specify the new style in your CSS file:
.root {
-fx-font: 12px Tahoma;
}
#fancytext {
-fx-font: 20px Tahoma;
}
之前:
之后是按下按钮:
这篇关于热门动态更新字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!