Java中的等宽字体不是等宽字体 [英] Monospaced font isn't monospaced in Java

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

问题描述

我想设置

在图片中已经可以看到字母的宽度不同.另外,当我添加此代码时,它会打印不同的值.

  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 

解决方案

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

在设置文本(或系统支持的某些常用字体)之前,请尝试运行以下调用.然后尝试使用您的字体系列.如果找不到任何字体,则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);

这篇关于Java中的等宽字体不是等宽字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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