原本的代码中所有的中文都变成了\u4E00\u51FB\u5FC5\u6740这种格式,网上查了说这是CJK码,请问怎么把他转换为中文?
我写了一段代码,虽然能输出,但是结果不能转换,而用系统的System.out.println("\u4E00\u51FB\u5FC5\u6740")直接输出就能显示正常结果.如果能直接使用JavaScript的document.write方法也可以,这个方法能解决.
Application1.javapackage cjk_convert;import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2009</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Application1
{
boolean packFrame = false; /**
* Construct and show the application.
*/
public Application1()
{
Frame1 frame = new Frame1();
// Validate frames that have preset sizes
// Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame)
{
frame.pack();
} else
{
frame.validate();
} // Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
} /**
* Application entry point.
*
* @param args String[]
*/
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} catch (Exception exception)
{
exception.printStackTrace();
} new Application1();
}
});
}
}Frame1.java
package cjk_convert;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.*;
import java.io.*;
public class Frame1 extends JFrame implements ActionListener,KeyListener
{
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JScrollPane jScrollPane1 = new JScrollPane();
JTextPane jTextPane1 = new JTextPane();
JLabel jLabel2 = new JLabel();
JScrollPane jScrollPane2 = new JScrollPane();
JTextPane jTextPane2 = new JTextPane();
JCheckBox jCheckBox1 = new JCheckBox();
JButton jButton1 = new JButton();
private boolean m_fAutoTrans = false ; public Frame1()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} private void jbInit()
{
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 320));
setTitle("CJK编码转换(\\u打头的编码)");
jLabel1.setText("请在下面输入需要转换的内容");
jLabel1.setBounds(new Rectangle(18, 12, 357, 20));
jScrollPane1.setBounds(new Rectangle(13, 31, 366, 93));
jLabel2.setText("转换结果输出");
jLabel2.setBounds(new Rectangle(14, 163, 363, 19));
jScrollPane2.setBounds(new Rectangle(13, 181, 366, 105));
jCheckBox1.setText("自动转换");
jCheckBox1.setBounds(new Rectangle(123, 127, 78, 23));
jCheckBox1.addActionListener(this);
jButton1.setBounds(new Rectangle(15, 127, 100, 23));
jButton1.setText("转换");
jButton1.addActionListener(this);
contentPane.add(jLabel1);
contentPane.add(jScrollPane1);
contentPane.add(jLabel2);
contentPane.add(jScrollPane2);
contentPane.add(jButton1);
contentPane.add(jCheckBox1);
jScrollPane2.getViewport().add(jTextPane2);
jScrollPane1.getViewport().add(jTextPane1);
jScrollPane1.addKeyListener(this);
} public void actionPerformed(ActionEvent e)
{
if (e.getSource().equals(jCheckBox1))
{
m_fAutoTrans = jCheckBox1.isSelected() ;
} else
if (e.getSource().equals(jButton1))
{
convert() ;
}
} public void keyTyped(KeyEvent e)
{
} public void keyPressed(KeyEvent e)
{
if (e.isControlDown())
if (e.getKeyCode() == KeyEvent.VK_V)
if (m_fAutoTrans)
convert() ;
} public void keyReleased(KeyEvent e)
{
}
public void convert()
{
String text = jTextPane1.getText() ;
jTextPane2.setText("");
try
{
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream(pis);
PrintStream ps = new PrintStream(pos) ;
System.setOut(ps);
System.out.println(text);
//将文字流写入到剪贴板
byte[] b = new byte[pis.available()] ;
pis.read(b) ;
jTextPane2.setText(new String(b));
} catch (IOException ex)
{
}
}
}
我写了一段代码,虽然能输出,但是结果不能转换,而用系统的System.out.println("\u4E00\u51FB\u5FC5\u6740")直接输出就能显示正常结果.如果能直接使用JavaScript的document.write方法也可以,这个方法能解决.
Application1.javapackage cjk_convert;import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2009</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Application1
{
boolean packFrame = false; /**
* Construct and show the application.
*/
public Application1()
{
Frame1 frame = new Frame1();
// Validate frames that have preset sizes
// Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame)
{
frame.pack();
} else
{
frame.validate();
} // Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
} /**
* Application entry point.
*
* @param args String[]
*/
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} catch (Exception exception)
{
exception.printStackTrace();
} new Application1();
}
});
}
}Frame1.java
package cjk_convert;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.*;
import java.io.*;
public class Frame1 extends JFrame implements ActionListener,KeyListener
{
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JScrollPane jScrollPane1 = new JScrollPane();
JTextPane jTextPane1 = new JTextPane();
JLabel jLabel2 = new JLabel();
JScrollPane jScrollPane2 = new JScrollPane();
JTextPane jTextPane2 = new JTextPane();
JCheckBox jCheckBox1 = new JCheckBox();
JButton jButton1 = new JButton();
private boolean m_fAutoTrans = false ; public Frame1()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} private void jbInit()
{
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 320));
setTitle("CJK编码转换(\\u打头的编码)");
jLabel1.setText("请在下面输入需要转换的内容");
jLabel1.setBounds(new Rectangle(18, 12, 357, 20));
jScrollPane1.setBounds(new Rectangle(13, 31, 366, 93));
jLabel2.setText("转换结果输出");
jLabel2.setBounds(new Rectangle(14, 163, 363, 19));
jScrollPane2.setBounds(new Rectangle(13, 181, 366, 105));
jCheckBox1.setText("自动转换");
jCheckBox1.setBounds(new Rectangle(123, 127, 78, 23));
jCheckBox1.addActionListener(this);
jButton1.setBounds(new Rectangle(15, 127, 100, 23));
jButton1.setText("转换");
jButton1.addActionListener(this);
contentPane.add(jLabel1);
contentPane.add(jScrollPane1);
contentPane.add(jLabel2);
contentPane.add(jScrollPane2);
contentPane.add(jButton1);
contentPane.add(jCheckBox1);
jScrollPane2.getViewport().add(jTextPane2);
jScrollPane1.getViewport().add(jTextPane1);
jScrollPane1.addKeyListener(this);
} public void actionPerformed(ActionEvent e)
{
if (e.getSource().equals(jCheckBox1))
{
m_fAutoTrans = jCheckBox1.isSelected() ;
} else
if (e.getSource().equals(jButton1))
{
convert() ;
}
} public void keyTyped(KeyEvent e)
{
} public void keyPressed(KeyEvent e)
{
if (e.isControlDown())
if (e.getKeyCode() == KeyEvent.VK_V)
if (m_fAutoTrans)
convert() ;
} public void keyReleased(KeyEvent e)
{
}
public void convert()
{
String text = jTextPane1.getText() ;
jTextPane2.setText("");
try
{
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream(pis);
PrintStream ps = new PrintStream(pos) ;
System.setOut(ps);
System.out.println(text);
//将文字流写入到剪贴板
byte[] b = new byte[pis.available()] ;
pis.read(b) ;
jTextPane2.setText(new String(b));
} catch (IOException ex)
{
}
}
}
这不是反编译的,是我自己写的.我的意思是用系统的System.out.println可以正常的显示中文,重定向out以后就不行了.
我的电脑因为针对特定平台开发,所以只能装1.4的JDK
如果如我所说,你可能是源代码文件的字符集设置错了,或者编辑的时候有编译选项配置错了。
反正是在编译的时候,源文件的代码和编译器期望的代码不一致。
而不管什么编码java对转义字符理解都是一样的,所以转义字符不乱。lz找找配置吧
是JBuilder,不过语言配置中居然没有能让他正常显示的配置...汗