テキストを等幅フォントに設定したかったのです。結局、GT Pressura Mono Regular Regularを選びました。
しかし、彼について何かが奇妙でした。文字幅の違いはわずかですが、テキストが等幅ではないことがわかりました。
フォントに問題があると思ったのですが、他の等幅フォント (ダウンロードしたフォントと Windows からのフォントの両方) を試しましたが、幅が一致しません。何が問題ですか?テキストには大文字のみを使用します。
助けてください。
ありがとうございました
アップデート
より良いイラストのためにコードを追加しています。
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);
}
}
文字の幅が同じでないことは、写真ですでに確認できます。さらに、このコードを追加すると、異なる値が出力されます。
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