multi language in Component

2019-07-24 06:25发布

问题:

I am using Swing, and I need the components to be able to display multiple languages at the same time (inside one component). However, the characters other than English characters are displayed as squares.

The Components are just JTextPane, JEditorPane, JTextField, JTextArea and all the default stuff.

What must I do to achieve the goal of multi language support?

回答1:

On most platforms, the default logical font family specified by each component's UI delegate has the required glyphs for supported locales. If you override these defaults with a particular physical font using setFont(), you may be able to get the desired result using deriveFont(), as suggested in theses examples. If you must use a particular physical font, you'll have to verify that the required glyphs are present. FontShower may be a useful adjunct.



回答2:

Use a full Unicode font. They are large. Then setFont(...) does the trick.

If you want to pack a font with your application loadFont and registerFont will do.

    Font font = Font.createFont(Font.TRUETYPE_FONT,
        getClass().getResourceAsStream("/..."));
    GraphicsEnvironment ge =
        GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.registerFont(font);