`
sodabao
  • 浏览: 159647 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

中文转换为全拼和首字母

阅读更多

介绍一个功能,可以把汉语转化成拼音,包括全拼和首字母,甚至可以带音调。

大家可以去http://pinyin4j.sourceforge.net/ 看看,下载支持jar包:pinyin4j-2.5.0.jar。

我也是看的demo,大家可以把下面的代码运行看看。

  1/** *//**
  2 * This file is part of pinyin4j (http://sourceforge.net/projects/pinyin4j/
  3 * and distributed under GNU GENERAL PUBLIC LICENSE (GPL).
  4 * 
  5 * pinyin4j is free software; you can redistribute it and/or modify 
  6 * it under the terms of the GNU General Public License as published by 
  7 * the Free Software Foundation; either version 2 of the License, or 
  8 * (at your option) any later version. 
  9 * 
 10 * pinyin4j is distributed in the hope that it will be useful, 
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13 * GNU General Public License for more details. 
 14 * 
 15 * You should have received a copy of the GNU General Public License 
 16 * along with pinyin4j.
 17 */

 18
 19package pinyin;
 20
 21import java.awt.BorderLayout;
 22import java.awt.Dimension;
 23import java.awt.Font;
 24import java.awt.GridLayout;
 25import java.awt.event.WindowAdapter;
 26import java.awt.event.WindowEvent;
 27
 28import javax.swing.JApplet;
 29import javax.swing.JButton;
 30import javax.swing.JComboBox;
 31import javax.swing.JFrame;
 32import javax.swing.JLabel;
 33import javax.swing.JPanel;
 34import javax.swing.JScrollPane;
 35import javax.swing.JTabbedPane;
 36import javax.swing.JTextArea;
 37import javax.swing.JTextField;
 38
 39import net.sourceforge.pinyin4j.PinyinHelper;
 40import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
 41import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
 42import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
 43import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
 44import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
 45
 46/** *//**
 47 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 48 * Builder, which is free for non-commercial use. If Jigloo is being used
 49 * commercially (ie, by a corporation, company or business for any purpose
 50 * whatever) then you should purchase a license for each developer using Jigloo.
 51 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 52 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 53 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 54 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 55 */

 56/** *//**
 57 * A demo show the functions of pinyin4j library
 58 * 
 59 * @author Li Min (xmlerlimin@gmail.com)
 60 * 
 61 */

 62public class Pinyin4jAppletDemo extends JApplet
 63{
 64    private static final Dimension APP_SIZE = new Dimension(600400);
 65
 66    private static final long serialVersionUID = -1934962385592030162L;
 67
 68    private JPanel jContentPane = null;
 69
 70    private JTabbedPane jTabbedPane = null;
 71
 72    private JPanel formattedCharPanel = null;
 73
 74    private JPanel optionPanel = null;
 75
 76    private JButton convertButton = null;
 77
 78    private JPanel buttonPanel = null;
 79
 80    private JTextArea formattedOutputField = null;
 81
 82    private JComboBox toneTypesComboBox = null;
 83
 84    private JComboBox vCharTypesComboBox = null;
 85
 86    private JComboBox caseTypesComboBox = null;
 87
 88    private static String appName = "pinyin4j-2.0.0 applet demo";
 89
 90    /** *//**
 91     * This method initializes charTextField
 92     * 
 93     * @return javax.swing.JTextField
 94     */

 95    private JTextField getCharTextField()
 96    {
 97        if (charTextField == null)
 98        {
 99            charTextField = new JTextField();
100            charTextField.setFont(new Font("Dialog", Font.PLAIN, 12)); // Generated
101            charTextField.setText(""); // Generated
102            charTextField.setPreferredSize(new Dimension(2620)); // Generated
103        }

104        return charTextField;
105    }

106
107    /** *//**
108     * This method initializes unformattedCharPanel
109     * 
110     * @return javax.swing.JPanel
111     */

112    private JPanel getUnformattedCharPanel()
113    {
114        if (unformattedCharPanel == null)
115        {
116            unformattedHanyuPinyinLabel = new JLabel();
117            unformattedHanyuPinyinLabel.setText("Hanyu Pinyin"); // Generated
118            GridLayout gridLayout = new GridLayout();
119            gridLayout.setRows(2); // Generated
120            gridLayout.setHgap(1); // Generated
121            gridLayout.setVgap(1); // Generated
122            gridLayout.setColumns(3); // Generated
123            unformattedCharPanel = new JPanel();
124            unformattedCharPanel.setLayout(gridLayout); // Generated
125            unformattedCharPanel.add(getUnformattedHanyuPinyinPanel(), null); // Generated
126            unformattedCharPanel.add(getUnformattedTongyongPinyinPanel(), null); // Generated
127            unformattedCharPanel.add(getUnformattedWadePinyinPanel(), null); // Generated
128            unformattedCharPanel.add(getUnformattedMPS2PinyinPanel(), null); // Generated
129            unformattedCharPanel.add(getUnformattedYalePinyinPanel(), null); // Generated
130            unformattedCharPanel.add(getUnformattedGwoyeuRomatzyhPanel(), null); // Generated
131        }

132        return unformattedCharPanel;
133    }

134
135    /** *//**
136     * This method initializes unformattedHanyuPinyinTextArea
137     * 
138     * @return javax.swing.JTextArea
139     */

140    private JTextArea getUnformattedHanyuPinyinTextArea()
141    {
142        if (unformattedHanyuPinyinTextArea == null)
143        {
144            unformattedHanyuPinyinTextArea = new JTextArea();
145            unformattedHanyuPinyinTextArea.setEditable(false); // Generated
146            unformattedHanyuPinyinTextArea.setLineWrap(true); // Generated
147        }

148        return unformattedHanyuPinyinTextArea;
149    }

150
151    /** *//**
152     * This method initializes unformattedHanyuPinyinPanel
153     * 
154     * @return javax.swing.JPanel
155     */

156    private JPanel getUnformattedHanyuPinyinPanel()
157    {
158        if (unformattedHanyuPinyinPanel == null)
159        {<
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics