简体   繁体   中英

Non-ASCII Character not displaying properly in JFrame or any Swing Component

Let's say we desire to have a non-ASCII character, for example, U+2082 (subscript 2).

Normally, we can display this in a swing component, such as JFrame, as Character.toString('\₂') .

Now, my issue is that I can't determine the the exact Unicode code, since the exact code is determined by the String supplied in the parameter. The parameter will always be a polyatomic ion - eg PO3. What my goal is to find the "3", turn that into a subscript 3 (U+2083), but also have the algorithm/method be abstracted enough that it will apply for any polyatomic ion (not just PO3, but also PO4 as well), and have it display correctly on a JFrame. I supply my method below.

private static String processName(String original)
{

    char[] or = original.toCharArray();

    int returned = -1;

    for(int i = 0; i < or.length; i++)
    {

        if(Character.isDigit(or[i]))
        {
            returned = Integer.parseInt(Character.toString(or[i])); 
            or[i] = (char) (returned + 2080);
            returned = -1;
        }

    }

    return new String(or);
}

You probably are thinking, well, the code looks clean and should display correctly. However, the part (char) (returned+2080) doesn't display the symbol - it displays a blank box. I tried to fix it by setting a compatible font (GNU Unifont), but that didn't do anything. Any ideas?

2083 is a hex value, not decimal. See the unicode page for details about this character. I think the value you want is 0x2083, or 8323

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM