介绍一个功能,可以把汉语转化成拼音,包括全拼和首字母,甚至可以带音调。
大家可以去http://pinyin4j.sourceforge.net/ 看看,下载支持jar包:pinyin4j-2.5.0.jar。
我也是看的demo,大家可以把下面的代码运行看看。
1

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

56

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
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
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
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
156
private JPanel getUnformattedHanyuPinyinPanel()
157
{
158
if (unformattedHanyuPinyinPanel == null)
159
{<