介绍一个功能,可以把汉语转化成拼音,包括全拼和首字母,甚至可以带音调。
大家可以去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
19
package pinyin;
20
21
import java.awt.BorderLayout;
22
import java.awt.Dimension;
23
import java.awt.Font;
24
import java.awt.GridLayout;
25
import java.awt.event.WindowAdapter;
26
import java.awt.event.WindowEvent;
27
28
import javax.swing.JApplet;
29
import javax.swing.JButton;
30
import javax.swing.JComboBox;
31
import javax.swing.JFrame;
32
import javax.swing.JLabel;
33
import javax.swing.JPanel;
34
import javax.swing.JScrollPane;
35
import javax.swing.JTabbedPane;
36
import javax.swing.JTextArea;
37
import javax.swing.JTextField;
38
39
import net.sourceforge.pinyin4j.PinyinHelper;
40
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
41
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
42
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
43
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
44
import 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
*/
62
public class Pinyin4jAppletDemo extends JApplet
63

{
64
private static final Dimension APP_SIZE = new Dimension(600, 400);
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(26, 20)); // 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
{<

/** *//**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

19
package pinyin;20

21
import java.awt.BorderLayout;22
import java.awt.Dimension;23
import java.awt.Font;24
import java.awt.GridLayout;25
import java.awt.event.WindowAdapter;26
import java.awt.event.WindowEvent;27

28
import javax.swing.JApplet;29
import javax.swing.JButton;30
import javax.swing.JComboBox;31
import javax.swing.JFrame;32
import javax.swing.JLabel;33
import javax.swing.JPanel;34
import javax.swing.JScrollPane;35
import javax.swing.JTabbedPane;36
import javax.swing.JTextArea;37
import javax.swing.JTextField;38

39
import net.sourceforge.pinyin4j.PinyinHelper;40
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;41
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;42
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;43
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;44
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;45

46

/** *//**47
* This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI48
* Builder, which is free for non-commercial use. If Jigloo is being used49
* commercially (ie, by a corporation, company or business for any purpose50
* whatever) then you should purchase a license for each developer using Jigloo.51
* Please visit www.cloudgarden.com for details. Use of Jigloo implies52
* acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN53
* PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR54
* ANY CORPORATE OR COMMERCIAL PURPOSE.55
*/56

/** *//**57
* A demo show the functions of pinyin4j library58
* 59
* @author Li Min (xmlerlimin@gmail.com)60
* 61
*/62
public class Pinyin4jAppletDemo extends JApplet63


{64
private static final Dimension APP_SIZE = new Dimension(600, 400);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 charTextField92
* 93
* @return javax.swing.JTextField94
*/95
private JTextField getCharTextField()96

{97
if (charTextField == null)98

{99
charTextField = new JTextField();100
charTextField.setFont(new Font("Dialog", Font.PLAIN, 12)); // Generated101
charTextField.setText("和"); // Generated102
charTextField.setPreferredSize(new Dimension(26, 20)); // Generated103
}104
return charTextField;105
}106

107

/** *//**108
* This method initializes unformattedCharPanel109
* 110
* @return javax.swing.JPanel111
*/112
private JPanel getUnformattedCharPanel()113

{114
if (unformattedCharPanel == null)115

{116
unformattedHanyuPinyinLabel = new JLabel();117
unformattedHanyuPinyinLabel.setText("Hanyu Pinyin"); // Generated118
GridLayout gridLayout = new GridLayout();119
gridLayout.setRows(2); // Generated120
gridLayout.setHgap(1); // Generated121
gridLayout.setVgap(1); // Generated122
gridLayout.setColumns(3); // Generated123
unformattedCharPanel = new JPanel();124
unformattedCharPanel.setLayout(gridLayout); // Generated125
unformattedCharPanel.add(getUnformattedHanyuPinyinPanel(), null); // Generated126
unformattedCharPanel.add(getUnformattedTongyongPinyinPanel(), null); // Generated127
unformattedCharPanel.add(getUnformattedWadePinyinPanel(), null); // Generated128
unformattedCharPanel.add(getUnformattedMPS2PinyinPanel(), null); // Generated129
unformattedCharPanel.add(getUnformattedYalePinyinPanel(), null); // Generated130
unformattedCharPanel.add(getUnformattedGwoyeuRomatzyhPanel(), null); // Generated131
}132
return unformattedCharPanel;133
}134

135

/** *//**136
* This method initializes unformattedHanyuPinyinTextArea137
* 138
* @return javax.swing.JTextArea139
*/140
private JTextArea getUnformattedHanyuPinyinTextArea()141

{142
if (unformattedHanyuPinyinTextArea == null)143

{144
unformattedHanyuPinyinTextArea = new JTextArea();145
unformattedHanyuPinyinTextArea.setEditable(false); // Generated146
unformattedHanyuPinyinTextArea.setLineWrap(true); // Generated147
}148
return unformattedHanyuPinyinTextArea;149
}150

151

/** *//**152
* This method initializes unformattedHanyuPinyinPanel153
* 154
* @return javax.swing.JPanel155
*/156
private JPanel getUnformattedHanyuPinyinPanel()157

{158
if (unformattedHanyuPinyinPanel == null)159

{<
2735

被折叠的 条评论
为什么被折叠?



