我正在尝试在表中显示unicode字符串,如以下程序中所指定。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author dell
*/
public class UnicodeExample extends javax.swing.JFrame {
/** Creates new form UnicodeExample */
public UnicodeExample() {
initComponents();
setTitle("Unicode Example");
setSize(400, 400);
setLocationRelativeTo(null);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel1.setText("Enter :");
getContentPane().add(jLabel1);
jLabel1.setBounds(24, 50, 70, 20);
jTextField1.setText("\\u00a5");
getContentPane().add(jTextField1);
jTextField1.setBounds(110, 50, 220, 20);
jButton1.setText("Display Entered UniCode");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(120, 110, 160, 23);
getContentPane().add(jLabel2);
jLabel2.setBounds(120, 170, 150, 40);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
String uniCode = jTextField1.getText();
jLabel2.setText(uniCode);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new UnicodeExample().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
但是Unicode字符没有显示。仅当我从外部源获取值作为参数并将该值设置为Swing控件时才会发生这种情况。但是如果我像String unicode="\u00a5"
这个问题有什么问题吗?
答案 0 :(得分:1)
硬编码有效,因为在这种情况下\u00a5
被视为一个单一的unicode字符。
但是,从JTextField
获取后,如果您硬编码,则会获得相当于"\u00a5"
的STRING "\\u00a5"
。
更清楚一点,试试
char c = '\u00a5';
System.out.println(c);
编辑:
如果您只在输入框中插入unicode表示,这个小代码可能会有所帮助:
String uniCode = jTextField1.getText();
uniCode = uniCode.substring(2);
char c = (char) Integer.parseInt(uniCode, 16);
jLabel2.setText(c + "");
在actionPerformed
方法中执行此操作。