JavaFX没有找到所有的系统字体 [英] JavaFX not finding all system fonts
问题描述
我注意到,我使用的JavaFX组件不像Swing的对应组件,不能正确地渲染一些系统字体。这里是完整的例子,展示了这个问题。
public class JavaFXApplication1 extends Application {
@Override
public void start(Stage primaryStage){
// Swing button
SwingNode node = new SwingNode();
SwingUtilities.invokeLater(() - > {
JButton swingButton = new JButton(Comic Sans MS);
swingButton.setFont(new java.awt.Font(Comic Sans MS ,java.awt.Font.PLAIN,20));
node.setContent(swingButton);
});
// JavaFX按钮
Button fxButton = new Button(Comic Sans MS);
fxButton.setFont(Font.font(Comic Sans MS,20));
HBox hbox = new HBox();
hbox.getChildren()。addAll(node,fxButton);
场景场景=新场景(hbox,300,250);
primaryStage.setTitle(Font test);
primaryStage.setScene(scene);
primaryStage.show();
public static void main(String [] args){
launch(args);
$ b $如果你的系统中有Comic Sans MS字体,你应该看到下面的结果。只有Swing组件看起来不错:
显然FX图形系统在启动时不加载所有系统字体。最明显的解决方案是手动加载丢失的文件,如下所示:
Font.loadFont(new FileInputStream(System.getenv WINDIR)+\\fonts\\+comic.ttf),20);
现在一切正常,但这个解决方案不是跨平台的,我真的期望它就像在Swing中那样开箱即用。我错过了什么吗?如果没有,那么为什么只有一部分系统字体被加载到JavaFX环境中?
我在Windows 10上运行它。
我找到了另外一件事:我创建了下面的代码:
> String fonts [] = GraphicsEnvironment.getLocalGraphicsEnvironment()。getAvailab leFontFamilyNames(); for(String font:fonts){System.out.printf(This is font%s%n,font);
返回例如:这是字体Something Strange(它是我下载和安装的一种字体)其可用。但是,当我把它放到这个代码:
< GridPane fx:controller =sample.Controller
xmlns :fx =http://javafx.com/fxmlalign =centerhgap =10vgap =10
style = - fx-font-family:奇怪的东西>
文字完全不会改变。也许这有助于你/我们的问题。
I've noticed that the JavaFX components I use, unlike their Swing counterparts, are not rendering some system fonts properly. Here's the complete example that presents the issue.
public class JavaFXApplication1 extends Application {
@Override
public void start(Stage primaryStage) {
// Swing button
SwingNode node = new SwingNode();
SwingUtilities.invokeLater(() -> {
JButton swingButton = new JButton("Comic Sans MS");
swingButton.setFont(new java.awt.Font("Comic Sans MS", java.awt.Font.PLAIN, 20));
node.setContent(swingButton);
});
// JavaFX button
Button fxButton = new Button("Comic Sans MS");
fxButton.setFont(Font.font("Comic Sans MS", 20));
HBox hbox = new HBox();
hbox.getChildren().addAll(node, fxButton);
Scene scene = new Scene(hbox, 300, 250);
primaryStage.setTitle("Font test");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
If you have the Comic Sans MS font in your system, you should see the following result. Only the Swing component looks fine:
Apparently the FX graphics system does not load all system fonts on startup. The most obvious solution is to load the missing ones manually, like that:
Font.loadFont(new FileInputStream(System.getenv("WINDIR")+"\\fonts\\"+"comic.ttf"), 20);
Now everything renders properly, but this solution isn't cross platform and I'd really expect it to work out-of-the-box just like it was in Swing. Am I missing something? If not, then why only a subset of system fonts is loaded into the JavaFX environment?
I'm running it on Windows 10.
解决方案 I figured out another thing: I created following code
String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); for(String font: fonts) { System.out.printf("This is font %s %n", font);
This returns for example: This is font Something Strange (its a font which i downloaded and installed) so its available. But when I put it into this code:
<GridPane fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10"
style="-fx-font-family: Something Strange">
The text does not change at all. Maybe this contribute to your / our problem.
这篇关于JavaFX没有找到所有的系统字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!