Set Font globally in JavaFX

后端 未结 3 1672
攒了一身酷
攒了一身酷 2020-12-09 11:00

How can I set the Font type globally in a JavaFX application?

Is there any solution that I can use? In JavaFX 8 the default Font has changed, and I would like to us

相关标签:
3条回答
  • 2020-12-09 11:25

    You can skin your application with CSS as described on the Oracle Website. Using following syntax you may set the general theme for your application:

    .root{
        -fx-font-size: 16pt;
        -fx-font-family: "Courier New";
        -fx-base: rgb(132, 145, 47);
        -fx-background: rgb(225, 228, 203);
    }
    

    You include the css as followed:

    scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
    
    0 讨论(0)
  • 2020-12-09 11:30

    Just a TornadoFX example

    package bj
    
    import tornadofx.*
    
    class MyView : View() {
        override val root = vbox {
            button("天地玄黄")
            button("宇宙洪荒")
        }
    }
    
    class MyStylesheet : Stylesheet() {
        init {
            root {
                fontFamily = "Noto Sans CJK SC Regular"
            }
        }
    }
    
    class MyApp : App(MyView::class, MyStylesheet::class)
    
    fun main(args: Array<String>) {
        launch<MyApp>(*args)
    }
    
    0 讨论(0)
  • 2020-12-09 11:49

    Changing the Default Font for a Scene

    This is the solution outlinked in nyyrikki's answer.

    You can change the default font used for most things in any given scene by applying the following CSS stylesheet to the scene:

    .root {
      -fx-font: 28px Vivaldi;
    }
    

    Substitute whatever settings you require for the -fx-font value according the font definition in the JavaFX CSS reference guide.

    Changing the Default Font for an Application

    If you want to change the default font used for most things in a JavaFX application, you can override the default style sheet using Application.setUserAgentStylesheet. Using this method you can set the default style for a JavaFX 8 application to the caspian stylesheet which was default for JavaFX 2.2 rather than the modena stylesheet which is default for JavaFX 8. If you want some hybrid of the two default styles or some custom default stylesheet such as AquaFX, then you will need to perform the customization yourself.

    Switching Font Rendering Technology

    Additionally, on some platforms, JavaFX 2.2 uses a different font rendering mechanism than JavaFX 8, which can account for subtle differences in font rendering between the two. There is an undocumented and unsupported command line switch which can be used to switch between font rendering mechanisms in JavaFX 8, but I don't know what the switch is off-hand and, even if I did, I wouldn't recommend deploying an application using the switch as it is unsupported.

    0 讨论(0)
提交回复
热议问题