让JTextArea显示固定宽度的字体而不使用抗锯齿

时间:2022-11-10 15:20:11

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.

我很想用update-render-paint创建自己的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!

提前谢谢!

2 个解决方案

#1


24  

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.

您可以使用逻辑字体“monospaced”。虽然它保证所有字符的字体大小相同,但在所有平台上都不一样。

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)加载它。

#2


1  

I came close to a solution by using JTextPane instead of JTextArea by setting the content type of the JTextPane to text/html and setting the text as a html content.

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

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 monospaced and all white spaces and new line characters of the text have replaced with html space entity and <br> tag, respectively. I tested the output in both Windows and Ubuntu OSes and they were same.

标签使文本等宽,文本的所有空格和新行字符分别用html space entity和
tag替换。我测试了Windows和Ubuntu操作系统中的输出,它们是相同的。

#1


24  

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.

您可以使用逻辑字体“monospaced”。虽然它保证所有字符的字体大小相同,但在所有平台上都不一样。

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)加载它。

#2


1  

I came close to a solution by using JTextPane instead of JTextArea by setting the content type of the JTextPane to text/html and setting the text as a html content.

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

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 monospaced and all white spaces and new line characters of the text have replaced with html space entity and <br> tag, respectively. I tested the output in both Windows and Ubuntu OSes and they were same.

标签使文本等宽,文本的所有空格和新行字符分别用html space entity和
tag替换。我测试了Windows和Ubuntu操作系统中的输出,它们是相同的。