JAVA编程:实现汉字到拼音的转换示例代码参考

来源:不详 责任编辑:栏目编辑 发表时间:2013-07-01 15:11 点击:

GUI代码部分:
/**
* @(#)CnToSpellGUI.java
* kindani
* 2004-10-25??
* */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
* 


* 

JDK版本

1.4

* @author KIN
* @version 1.0
* @see
* @since 1.0
*/
public class CnToSpell2GUI extends JFrame {

private CnToSpell2GUI c = null;

public CnToSpell2GUI () {
super("Cn to Spell");
setSize(800,100);
getContentPane().setLayout(new FlowLayout());
// component layout
JTextArea from = new JTextArea(5,20);
JTextArea to = new JTextArea(5,20);
JButton b = new JButton("cn to pinyin");
getContentPane().add(new JLabel("From:"));
getContentPane().add(from);
getContentPane().add(b);
getContentPane().add(new JLabel("To:"));
getContentPane().add(to);
// action handle
b.addActionListener(new Cn2PinyinActionListener(from,to));
setVisible(true);
// set this for pack
c = this;
}

/**button action listener to convert text to pinyin from one textbox to another textbox*/
class Cn2PinyinActionListener implements ActionListener{

private JTextArea from = null;
private JTextArea to = null;
public Cn2PinyinActionListener(JTextArea from, JTextArea to) {
this.from = from;
this.to = to;
}
public void actionPerformed(ActionEvent e) {
if (from.getText().length() == 0) {
JOptionPane.showMessageDialog(from,"From text is empty!","Warning",JOptionPane.WARNING_MESSAGE);
}
String text = from.getText();
to.setText(CnToSpell.getFullSpell(text));
c.pack();
}
}

public static void main(String [] args) {
CnToSpell2GUI g = new CnToSpell2GUI();
}
}



------------------------------------------------------------------------
/**
* @(#)CnToSpell.java 
* 版权声明 Easydozer 版权所有 违者必究 
*
* 修订记录:
* 1)更改者:Easydozer
* 时 间:2004-10-20 
* 描 述:创建
*/
package com.easydozer.commons.util;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;

/**
* 
汉字转化为全拼
* 

JDK版本:

1.4

* @author 谢计生
* @version 1.0
* @see 
* @since 1.0
*/
public class CnToSpell
{
private static LinkedHashMap spellMap = null;

static
{
if(spellMap == null){
spellMap = new LinkedHashMap(400);
}
initialize();
System.out.println("Chinese transfer Spell Done.");
}

private CnToSpell()
{
}

private static void spellPut(String spell,int ascii)
{
spellMap.put(spell,new Integer(ascii));
}

private static void initialize()
{
spellPut("a", -20319);
spellPut("ai", -20317);
spellPut("an", -20304);
spellPut("ang", -20295);
spellPut("ao", -20292);
spellPut("ba", -20283);
spellPut("bai", -20265);
spellPut("ban", -20257);
spellPut("bang", -20242);
spellPut("bao", -20230);
spellPut("bei", -20051);
spellPut("ben", -20036);
spellPut("beng", -20032);
spellPut("bi", -20026);
spellPut("bian", -20002);
spellPut("biao", -19990);
spellPut("bie", -19986);
spellPut("bin", -19982);
spellPut("bing", -19976);
spellPut("bo", -19805);
spellPut("bu", -19784);
spellPut("ca", -19775);
spellPut("cai", -19774);
spellPut("can", -19763);
spellPut("cang", -19756);
spellPut("cao", -19751);
spellPut("ce", -19746);
spellPut("ceng", -19741);
spellPut("cha", -19739);
spellPut("chai", -19728);
spellPut("chan", -19725);
spellPut("chang", -19715);
spellPut("chao", -19540);
spellPut("che", -19531);
spellPut("chen", -19525);
spellPut("cheng", -19515);
spellPut("chi", -19500);
spellPut("chong", -19484);
spellPut("chou", -19479);
spellPut("chu", -19467);
spellPut("chuai", -19289);
spellPut("chuan", -19288);
spellPut("chuang", -19281);
spellPut("chui", -19275);
spellPut("chun", -19270);
spellPut("chuo", -19263);
spellPut("ci", -19261);
spellPut("cong", -19249);
spellPut("cou", -19243);
spellPut("cu", -19242);
spellPut("cuan", -19238);
spellPut("cui", -19235);
spellPut("cun", -19227);
spellPut("cuo", -19224);
spellPut("da", -19218);
spellPut("dai", -19212);
spellPut("dan", -19038);
spellPut("dang", -19023);
spellPut("dao", -19018);
spellPut("de", -19006);
spellPut("deng", -19003);
spellPut("di
	
    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • Java编程语言的八大优点
    • JVM对象生命周期详细介绍
    • Java平台上的CRM系统
    • Java 算数测试小程序
    • Command(命令模式)
    • Java 一个简单的画图程序
    • Java环境 使用Resin在NT环境下配置JSP环境
    • Java 日历的小程序
    • Java 统计代码的小工具
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1