测量JavaFX中文本的长度 [英] Measure the length of text in JavaFX

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

问题描述

这是我编写的一个简单程序,在舞台上的圆角矩形背景上有一个按钮.

Here is a simple program that I wrote to have a button on the stage over a rounded rectangle background.

package definitive_guide;

import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.StackPaneBuilder;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class SingleButton extends Application {
    Button btn;
    Scene scene;
    StackPane stack;
    Rectangle roundedRectangle;

    @Override
    public void start(Stage stage) throws Exception {
        btn = ButtonBuilder
                .create()
                .font(Font.font("DejaVu Sans Mono",22))
                .text("Hello World")
                .build();

        roundedRectangle = RectangleBuilder
                .create()
                .width(300)
                .height(300)
                .arcHeight(150)
                .arcWidth(200)
                .fill(Color.rgb(128, 128, 128))
                .build();

        stack = StackPaneBuilder
                .create()
                .children(roundedRectangle , btn)
                .build();
        stack.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);

        scene = SceneBuilder
                .create()
                .root(stack)
                .build();

        stage.setScene(scene);
        stage.show();


    }


    public static void main(String[] args) {
        Application.launch("definitive_guide.SingleButton.java");
    }

}  

现在,手动输入矩形的长度,宽度和弧度的值.因此,它们不是动态的.

For now, the values for the length , breadth and the arcs of the rectangle are manually entered. So, they are not dynamic.

我希望能够根据文本的大小加上一些填充来设置其大小,以便在不同分辨率的设备上具有一致的外观.
在Swing中,FontMetrics很方便,但是我不知道在JavaFX中该怎么做.

I want to be able to set its size depending upon the size of the text plus some padding so as to have a consistent look and feel across devices of different resolutions.
In Swing, FontMetrics was handy but I do not know what to do in JavaFX.

我该怎么做?

推荐答案

如果您真的关心Text,请尝试以下操作:

If you really concerned with Text, try this:

final Text text = new Text("Click me");
text.setFont(Font.font("DejaVu Sans Mono", 42));

text.setOnMouseClicked(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent event) {
        // set with long text
        text.setText("Hello Sun, Hello Galaxy, Hello Space!");
    }
});

text.layoutBoundsProperty().addListener(new ChangeListener<Bounds>() {
    @Override
    public void changed(ObservableValue<? extends Bounds> observable, Bounds oldValue, Bounds newValue) {
        if(oldValue.getHeight() != newValue.getHeight() || oldValue.getWidth()!= newValue.getWidth()) {
            roundedRectangle.setWidth(newValue.getWidth() + 100);
            roundedRectangle.setHeight(newValue.getHeight() + 30);
        }
    }
});

否则,如果您对button的文本感兴趣,则可以尝试直接绑定到button:

Else if the text of button is your interest, you can try bindings to button directly:

roundedRectangle.widthProperty().bind(btn.widthProperty().add(100));
roundedRectangle.heightProperty().bind(btn.heightProperty().add(30));

弧的宽度和高度值也是可绑定的:

The arc width and height values are also bindable:

roundedRectangle.arcHeightProperty()
roundedRectangle.arcWidthProperty()

这篇关于测量JavaFX中文本的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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