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

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

问题描述

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



我知道有fontProperty方法,但是我没有什么可绑定的,因为我无法弄清楚如何创建一个具有同步大小的动态ObjectProperty。我应该怎么做?



编辑:为了避免混淆,我试图根据其他因素来改变字体大小,而不是相反。 >

解决方案

只要将所需的字体大小更改为容器,并设置该样式或使用绑定(如果需要)。

  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?
标签lbl =新标签(我是标签);
TextArea ta = new TextArea(很多文本可以打字,甚至数字1234567890);
HBox hbox = new HBox(new Label(I never change));
VBox child = new VBox(btn,lbl,ta);
VBox root = new VBox(child,hbox);
场景场景=新场景(根,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'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.

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?

EDIT: 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\nand 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);
    }

}

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天全站免登陆