在 JavaFX 中绑定字体大小? [英] Bind Font Size in JavaFX?

查看:36
本文介绍了在 JavaFX 中绑定字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的应用程序流畅.但是,当我将窗口放大时,与 UI 元素相比,字体看起来很小.最终,当我调整窗口大小时,我希望文本的大小变大或变小.我知道理论上我可以用 style 属性来做到这一点,但在某些情况下,我已经将该属性绑定到其他东西.

I'd like to make my application fluid. However, the fonts look small compared to the UI elements when I make the windows bigger. Ultimately, I want the size of my text to get bigger or smaller when I resize the window. I know I could theoretically do this with the style property, but I already have that property bound to something else in some instances.

我知道有fontProperty"方法,但我没有任何东西可以绑定它,因为我不知道如何创建一个具有同步大小的动态 ObjectProperty.我该怎么办?

I know there's the "fontProperty" method, but I have nothing to bind it to since I can't figure out how to create a dynamic ObjectProperty with a synced size. What should I do?

为避免混淆,我试图根据其他因素来更改字体大小,而不是相反.

To avoid confusion, I'm trying to make the Font size change based on other factors, not the other way around.

推荐答案

只需将所有要更改字体大小的内容放在容器中,然后设置该样式或根据需要使用绑定.

Just put everything where you want the fontsize to change in a container and set that style or use bindings if you want.

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class FontBind extends Application {

    private DoubleProperty fontSize = new SimpleDoubleProperty(10);
    private IntegerProperty blues = new SimpleIntegerProperty(50);

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button("click me, I change color");
        btn.setOnAction((evt)->{blues.set(blues.get()+20);});//max?
        Label lbl = new Label("I'm a label");
        TextArea ta = new TextArea("Lots of text can be typed
and even number 1234567890");
        HBox hbox = new HBox(new Label("I never change"));
        VBox child = new VBox(btn, lbl, ta);
        VBox root = new VBox(child, hbox);
        Scene scene = new Scene(root, 300, 250);

        fontSize.bind(scene.widthProperty().add(scene.heightProperty()).divide(50));
        child.styleProperty().bind(Bindings.concat("-fx-font-size: ", fontSize.asString(), ";"
                                                  ,"-fx-base: rgb(100,100,",blues.asString(),");"));

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

我不确定您的样式已绑定到什么,但您可以在样式字符串中设置多个 css 值.

I'm not sure what your style is already bound to, but you're allowed to set multiple css values in the style string.

这篇关于在 JavaFX 中绑定字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆