怎样讲string转换成unicode,请高手来 Java SE
2个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UCodeDemo extends JFrame {
private static final long serialVersionUID = 1L;
static JTextField jt1;
static JTextArea jt2;
JScrollPane jsp;
JButton jb;
public UCodeDemo() {
jt1 = new JTextField(20);
jt1.setText("请输入....");
//给单行文本框添加事件监听,当监听到键盘的回车键的时候,显示字符在Unicode的位置
jt1.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER){
getUcode();
}
}
});
jt2 = new JTextArea();
jb = new JButton("确定");
jsp = new JScrollPane(jt2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//给按钮设置事件监听,当点击按钮的时候,显示字符在Unicode的位置
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getUcode();
}
});
this.setLayout(new BorderLayout());
this.add(jt1, BorderLayout.NORTH);
this.add(jb, BorderLayout.SOUTH);
this.add(jsp, BorderLayout.CENTER);
this.setTitle("Unicode小应用");
this.setLocation(350, 250);
this.setSize(300, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
//方法:显示字符在Unicode的位置
protected void getUcode() {
String str = jt1.getText();
char[] cc = str.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < cc.length; i++) {
sb.append("【" + cc[i] + "的Unicode位置】:" + (int) cc[i] + "\n");
}
jt2.setText(sb.toString());
jt2.setForeground(getColor());
}
public static void main(String[] args) {
new UCodeDemo();
}
private Color getColor(){
return new Color(getRandom(),getRandom(),getRandom());
}
int getRandom(){
int rgb=(int)(Math.random()*256);
return rgb;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询