Css 如何更改JavaFX8 Modena主题中的基本字体大小?

Css 如何更改JavaFX8 Modena主题中的基本字体大小?,css,javafx-8,Css,Javafx 8,目前,Modena主题使用系统默认的12点字体大小,除非字体大小已缩放 根据CSS文件本身中的注释: RESIZING FOR DIFFERENT SCREEN DPI ------------------------------- When the screen DPI changes Windows will use a different font size by default. The default is 12px and can change to 15px or 18px d

目前,Modena主题使用系统默认的12点字体大小,除非字体大小已缩放

根据CSS文件本身中的注释:

RESIZING FOR DIFFERENT SCREEN DPI
-------------------------------

When the screen DPI changes Windows will use a different font size by default. 
The default is 12px and can change to 15px or 18px depending on user 
preference or screen DPI. On Mac the default is 13px and embedded will depend 
on hardware. To make UI controls scale and be the right proportions for each of 
these font sizes we base the padding (which controls size of control) on the 
font size. This is done using the CSS measurement unit of a "em" where
(1em = font size). The default sizes are based on Windows default of 12px, as
a quick reference here are common px sizes in em units on windows.

Windows 12px -> em units    -> Mac 13px      | 
----------------------------------------
     1px     -> 0.083333em  -> 1.08px ~ 2px
     2px     -> 0.166667em  -> 2.16px ~ 3px
     3px  = 0.25em
     4px  = 0.333333em
     5px  = 0.416667em
     6px  = 0.5em
     7px  = 0.583333em
     8px  = 0.666667em
     9px  = 0.75em
    10px  = 0.833333em
    11px  = 0.916667em
    12px  = 1em
不必更改CSS中的所有字体大小,是否有一个简单的属性可以将基础字体设置为大于或小于当前设置,从而允许所有其他间距、填充等进行缩放


谢谢

如果您根据
em
定义大小,那么它们将被定义为字体大小的比例。然后有类似的

.root {
  -fx-font-size: 15pt ;
}
在外部样式表中,将更改
1em
的定义,从而更改填充的大小等

如果希望从Java动态更改字体大小,可以执行以下操作

DoubleProperty fontSize = new SimpleDoubleProperty(12); // font size in pt
root.styleProperty().bind(Bindings.format("-fx-font-size: %.2fpt;", fontSize));
SSCCE:

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class FontSizeTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        Label label = new Label("Some text");
        Region rect = new Region();
        rect.getStyleClass().add("rect");
        VBox vbox = new VBox(label, rect);
        vbox.getStyleClass().add("vbox");

        Slider slider = new Slider(6, 24, 12);


        BorderPane root = new BorderPane(vbox, null, null, slider, null);
        BorderPane.setMargin(slider, new Insets(10));
        BorderPane.setAlignment(slider, Pos.CENTER);

        root.styleProperty().bind(Bindings.format("-fx-font-size: %.2fpt;", slider.valueProperty()));

        Scene scene = new Scene(root, 400, 400) ;
        scene.getStylesheets().add("font-size-test.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
font-size-test.css:

.rect {
    -fx-min-width: 1em ;
    -fx-max-width: 1em ;
    -fx-min-height: 1em ;
    -fx-max-height: 1em ;
    -fx-background-color: cornflowerblue ;
}
.vbox {
    -fx-spacing: 1em ;
    -fx-padding: 1.5em ;
}
请注意,在本例中移动滑块时,所有大小都会协调变化:文本大小、矩形大小、文本和矩形之间的间距以及包含标签和矩形的vbox周围的填充


你能编辑CSS吗?你可以编辑CSS或应用你自己的CSS,我正在这样做。下面的响应允许一行更改,它级联得很好。谢谢-这似乎真的增加了所有大小-即使我将其设置为12(默认设置),它似乎也会扭曲所有内容。但是它工作了,我可以玩它-谢谢。啊,我在
pt
中设置了字体,我想默认的是windows上的
12px
,mac上的
13px
(注意像素不是点);所以我认为这就解释了这种差异。这种缩放方式有问题吗?如果我使用-fx字体大小更改字体:1.05em;例如,它设置了整个设置,但是如果我更改系统设置,我看不到windows填充和缩放调整正确。嗯,不知道,在我的Mac上我无法测试(AFAIK在OS X中没有全局字体大小设置)。好的,问题是JDK 1.8_u45。我升级到1.8u60,DPI将扩展属性。我试着设置-fx字体大小:1.75em,并调整FXML的大小,以正确地适应新的样式。然后,我将操作系统级别的字体大小更改为150%,并且所有字体都正确缩放。谢谢你的提示。