随笔 - 187, 文章 - 3, 评论 - 414, 引用 - 0
数据加载中……

中文转换为全拼和首字母

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

大家可以去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        {
160            unformattedHanyuPinyinPanel = new JPanel();
161            unformattedHanyuPinyinPanel.setLayout(new BorderLayout()); // Generated
162            unformattedHanyuPinyinPanel.add(unformattedHanyuPinyinLabel, BorderLayout.NORTH); // Generated
163            unformattedHanyuPinyinPanel.add(getUnformattedHanyuPinyinScrollPane(), BorderLayout.CENTER); // Generated
164        }

165        return unformattedHanyuPinyinPanel;
166    }

167
168    /**
169     * This method initializes unformattedTongyongPinyinPanel
170     * 
171     * @return javax.swing.JPanel
172     */

173    private JPanel getUnformattedTongyongPinyinPanel()
174    {
175        if (unformattedTongyongPinyinPanel == null)
176        {
177            unformattedTongyongPinyinLabel = new JLabel();
178            unformattedTongyongPinyinLabel.setText("Tongyong Pinyin"); // Generated
179            unformattedTongyongPinyinPanel = new JPanel();
180            unformattedTongyongPinyinPanel.setLayout(new BorderLayout()); // Generated
181            unformattedTongyongPinyinPanel.add(unformattedTongyongPinyinLabel, java.awt.BorderLayout.NORTH); // Generated
182            unformattedTongyongPinyinPanel.add(getUnformattedTongyongPinyinScrollPane(), BorderLayout.CENTER); // Generated
183        }

184        return unformattedTongyongPinyinPanel;
185    }

186
187    /**
188     * This method initializes unformattedTongyongPinyinTextArea
189     * 
190     * @return javax.swing.JTextArea
191     */

192    private JTextArea getUnformattedTongyongPinyinTextArea()
193    {
194        if (unformattedTongyongPinyinTextArea == null)
195        {
196            unformattedTongyongPinyinTextArea = new JTextArea();
197            unformattedTongyongPinyinTextArea.setEditable(false); // Generated
198            unformattedTongyongPinyinTextArea.setLineWrap(true); // Generated
199        }

200        return unformattedTongyongPinyinTextArea;
201    }

202
203    /**
204     * This method initializes unformattedWadePinyinPanel
205     * 
206     * @return javax.swing.JPanel
207     */

208    private JPanel getUnformattedWadePinyinPanel()
209    {
210        if (unformattedWadePinyinPanel == null)
211        {
212            unformattedWadePinyinLabel = new JLabel();
213            unformattedWadePinyinLabel.setText("Wade-Giles  Pinyin"); // Generated
214            unformattedWadePinyinPanel = new JPanel();
215            unformattedWadePinyinPanel.setLayout(new BorderLayout()); // Generated
216            unformattedWadePinyinPanel.add(unformattedWadePinyinLabel, java.awt.BorderLayout.NORTH); // Generated
217            unformattedWadePinyinPanel.add(getUnformattedWadePinyinScrollPane(), BorderLayout.CENTER); // Generated
218        }

219        return unformattedWadePinyinPanel;
220    }

221
222    /**
223     * This method initializes unformattedWadePinyinTextArea
224     * 
225     * @return javax.swing.JTextArea
226     */

227    private JTextArea getUnformattedWadePinyinTextArea()
228    {
229        if (unformattedWadePinyinTextArea == null)
230        {
231            unformattedWadePinyinTextArea = new JTextArea();
232            unformattedWadePinyinTextArea.setEditable(false); // Generated
233            unformattedWadePinyinTextArea.setLineWrap(true); // Generated
234        }

235        return unformattedWadePinyinTextArea;
236    }

237
238    /**
239     * This method initializes unformattedMPS2PinyinPanel
240     * 
241     * @return javax.swing.JPanel
242     */

243    private JPanel getUnformattedMPS2PinyinPanel()
244    {
245        if (unformattedMPS2PinyinPanel == null)
246        {
247            unformattedMPS2PinyinLabel = new JLabel();
248            unformattedMPS2PinyinLabel.setText("MPSII Pinyin"); // Generated
249            unformattedMPS2PinyinPanel = new JPanel();
250            unformattedMPS2PinyinPanel.setLayout(new BorderLayout()); // Generated
251            unformattedMPS2PinyinPanel.add(unformattedMPS2PinyinLabel, java.awt.BorderLayout.NORTH); // Generated
252            unformattedMPS2PinyinPanel.add(getUnformattedMPS2PinyinScrollPane(), BorderLayout.CENTER); // Generated
253        }

254        return unformattedMPS2PinyinPanel;
255    }

256
257    /**
258     * This method initializes unformattedMPS2PinyinTextArea
259     * 
260     * @return javax.swing.JTextArea
261     */

262    private JTextArea getUnformattedMPS2PinyinTextArea()
263    {
264        if (unformattedMPS2PinyinTextArea == null)
265        {
266            unformattedMPS2PinyinTextArea = new JTextArea();
267            unformattedMPS2PinyinTextArea.setEditable(false); // Generated
268            unformattedMPS2PinyinTextArea.setLineWrap(true); // Generated
269        }

270        return unformattedMPS2PinyinTextArea;
271    }

272
273    /**
274     * This method initializes unformattedYalePinyinPanel
275     * 
276     * @return javax.swing.JPanel
277     */

278    private JPanel getUnformattedYalePinyinPanel()
279    {
280        if (unformattedYalePinyinPanel == null)
281        {
282            unformattedYalePinyinLabel = new JLabel();
283            unformattedYalePinyinLabel.setText("Yale Pinyin"); // Generated
284            unformattedYalePinyinPanel = new JPanel();
285            unformattedYalePinyinPanel.setLayout(new BorderLayout()); // Generated
286            unformattedYalePinyinPanel.add(unformattedYalePinyinLabel, java.awt.BorderLayout.NORTH); // Generated
287