Hello,
I'm not sure where to post this. I'm trying to write a simple GUI Unicode Viewer. The user enters the Hex code in the JTextField and the program generates the Unicode character for that code as well as the next 320 Unicode characters ad displays them in a JTextArea. However, when I try to insert the "\u" escape character in the front of the Hex code I get an "illegal unicode escape" compiler error. How do I fix this? I tried converting the hex string to an int and explicitly cast it to a char which worked for small values but caused a NumberFormatException on larger values
Here is the code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class Exercise31_1 extends JApplet { private JTextField jtfUnicode = new JTextField(5); private JTextArea jtaUnicode = new JTextArea(); private String hexCode; private int hexNumber; public void init() { JPanel p1 = new JPanel(new BorderLayout()); p1.add(jtfUnicode, BorderLayout.CENTER); p1.setBorder(new TitledBorder("Specify Unicode")); JPanel p2 = new JPanel(new BorderLayout()); p2.add(new JScrollPane(jtaUnicode), BorderLayout.CENTER); jtaUnicode.setEditable(false); add(p1, BorderLayout.NORTH); add(p2, BorderLayout.CENTER); //jtaUnicode.append("\u4F20"); jtfUnicode.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { hexCode = jtfUnicode.getText().trim(); hexNumber = HexToDecimalConversion.hexToDecimal(hexCode); for (int i = 0; i < 20; i++) { jtaUnicode.append(hexCode + " "); for (int j = 1; j < 16; j++) { hexNumber++; hexCode = Decimal2HexConversion.decimalToHex(hexNumber); jtaUnicode.append(hexCode+ ","); } jtaUnicode.append("\n\n"); hexNumber++; hexCode = Decimal2HexConversion.decimalToHex(hexNumber); } } }); } public static void main(String[] args) { Exercise31_1 applet = new Exercise31_1(); JFrame frame = new JFrame("Unicode viewer"); frame.add(applet, BorderLayout.CENTER); applet.init(); applet.start(); frame.setSize(500, 300); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
Thanks in advance for the help.
Casey