900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > java把汉字转换成拼音_Java实现汉字转换为拼音

java把汉字转换成拼音_Java实现汉字转换为拼音

时间:2023-05-29 03:36:52

相关推荐

java把汉字转换成拼音_Java实现汉字转换为拼音

本文的核心代码取自easydozer的blog:/easydozer/

代码说明:

Java实现汉字转换为拼音的GUI版本。

GUI代码部分:

/**

* @(#)CnToSpellGUI.java

* */

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

/**

*

*

JDK版本

1.4

* @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();

}

}

核心代码部分:

* 描述:创建

*/

package mons.util;

import java.util.Iterator;

import java.util.LinkedHashMap;

import java.util.Set;

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);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。