让 JTextArea 在没有抗锯齿的情况下显示固定宽度的字体 [英] Getting JTextArea to display fixed-width font without antialiasing

查看:22
本文介绍了让 JTextArea 在没有抗锯齿的情况下显示固定宽度的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何让 JTextArea 显示固定大小所有平台上的字体?

Does anybody know how to get JTextArea to display a fixed size font on all platforms?

我想制作一个带有保存/打开功能的简单代码编辑器,这很简单,但我想让字体成为固定尺寸,最好是快递新的.

I want to make a simple code editor with save/open functionality, which is simple enough, but I would like to get font to be fixed-size, preferably courier new.

问题是 courier new 显然是专有的,而不是只是默认情况下它没有安装在许多系统上,但在大多数系统上现代系统,默认设置为 cleartype,这使得看起来像垃圾.

The problem is that courier new is proprietary apparently, and not only is it not installed by default on many systems, but on most modern systems, it is set to cleartype be default, which makes it look like garbage.

我很想用 update-render-paint 和重新发明 JTextArea,并将字体保存为固定大小的位图,但是这种方法看起来很愚蠢,而且非常耗时.

I am tempted to make my own JPanel with update-render-paint and reinvent the JTextArea, and save fonts as fixed-size bitmaps, but this approach appears silly, and very time consuming.

我想在项目中包含一个免费的固定大小的字体,并且在所有平台上使用该字体,这似乎是可能的.然而,现代系统似乎强制平滑所有字体,我会喜欢阻止它做.

I would like to include a free fixed-size font to the project and use that font on all platforms, which appears possible. However, modern systems appear to force-smooth all fonts, which I would like to prevent it from doing.

遗憾的是,Swing 似乎自动遵守系统偏好所以在不破坏用户设置的情况下,这似乎是不行的.

Sadly, it appears that Swing automatically abides by system preferences so without destroying user's settings, it appears to be a no go.

所以简而言之,有没有办法让 JTextArea 显示一个固定宽度的字体并禁用字体平滑/抗锯齿(或至少切换),或使用 Swing 无法完成这项任务吗?

So in short, is there a way to get JTextArea to display a fixed-width font and disable font smoothing/antialiasing (or at least toggle), or is this task impossible using swing?

提前致谢!

推荐答案

您可以使用逻辑字体等宽".虽然它保证所有字符的字体大小相同,但它不会在所有平台上都相同.

You can use the logical font "monospaced". While it guarantees to have a font that has the same size for all characters, it won't be the same on all platform.

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class TestTextArea {

    private void initUI() {
        JFrame frame = new JFrame("test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextArea textArea = new JTextArea(24, 80);
        textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
        frame.add(new JScrollPane(textArea));
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestTextArea().initUI();
            }
        });

    }

}

或者,您可以查找免费"满足您需要的字体,将该字体嵌入代码并使用 java.awt.Font.createFont(int, InputStream) 加载.

Alternatively, you could look up a "free" font which meets your need, embed that font in with code and load it with java.awt.Font.createFont(int, InputStream).

这篇关于让 JTextArea 在没有抗锯齿的情况下显示固定宽度的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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