获取JTextArea以显示固定宽度的字体而不进行抗锯齿 [英] Getting JTextArea to display fixed-width font without antialiasing

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

问题描述

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

我想用一个简单的代码编辑器来保存/开放的功能,
这是很简单,但我想获得字体为
固定大小,最好是快递新。



问题是快递新专有明显,而不是
只是它不是默认安装在许多系统上,但在大多数
现代系统,它被设置为cleartype默认,这使得
它看起来像垃圾。



我试图使用update-render-paint和
来创建自己的JPanel,重新创建JTextArea,并将字体保存为固定大小的位图,但
这种方法显得很傻,而且非常耗时。

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



不幸的是,似乎Swing自动遵守系统首选项
,所以在不破坏用户的设置的情况下,它似乎是一个不行。

所以总之,有没有办法让JTextArea显示一个固定宽度的
字体,并禁用字体平滑/抗锯齿(或至少切换),或者
这个任务不可能使用swing?

提前感谢!

解决方案

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

  import java.awt.Font; 

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

public class TestTextArea {
$ b $ 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();
}
});



$ b $ / code>

或者,你可以查找符合您需要的免费字体,将该字体嵌入到代码中,并使用 java.awt.Font.createFont(int,InputStream) / p>

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.

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.

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.

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

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?

Thanks ahead of time!

解决方案

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

这篇关于获取JTextArea以显示固定宽度的字体而不进行抗锯齿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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