且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java中的等宽字体不是等宽字体

更新时间:2023-11-12 08:05:40

该问题最有可能发生,因为它找不到字体.

在设置文本(或系统支持的某些常用字体)之前,请尝试运行以下调用.然后尝试使用您的字体系列.如果找不到任何字体,则Java无法找到已安装的字体.

  System.out.println(Font.getFontNames("Consolas"))); 

以下内容适用于Windows 10.

 标签标题=新标签(测试");title.setFont(Font.font("Consolas",FontWeight.THIN,60));title.setTextFill(Color.RED); 

I wanted to set the Text to a monospaced font. In the end, I chose GT Pressura Mono Regular Regular.

But, something about him was weird. I have found that the text isn't monospaced, although the differences in letter width are small.

I thought there was a problem with the font, but I tried other monospaced fonts (both downloaded and from windows) and the width never matches. What is the problem? I only use upper case letters for text.

Please help.

Thank you

UPDATE

I am adding a code for better illustration.

import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextBoundsType;
import javafx.stage.Stage;

public class MonospacedText extends javafx.application.Application {

    @Override
    public void start(Stage stage) {
        var textA = new Text("A");
        textA.setBoundsType(TextBoundsType.VISUAL);
        textA.setFont(new Font("Consolas", 100));
    
        var textB = new Text("B");
        textB.setBoundsType(TextBoundsType.VISUAL);
        textB.setFont(new Font("Consolas", 100));
    
        var textC = new Text("C");
        textC.setBoundsType(TextBoundsType.VISUAL);
        textC.setFont(new Font("Consolas", 100));
    
        var textD = new Text("D");
        textD.setBoundsType(TextBoundsType.VISUAL);
        textD.setFont(new Font("Consolas", 100));
    
        var textE = new Text("E");
        textE.setBoundsType(TextBoundsType.VISUAL);
        textE.setFont(new Font("Consolas", 100));
    
        var vbox = new VBox(textA, textB, textC, textD, textE);
        vbox.setAlignment(Pos.TOP_LEFT);
        stage.setScene(new Scene(vbox));
        stage.show();
    }

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

It can already be seen in the picture that the letters aren't the same width. In addition, when I add this code, it prints different values.

System.out.println(textA.getLayoutBounds().getWidth());
System.out.println(textB.getLayoutBounds().getWidth());
System.out.println(textC.getLayoutBounds().getWidth());
System.out.println(textD.getLayoutBounds().getWidth());
System.out.println(textE.getLayoutBounds().getWidth());

// 54.00390625
// 41.89453125
// 44.091796875
// 46.09375
// 36.279296875

The issue is most likely occurring because it is unable to find the font.

Please try running the below call before setting the text (or some common font your system supports). Then try with your font-family. If it is not finding any fonts, Java is not able to find the installed fonts.

System.out.println(Font.getFontNames("Consolas"));

The below works for me in Windows 10.

Label title = new Label("Testing");
title.setFont(Font.font("Consolas", FontWeight.THIN, 60));
title.setTextFill(Color.RED);