java 让 JTextArea 在没有抗锯齿的情况下显示固定宽度的字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16279781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:30:13  来源:igfitidea点击:

Getting JTextArea to display fixed-width font without antialiasing

javaswingfontscross-platformjtextarea

提问by Dmitry

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

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

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.

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

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.

问题是 courier new 显然是专有的,不仅在许多系统上没有默认安装,而且在大多数现代系统上,它被设置为默认的 cleartype,这使它看起来像垃圾。

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.

我很想用更新-渲染-绘制制作我自己的 JPanel 并重新发明 JTextArea,并将字体保存为固定大小的位图,但这种方法看起来很愚蠢,而且非常耗时。

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.

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

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

可悲的是,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?

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

Thanks ahead of time!

提前致谢!

回答by Guillaume Polet

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("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();
            }
        });

    }

}

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).

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

回答by CLOUGH

I came close to a solution by using JTextPaneinstead of JTextAreaby setting the content type of the JTextPaneto text/htmland setting the text as a htmlcontent.

我通过使用JTextPane而不是JTextArea通过设置JTextPaneto的内容类型text/html并将文本设置为内容来接近解决方案html

JtextPane textPane;
String text;

// initialization

textPane.setContentType("text/html");
textPane.setText(""
        + "<html>"
        + "<body>"
        + "<p>"
        + "<tt>"+text.replaceAll(" ", "&nbsp;").replaceAll("\n", "<br>")+"</tt>"
        + "</p>"
        + "</body>"
        + "</html>"                
        + "");

<tt>tag is making the text monospacedand all white spaces and new line characters of the texthave replaced with html space entityand <br>tag, respectively. I tested the output in both Windows and Ubuntu OSes and they were same.

<tt>tag 使文本等宽,并且 的所有空格和换行符text分别替换为 html空间实体<br>标签。我在 Windows 和 Ubuntu 操作系统中测试了输出,它们是相同的。