java - Java同时显示繁体中文和简体中文

标签 java unicode internationalization

是否可以在同一个 Java 应用程序中使用像“SansSerif”这样的逻辑字体来绘制繁体和简体中文字符?我只得到带有 CJK 代码点的传统品种。

我尝试在创建字体之前设置 Locale.setDefault() 和 GraphicsEnvironment.preferLocaleFonts()。启动 java.exe 时,我尝试在命令行上使用 -Duser.language 和 -Duser.country。还尝试创建带有 AttributedCharacterIterator.Attribute.LANGUAGE 设置的字体。没有影响。

我没有使用 Swing 或 AWT。只是试图绘制到屏幕外的 BufferedImage 中。我在 Windows 7 上,我确认我安装了支持繁体和简体中文(MingLiU 和 SimSun)的字体。我还检查了 Java 的字体配置文件,我看到那里列出了这两种字体。

我还应该做什么?

最佳答案

是的,您可以在 Java 中同时显示简体中文和繁体中文文本,只要您的字体包含这两种字符集即可。

我写了这个简短的程序来演示:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

public class ChineseFonts {

    public static final int GAP = 35;
    public static final int FONTS_PER_LINE = 2;

    public ChineseFonts(String s) {

        Rectangle rect = new Rectangle(0, 0, 1024, 768);
        BufferedImage bufferedImage = new BufferedImage((int) Math.ceil(rect.getWidth()), (int) Math.ceil(rect.getHeight()), BufferedImage.TYPE_4BYTE_ABGR);
        Graphics graphics = bufferedImage.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(rect.x, rect.y, rect.width, rect.height);
        graphics.setColor(Color.BLUE);

        String title = "Chinese Fonts on " + System.getProperty("os.name") + ", version " + System.getProperty("os.version");
        int fontY = 30;
        printString(title, graphics, 0, fontY, new Font(Font.SERIF, Font.BOLD | Font.ITALIC, 28), false);
        fontY += GAP + 10;
        int counter = 0;
        for (String fontName : new String[]{Font.MONOSPACED, Font.SANS_SERIF, Font.SERIF}) {
            Font font = new Font(fontName, Font.PLAIN, 24);
            printString(s, graphics, counter++, fontY, font, true);
            if (counter % FONTS_PER_LINE == 0)
                fontY += GAP;
        }
        Font[] localFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
        List<Font> chineseFonts = new ArrayList<Font>();
        String simplifiedAndTraditionalChinese = "????";
        for (int j = 0; j < localFonts.length; j++) {
            if (localFonts[j].canDisplayUpTo(simplifiedAndTraditionalChinese) == -1) {
                chineseFonts.add(localFonts[j].deriveFont(24F));
            }
        }
        for (Font font : chineseFonts) {
            printString(s, graphics, counter++, fontY, font, true);
            if (counter % FONTS_PER_LINE == 0)
                fontY += GAP;
        }
        graphics.dispose();
        try {
            ImageIO.write(bufferedImage, "png", new File("chineseFonts.png"));
        } catch (Exception e) {
            // ignored
        }
    }

    private void printString(String s, Graphics graphics, int counter, int y, Font font, boolean showFontDetails) {
        graphics.setFont(font);
        if (showFontDetails)
            s = font.getFamily() + " " + s;
        graphics.drawString(s, 20 + (counter % FONTS_PER_LINE) * 510, y);
        if (showFontDetails)
            System.out.println("Printing " + s + " using " + font.getName() + ", which is " + font.getFontName() + " in family " + font.getFamily());
    }

    public static void main(String args[]) {
        new ChineseFonts("S: 漢字 T: 汉字");
    }
}

使用的2个简体字和2个繁体字来自维基百科汉字页面。当你运行它时,所有使用的字体都被打印到标准输出,并且图像与字体名称和汉字一起输出。

这是 3 种逻辑字体和第一种有效的物理字体:

  • 等宽
  • 无衬线字体
  • 衬线
  • Arial Unicode MS S

关于java - Java同时显示繁体中文和简体中文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9172842/

相关文章:

internationalization - 如何使用 r.js 和 hbs.js 使用多个语言环境

java - 是否可以在 Android 中的 ArrayAdapter getView() 中膨胀两个布局?

java - Spring Boot - 自定义查询上的空指针

java - 如何在java中将Unicode(utf-8)转换为十六进制字符串?

MySQL不想存储unicode字符

java - Java 与 C# 中不同的 UTF-16 编码

java - AspectJ 未捕获所有方法

java - 具有长值的 JSpinner

spring - Spring Boot Controller区域设置不随参数更改

jquery - 为什么 jQuery DataTables 国际化 i18n 对我不起作用?