i have 2 JTextArea that one of these contain Unicode Code point like this \u0645 i want another JTextArea 开发者_如何转开发
show Character representation of this Unicode code point.but when pass this code point to JTextArea , it show
code point not Character but if i set code point to JTextArea setText method directly it work correctly !
why ? and which can i pass String of Codepoint from one JTextArea to another ?
thanks
This code displays a character, and in the other text-area the corresponding "unicode string" counterpart:
import java.awt.*;
import javax.swing.*;
public class FrameTest {
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
cp.setLayout(new BorderLayout());
JTextArea ta1 = new JTextArea(20, 20);
JTextArea ta2 = new JTextArea(20, 20);
Character c = '\u0645';
ta1.setText("" + c);
String s = String.format("\\%04x", (int) c.charValue());
ta2.setText(s);
cp.add(ta1, BorderLayout.WEST);
cp.add(ta2, BorderLayout.EAST);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(500, 100);
jf.setVisible(true);
}
}
So if you have a long text of such characters, you would need to loop through the string character by character, (using getCharAt(int)
or getChars()
) and process each character with String.format("\\%04x", (int) c.charValue());
and append the result to the target string. (Preferrably using a StringBuffer
.)
JTextArea text=new JTextArea();
String dir=text.getText();
JTextArea text2=new JTextArea();
text2.setText(dir);
but this don't work, dir contain string of code points.if i pass String of codepoint directly to
setText("\u0645\u0645") it's work !
If i set code point to JTextArea setText method directly it work correctly !
If by this you mean something like myTextArea.setText('\u0645')
, then the issue is rather clear:
The Java compiler interprets \u
as the beginning of a unicode escape. That means that anyhwere in Java source code the characters \u0645
are completely equivalent to actually putting the character م
(Unicode character U+0645 ARABIC LETTER MEEM) at that same place.
So the following two lines do exactly the same thing:
myTextArea.setText('\u0645')
myTextArea.setText('م')
The reason for this is, because at compile time \u0645
is turned into the corresponding Unicode character.
It's an entirely different situation if you have the Java String that contains the 6 characters \u0645
. That String can be represented as the Java String literal "\\u0645"
(note the double-backslash to avoid the compiler interpreting the Unicode escape).
In that case you can grab the third to last character ("\\u0645".subString(2)
), parse that as a hexadecimal number (with Integer.parseInt(theString, 16)
) and cast the result to char
. Then you'll have a char
value containing the actual Unicode character.
精彩评论