介绍一个功能,可以把汉语转化成拼音,包括全拼和首字母,甚至可以带音调。
大家可以去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

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46


47

48

49

50

51

52

53

54

55

56


57

58

59

60

61

62

63



64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90


91

92

93

94

95

96



97

98



99

100

101

102

103

104

105

106

107


108

109

110

111

112

113



114

115



116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135


136

137

138

139

140

141



142

143



144

145

146

147

148

149

150

151


152

153

154

155

156

157



158

159


