中文转换为全拼和首字母
介绍一个功能,可以把汉语转化成拼音,包括全拼和首字母,甚至可以带音调。
大家可以去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 ([email protected])
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 {
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 unformattedYalePinyinPanel.add(getUnformattedYalePinyinScrollPane(), BorderLayout.CENTER); // Generated
288 }
289 return unformattedYalePinyinPanel;
290 }
291
292 /** *//**
293 * This method initializes unformattedYalePinyinTextArea
294 *
295 * @return javax.swing.JTextArea
296 */
297 private JTextArea getUnformattedYalePinyinTextArea()
298 {
299 if (unformattedYalePinyinTextArea == null)
300 {
301 unformattedYalePinyinTextArea = new JTextArea();
302 unformattedYalePinyinTextArea.setEditable(false); // Generated
303 unformattedYalePinyinTextArea.setLineWrap(true); // Generated
304 }
305 return unformattedYalePinyinTextArea;
306 }
307
308 /** *//**
309 * This method initializes unformattedGwoyeuRomatzyhPanel
310 *
311 * @return javax.swing.JPanel
312 */
313 private JPanel getUnformattedGwoyeuRomatzyhPanel()
314 {
315 if (unformattedGwoyeuRomatzyhPanel == null)
316 {
317 unformattedGwoyeuRomatzyhLabel = new JLabel();
318 unformattedGwoyeuRomatzyhLabel.setText("Gwoyeu Romatzyh"); // Generated
319 unformattedGwoyeuRomatzyhPanel = new JPanel();
320 unformattedGwoyeuRomatzyhPanel.setLayout(new BorderLayout()); // Generated
321 unformattedGwoyeuRomatzyhPanel.add(unformattedGwoyeuRomatzyhLabel, java.awt.BorderLayout.NORTH); // Generated
322 unformattedGwoyeuRomatzyhPanel.add(getUnformattedGwoyeuRomatzyhScrollPane(), BorderLayout.CENTER); // Generated
323 }
324 return unformattedGwoyeuRomatzyhPanel;
325 }
326
327 /** *//**
328 * This method initializes unformattedGwoyeuRomatzyhTextArea
329 *
330 * @return javax.swing.JTextArea
331 */
332 private JTextArea getUnformattedGwoyeuRomatzyhTextArea()
333 {
334 if (unformattedGwoyeuRomatzyhTextArea == null)
335 {
336 unformattedGwoyeuRomatzyhTextArea = new JTextArea();
337 unformattedGwoyeuRomatzyhTextArea.setEditable(false); // Generated
338 unformattedGwoyeuRomatzyhTextArea.setLineWrap(true); // Generated
339 }
340 return unformattedGwoyeuRomatzyhTextArea;
341 }
342
343 /** *//**
344 * This method initializes unformattedMPS2PinyinScrollPane
345 *
346 * @return javax.swing.JScrollPane
347 */
348 private JScrollPane getUnformattedMPS2PinyinScrollPane()
349 {
350 if (unformattedMPS2PinyinScrollPane == null)
351 {
352 unformattedMPS2PinyinScrollPane = new JScrollPane();
353 unformattedMPS2PinyinScrollPane.setViewportView(getUnformattedMPS2PinyinTextArea()); // Generated
354 }
355 return unformattedMPS2PinyinScrollPane;
356 }
357
358 /** *//**
359 * This method initializes unformattedHanyuPinyinScrollPane
360 *
361 * @return javax.swing.JScrollPane
362 */
363 private JScrollPane getUnformattedHanyuPinyinScrollPane()
364 {
365 if (unformattedHanyuPinyinScrollPane == null)
366 {
367 unformattedHanyuPinyinScrollPane = new JScrollPane();
368 unformattedHanyuPinyinScrollPane.setViewportView(getUnformattedHanyuPinyinTextArea()); // Generated
369 }
370 return unformattedHanyuPinyinScrollPane;
371 }
372
373 /** *//**
374 * This method initializes unformattedTongyongPinyinScrollPane
375 *
376 * @return javax.swing.JScrollPane
377 */
378 private JScrollPane getUnformattedTongyongPinyinScrollPane()
379 {
380 if (unformattedTongyongPinyinScrollPane == null)
381 {
382 unformattedTongyongPinyinScrollPane = new JScrollPane();
383 unformattedTongyongPinyinScrollPane.setViewportView(getUnformattedTongyongPinyinTextArea()); // Generated
384 }
385 return unformattedTongyongPinyinScrollPane;
386 }
387
388 /** *//**
389 * This method initializes unformattedWadePinyinScrollPane
390 *
391 * @return javax.swing.JScrollPane
392 */
393 private JScrollPane getUnformattedWadePinyinScrollPane()
394 {
395 if (unformattedWadePinyinScrollPane == null)
396 {
397 unformattedWadePinyinScrollPane = new JScrollPane();
398 unformattedWadePinyinScrollPane.setViewportView(getUnformattedWadePinyinTextArea()); // Generated
399 }
400 return unformattedWadePinyinScrollPane;
401 }
402
403 /** *//**
404 * This method initializes unformattedYalePinyinScrollPane
405 *
406 * @return javax.swing.JScrollPane
407 */
408 private JScrollPane getUnformattedYalePinyinScrollPane()
409 {
410 if (unformattedYalePinyinScrollPane == null)
411 {
412 unformattedYalePinyinScrollPane = new JScrollPane();
413 unformattedYalePinyinScrollPane.setViewportView(getUnformattedYalePinyinTextArea()); // Generated
414 }
415 return unformattedYalePinyinScrollPane;
416 }
417
418 /** *//**
419 * This method initializes unformattedGwoyeuRomatzyhScrollPane
420 *
421 * @return javax.swing.JScrollPane
422 */
423 private JScrollPane getUnformattedGwoyeuRomatzyhScrollPane()
424 {
425 if (unformattedGwoyeuRomatzyhScrollPane == null)
426 {
427 unformattedGwoyeuRomatzyhScrollPane = new JScrollPane();
428 unformattedGwoyeuRomatzyhScrollPane.setViewportView(getUnformattedGwoyeuRomatzyhTextArea()); // Generated
429 }
430 return unformattedGwoyeuRomatzyhScrollPane;
431 }
432
433 static public void main(String argv[])
434 {
435 final Pinyin4jAppletDemo appletDemo = new Pinyin4jAppletDemo();
436
437 System.runFinalizersOnExit(true);
438
439 JFrame jFrame = new JFrame(appName);
440 jFrame.addWindowListener(new WindowAdapter()
441 {
442 public void windowClosing(WindowEvent event)
443 {
444 appletDemo.stop();
445 appletDemo.destroy();
446 System.exit(0);
447 }
448 });
449 jFrame.add("Center", appletDemo);
450
451 appletDemo.init();
452 appletDemo.start();
453
454 jFrame.setSize(APP_SIZE);
455 jFrame.pack();
456 jFrame.setVisible(true);
457 }
458
459 /** *//**
460 * This is the default constructor
461 */
462 public Pinyin4jAppletDemo()
463 {
464 super();
465 init();
466 }
467
468 /** *//**
469 * This method initializes this
470 *
471 * @return void
472 */
473 public void init()
474 {
475 this.setSize(APP_SIZE);
476 this.setContentPane(getJContentPane());
477 this.setName(appName);
478 }
479
480 /** *//**
481 * This method initializes jContentPane
482 *
483 * @return javax.swing.JPanel
484 */
485 private JPanel getJContentPane()
486 {
487 if (jContentPane == null)
488 {
489 jContentPane = new JPanel();
490 jContentPane.setLayout(new BorderLayout());
491 jContentPane.add(getJTabbedPane(), java.awt.BorderLayout.CENTER); // Generated
492 jContentPane.add(getOptionPanel(), java.awt.BorderLayout.NORTH); // Generated
493 jContentPane.add(getButtonPanel(), java.awt.BorderLayout.SOUTH); // Generated
494 }
495 return jContentPane;
496 }
497
498 /** *//**
499 * This method initializes jTabbedPane
500 *
501 * @return javax.swing.JTabbedPane
502 */
503 private JTabbedPane getJTabbedPane()
504 {
505 if (jTabbedPane == null)
506 {
507 jTabbedPane = new JTabbedPane();
508 jTabbedPane.addTab("Unformatted Chinese Romanization Systems", null, getUnformattedCharPanel(), null); // Generated
509 jTabbedPane.addTab("Formatted Hanyu Pinyin", null, getFormattedCharPanel(), null); // Generated
510 }
511 return jTabbedPane;
512 }
513
514 /** *//**
515 * This method initializes jPanel3
516 *
517 * @return javax.swing.JPanel
518 */
519 private JPanel getFormattedCharPanel()
520 {
521 if (formattedCharPanel == null)
522 {
523 formattedCharPanel = new JPanel();
524 formattedCharPanel.setLayout(new BorderLayout());
525 formattedCharPanel.add(getFormattedOutputField(), java.awt.BorderLayout.CENTER); // Generated
526 }
527 return formattedCharPanel;
528 }
529
530 /** *//**
531 * This method initializes jPanel
532 *
533 * @return javax.swing.JPanel
534 */
535 private JPanel getOptionPanel()
536 {
537 if (optionPanel == null)
538 {
539 charLabel = new JLabel();
540 charLabel.setText("Input Chinese:"); // Generated
541
542 toneLabel = new JLabel();
543 toneLabel.setText(" Format:"); // Generated
544
545 optionPanel = new JPanel();
546 optionPanel.setPreferredSize(new java.awt.Dimension(640, 34)); // Generated
547 optionPanel.add(charLabel, null); // Generated
548 optionPanel.add(getCharTextField(), null); // Generated
549 optionPanel.add(toneLabel, null); // Generated
550 optionPanel.add(getToneTypesComboBox(), null); // Generated
551 optionPanel.add(getVCharTypesComboBox(), null); // Generated
552 optionPanel.add(getCaseTypesComboBox(), null); // Generated
553 }
554 return optionPanel;
555 }
556
557 /** *//**
558 * This method initializes jButton
559 *
560 * @return javax.swing.JButton
561 */
562 private JButton getConvertButton()
563 {
564 if (convertButton == null)
565 {
566 convertButton = new JButton();
567 convertButton.setText("Convert to Pinyin"); // Generated
568 convertButton.addActionListener(new java.awt.event.ActionListener()
569 {
570 public void actionPerformed(java.awt.event.ActionEvent e)
571 {
572 char chineseCharacter = getChineseCharText().charAt(0);
573
574 updateUnformattedTextField(chineseCharacter);
575
576 String toneSelection = (String) toneTypesComboBox.getSelectedItem();
577 String vcharSelection = (String) vCharTypesComboBox.getSelectedItem();
578 String caseSelection = (String) caseTypesComboBox.getSelectedItem();
579
580 updateFormattedTextField(chineseCharacter, toneSelection, vcharSelection, caseSelection);
581 }
582
583 /** *//**
584 * @param chineseCharacter
585 */
586 private void updateUnformattedTextField(char chineseCharacter)
587 {
588 unformattedHanyuPinyinTextArea.setText(concatPinyinStringArray(PinyinHelper.toHanyuPinyinStringArray(chineseCharacter)));
589 unformattedTongyongPinyinTextArea.setText(concatPinyinStringArray(PinyinHelper.toTongyongPinyinStringArray(chineseCharacter)));
590 unformattedWadePinyinTextArea.setText(concatPinyinStringArray(PinyinHelper.toWadeGilesPinyinStringArray(chineseCharacter)));
591 unformattedMPS2PinyinTextArea.setText(concatPinyinStringArray(PinyinHelper.toMPS2PinyinStringArray(chineseCharacter)));
592 unformattedYalePinyinTextArea.setText(concatPinyinStringArray(PinyinHelper.toYalePinyinStringArray(chineseCharacter)));
593 unformattedGwoyeuRomatzyhTextArea.setText(concatPinyinStringArray(PinyinHelper.toGwoyeuRomatzyhStringArray(chineseCharacter)));
594 }
595
596 /** *//**
597 * @param chineseCharacter
598 * @param toneSelection
599 * @param vcharSelection
600 * @param caseSelection
601 */
602 private void updateFormattedTextField(char chineseCharacter, String toneSelection, String vcharSelection, String caseSelection)
603 {
604 HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
605
606 if (toneTypes[0] == toneSelection)
607 {
608 outputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);
609 }
610 else if (toneTypes[1] == toneSelection)
611 {
612 outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
613 }
614 else if (toneTypes[2] == toneSelection)
615 {
616 outputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
617 }
618
619 if (vCharTypes[0] == vcharSelection)
620 {
621 outputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);
622 }
623 else if (vCharTypes[1] == vcharSelection)
624 {
625 outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
626 }
627 else if (vCharTypes[2] == vcharSelection)
628 {
629 outputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
630 }
631
632 if (caseTypes[0] == caseSelection)
633 {
634 outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
635 }
636 else if (caseTypes[1] == caseSelection)
637 {
638 outputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
639 }
640
641 String[] pinyinArray = null;
642 try
643 {
644 pinyinArray = PinyinHelper.toHanyuPinyinStringArray(chineseCharacter, outputFormat);
645 }
646 catch (BadHanyuPinyinOutputFormatCombination e1)
647 {
648 e1.printStackTrace();
649 }
650 String outputString = concatPinyinStringArray(pinyinArray);
651 formattedOutputField.setText(outputString);
652 }
653
654 /** *//**
655 * @param pinyinArray
656 * @return
657 */
658 private String concatPinyinStringArray(String[] pinyinArray)
659 {
660 StringBuffer pinyinStrBuf = new StringBuffer();
661
662 if ((null != pinyinArray) && (pinyinArray.length > 0))
663 {
664 for (int i = 0; i < pinyinArray.length; i++)
665 {
666 pinyinStrBuf.append(pinyinArray[i]);
667 pinyinStrBuf.append(System.getProperty("line.separator"));
668 }
669 }
670 String outputString = pinyinStrBuf.toString();
671 return outputString;
672 }
673 });
674
675 }
676 return convertButton;
677 }
678
679 /** *//**
680 * This method initializes jPanel2
681 *
682 * @return javax.swing.JPanel
683 */
684 private JPanel getButtonPanel()
685 {
686 if (buttonPanel == null)
687 {
688 buttonPanel = new JPanel();
689 buttonPanel.add(getConvertButton(), null); // Generated
690 }
691 return buttonPanel;
692 }
693
694 /** *//**
695 * This method initializes jTextArea1
696 *
697 * @return javax.swing.JTextArea
698 */
699 private JTextArea getFormattedOutputField()
700 {
701 if (formattedOutputField == null)
702 {
703 formattedOutputField = new JTextArea();
704 formattedOutputField.setEditable(false); // Generated
705 }
706 return formattedOutputField;
707 }
708
709 /** *//**
710 * This method initializes jComboBox
711 *
712 * @return javax.swing.JComboBox
713 */
714 private JComboBox getToneTypesComboBox()
715 {
716 if (toneTypesComboBox == null)
717 {
718 toneTypesComboBox = new JComboBox(toneTypes);
719 toneTypesComboBox.addActionListener(new java.awt.event.ActionListener()
720 {
721 public void actionPerformed(java.awt.event.ActionEvent e)
722 {
723 if (toneTypes[2] == (String) toneTypesComboBox.getSelectedItem())
724 {
725 vCharTypesComboBox.setSelectedIndex(2);
726 vCharTypesComboBox.setEnabled(false);
727 }
728 else
729 {
730 vCharTypesComboBox.setEnabled(true);
731 }
732 }
733 });
734 }
735 return toneTypesComboBox;
736 }
737
738 /** *//**
739 * This method initializes jComboBox1
740 *
741 * @return javax.swing.JComboBox
742 */
743 private JComboBox getVCharTypesComboBox()
744 {
745 if (vCharTypesComboBox == null)
746 {
747 vCharTypesComboBox = new JComboBox(vCharTypes);
748 }
749 return vCharTypesComboBox;
750 }
751
752 /** *//**
753 * This method initializes jComboBox2
754 *
755 * @return javax.swing.JComboBox
756 */
757 private JComboBox getCaseTypesComboBox()
758 {
759 if (caseTypesComboBox == null)
760 {
761 caseTypesComboBox = new JComboBox(caseTypes);
762 }
763 return caseTypesComboBox;
764 }
765
766 /** *//**
767 * @return
768 */
769 private String getChineseCharText()
770 {
771 return charTextField.getText();
772 }
773
774 String[] caseTypes = { "LOWERCASE", "UPPERCASE" };
775
776 String[] vCharTypes = { "WITH_U_AND_COLON", "WITH_V", "WITH_U_UNICODE" };
777
778 String[] toneTypes = { "WITH_TONE_NUMBER", "WITHOUT_TONE", "WITH_TONE_MARK" };
779
780 private JLabel toneLabel = null;
781
782 private JLabel charLabel = null;
783
784 private JTextField charTextField = null;
785
786 private JPanel unformattedCharPanel = null;
787
788 private JLabel unformattedHanyuPinyinLabel = null;
789
790 private JTextArea unformattedHanyuPinyinTextArea = null;
791
792 private JPanel unformattedHanyuPinyinPanel = null;
793
794 private JPanel unformattedTongyongPinyinPanel = null;
795
796 private JLabel unformattedTongyongPinyinLabel = null;
797
798 private JTextArea unformattedTongyongPinyinTextArea = null;
799
800 private JPanel unformattedWadePinyinPanel = null;
801
802 private JLabel unformattedWadePinyinLabel = null;
803
804 private JTextArea unformattedWadePinyinTextArea = null;
805
806 private JPanel unformattedMPS2PinyinPanel = null;
807
808 private JLabel unformattedMPS2PinyinLabel = null;
809
810 private JTextArea unformattedMPS2PinyinTextArea = null;
811
812 private JPanel unformattedYalePinyinPanel = null;
813
814 private JLabel unformattedYalePinyinLabel = null;
815
816 private JTextArea unformattedYalePinyinTextArea = null;
817
818 private JPanel unformattedGwoyeuRomatzyhPanel = null;
819
820 private JLabel unformattedGwoyeuRomatzyhLabel = null;
821
822 private JTextArea unformattedGwoyeuRomatzyhTextArea = null;
823
824 private JScrollPane unformattedMPS2PinyinScrollPane = null;
825
826 private JScrollPane unformattedHanyuPinyinScrollPane = null;
827
828 private JScrollPane unformattedTongyongPinyinScrollPane = null;
829
830 private JScrollPane unformattedWadePinyinScrollPane = null;
831
832 private JScrollPane unformattedYalePinyinScrollPane = null;
833
834 private JScrollPane unformattedGwoyeuRomatzyhScrollPane = null;
835} // @jve:decl-index=0:visual-constraint="10,10"
836
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 ([email protected])
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 {
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 unformattedYalePinyinPanel.add(getUnformattedYalePinyinScrollPane(), BorderLayout.CENTER); // Generated
288 }
289 return unformattedYalePinyinPanel;
290 }
291
292 /** *//**
293 * This method initializes unformattedYalePinyinTextArea
294 *
295 * @return javax.swing.JTextArea
296 */
297 private JTextArea getUnformattedYalePinyinTextArea()
298 {
299 if (unformattedYalePinyinTextArea == null)
300 {
301 unformattedYalePinyinTextArea = new JTextArea();
302 unformattedYalePinyinTextArea.setEditable(false); // Generated
303 unformattedYalePinyinTextArea.setLineWrap(true); // Generated
304 }
305 return unformattedYalePinyinTextArea;
306 }
307
308 /** *//**
309 * This method initializes unformattedGwoyeuRomatzyhPanel
310 *
311 * @return javax.swing.JPanel
312 */
313 private JPanel getUnformattedGwoyeuRomatzyhPanel()
314 {
315 if (unformattedGwoyeuRomatzyhPanel == null)
316 {
317 unformattedGwoyeuRomatzyhLabel = new JLabel();
318 unformattedGwoyeuRomatzyhLabel.setText("Gwoyeu Romatzyh"); // Generated
319 unformattedGwoyeuRomatzyhPanel = new JPanel();
320 unformattedGwoyeuRomatzyhPanel.setLayout(new BorderLayout()); // Generated
321 unformattedGwoyeuRomatzyhPanel.add(unformattedGwoyeuRomatzyhLabel, java.awt.BorderLayout.NORTH); // Generated
322 unformattedGwoyeuRomatzyhPanel.add(getUnformattedGwoyeuRomatzyhScrollPane(), BorderLayout.CENTER); // Generated
323 }
324 return unformattedGwoyeuRomatzyhPanel;
325 }
326
327 /** *//**
328 * This method initializes unformattedGwoyeuRomatzyhTextArea
329 *
330 * @return javax.swing.JTextArea
331 */
332 private JTextArea getUnformattedGwoyeuRomatzyhTextArea()
333 {
334 if (unformattedGwoyeuRomatzyhTextArea == null)
335 {
336 unformattedGwoyeuRomatzyhTextArea = new JTextArea();
337 unformattedGwoyeuRomatzyhTextArea.setEditable(false); // Generated
338 unformattedGwoyeuRomatzyhTextArea.setLineWrap(true); // Generated
339 }
340 return unformattedGwoyeuRomatzyhTextArea;
341 }
342
343 /** *//**
344 * This method initializes unformattedMPS2PinyinScrollPane
345 *
346 * @return javax.swing.JScrollPane
347 */
348 private JScrollPane getUnformattedMPS2PinyinScrollPane()
349 {
350 if (unformattedMPS2PinyinScrollPane == null)
351 {
352 unformattedMPS2PinyinScrollPane = new JScrollPane();
353 unformattedMPS2PinyinScrollPane.setViewportView(getUnformattedMPS2PinyinTextArea()); // Generated
354 }
355 return unformattedMPS2PinyinScrollPane;
356 }
357
358 /** *//**
359 * This method initializes unformattedHanyuPinyinScrollPane
360 *
361 * @return javax.swing.JScrollPane
362 */
363 private JScrollPane getUnformattedHanyuPinyinScrollPane()
364 {
365 if (unformattedHanyuPinyinScrollPane == null)
366 {
367 unformattedHanyuPinyinScrollPane = new JScrollPane();
368 unformattedHanyuPinyinScrollPane.setViewportView(getUnformattedHanyuPinyinTextArea()); // Generated
369 }
370 return unformattedHanyuPinyinScrollPane;
371 }
372
373 /** *//**
374 * This method initializes unformattedTongyongPinyinScrollPane
375 *
376 * @return javax.swing.JScrollPane
377 */
378 private JScrollPane getUnformattedTongyongPinyinScrollPane()
379 {
380 if (unformattedTongyongPinyinScrollPane == null)
381 {
382 unformattedTongyongPinyinScrollPane = new JScrollPane();
383 unformattedTongyongPinyinScrollPane.setViewportView(getUnformattedTongyongPinyinTextArea()); // Generated
384 }
385 return unformattedTongyongPinyinScrollPane;
386 }
387
388 /** *//**
389 * This method initializes unformattedWadePinyinScrollPane
390 *
391 * @return javax.swing.JScrollPane
392 */
393 private JScrollPane getUnformattedWadePinyinScrollPane()
394 {
395 if (unformattedWadePinyinScrollPane == null)
396 {
397 unformattedWadePinyinScrollPane = new JScrollPane();
398 unformattedWadePinyinScrollPane.setViewportView(getUnformattedWadePinyinTextArea()); // Generated
399 }
400 return unformattedWadePinyinScrollPane;
401 }
402
403 /** *//**
404 * This method initializes unformattedYalePinyinScrollPane
405 *
406 * @return javax.swing.JScrollPane
407 */
408 private JScrollPane getUnformattedYalePinyinScrollPane()
409 {
410 if (unformattedYalePinyinScrollPane == null)
411 {
412 unformattedYalePinyinScrollPane = new JScrollPane();
413 unformattedYalePinyinScrollPane.setViewportView(getUnformattedYalePinyinTextArea()); // Generated
414 }
415 return unformattedYalePinyinScrollPane;
416 }
417
418 /** *//**
419 * This method initializes unformattedGwoyeuRomatzyhScrollPane
420 *
421 * @return javax.swing.JScrollPane
422 */
423 private JScrollPane getUnformattedGwoyeuRomatzyhScrollPane()
424 {
425 if (unformattedGwoyeuRomatzyhScrollPane == null)
426 {
427 unformattedGwoyeuRomatzyhScrollPane = new JScrollPane();
428 unformattedGwoyeuRomatzyhScrollPane.setViewportView(getUnformattedGwoyeuRomatzyhTextArea()); // Generated
429 }
430 return unformattedGwoyeuRomatzyhScrollPane;
431 }
432
433 static public void main(String argv[])
434 {
435 final Pinyin4jAppletDemo appletDemo = new Pinyin4jAppletDemo();
436
437 System.runFinalizersOnExit(true);
438
439 JFrame jFrame = new JFrame(appName);
440 jFrame.addWindowListener(new WindowAdapter()
441 {
442 public void windowClosing(WindowEvent event)
443 {
444 appletDemo.stop();
445 appletDemo.destroy();
446 System.exit(0);
447 }
448 });
449 jFrame.add("Center", appletDemo);
450
451 appletDemo.init();
452 appletDemo.start();
453
454 jFrame.setSize(APP_SIZE);
455 jFrame.pack();
456 jFrame.setVisible(true);
457 }
458
459 /** *//**
460 * This is the default constructor
461 */
462 public Pinyin4jAppletDemo()
463 {
464 super();
465 init();
466 }
467
468 /** *//**
469 * This method initializes this
470 *
471 * @return void
472 */
473 public void init()
474 {
475 this.setSize(APP_SIZE);
476 this.setContentPane(getJContentPane());
477 this.setName(appName);
478 }
479
480 /** *//**
481 * This method initializes jContentPane
482 *
483 * @return javax.swing.JPanel
484 */
485 private JPanel getJContentPane()
486 {
487 if (jContentPane == null)
488 {
489 jContentPane = new JPanel();
490 jContentPane.setLayout(new BorderLayout());
491 jContentPane.add(getJTabbedPane(), java.awt.BorderLayout.CENTER); // Generated
492 jContentPane.add(getOptionPanel(), java.awt.BorderLayout.NORTH); // Generated
493 jContentPane.add(getButtonPanel(), java.awt.BorderLayout.SOUTH); // Generated
494 }
495 return jContentPane;
496 }
497
498 /** *//**
499 * This method initializes jTabbedPane
500 *
501 * @return javax.swing.JTabbedPane
502 */
503 private JTabbedPane getJTabbedPane()
504 {
505 if (jTabbedPane == null)
506 {
507 jTabbedPane = new JTabbedPane();
508 jTabbedPane.addTab("Unformatted Chinese Romanization Systems", null, getUnformattedCharPanel(), null); // Generated
509 jTabbedPane.addTab("Formatted Hanyu Pinyin", null, getFormattedCharPanel(), null); // Generated
510 }
511 return jTabbedPane;
512 }
513
514 /** *//**
515 * This method initializes jPanel3
516 *
517 * @return javax.swing.JPanel
518 */
519 private JPanel getFormattedCharPanel()
520 {
521 if (formattedCharPanel == null)
522 {
523 formattedCharPanel = new JPanel();
524 formattedCharPanel.setLayout(new BorderLayout());
525 formattedCharPanel.add(getFormattedOutputField(), java.awt.BorderLayout.CENTER); // Generated
526 }
527 return formattedCharPanel;
528 }
529
530 /** *//**
531 * This method initializes jPanel
532 *
533 * @return javax.swing.JPanel
534 */
535 private JPanel getOptionPanel()
536 {
537 if (optionPanel == null)
538 {
539 charLabel = new JLabel();
540 charLabel.setText("Input Chinese:"); // Generated
541
542 toneLabel = new JLabel();
543 toneLabel.setText(" Format:"); // Generated
544
545 optionPanel = new JPanel();
546 optionPanel.setPreferredSize(new java.awt.Dimension(640, 34)); // Generated
547 optionPanel.add(charLabel, null); // Generated
548 optionPanel.add(getCharTextField(), null); // Generated
549 optionPanel.add(toneLabel, null); // Generated
550 optionPanel.add(getToneTypesComboBox(), null); // Generated
551 optionPanel.add(getVCharTypesComboBox(), null); // Generated
552 optionPanel.add(getCaseTypesComboBox(), null); // Generated
553 }
554 return optionPanel;
555 }
556
557 /** *//**
558 * This method initializes jButton
559 *
560 * @return javax.swing.JButton
561 */
562 private JButton getConvertButton()
563 {
564 if (convertButton == null)
565 {
566 convertButton = new JButton();
567 convertButton.setText("Convert to Pinyin"); // Generated
568 convertButton.addActionListener(new java.awt.event.ActionListener()
569 {
570 public void actionPerformed(java.awt.event.ActionEvent e)
571 {
572 char chineseCharacter = getChineseCharText().charAt(0);
573
574 updateUnformattedTextField(chineseCharacter);
575
576 String toneSelection = (String) toneTypesComboBox.getSelectedItem();
577 String vcharSelection = (String) vCharTypesComboBox.getSelectedItem();
578 String caseSelection = (String) caseTypesComboBox.getSelectedItem();
579
580 updateFormattedTextField(chineseCharacter, toneSelection, vcharSelection, caseSelection);
581 }
582
583 /** *//**
584 * @param chineseCharacter
585 */
586 private void updateUnformattedTextField(char chineseCharacter)
587 {
588 unformattedHanyuPinyinTextArea.setText(concatPinyinStringArray(PinyinHelper.toHanyuPinyinStringArray(chineseCharacter)));
589 unformattedTongyongPinyinTextArea.setText(concatPinyinStringArray(PinyinHelper.toTongyongPinyinStringArray(chineseCharacter)));
590 unformattedWadePinyinTextArea.setText(concatPinyinStringArray(PinyinHelper.toWadeGilesPinyinStringArray(chineseCharacter)));
591 unformattedMPS2PinyinTextArea.setText(concatPinyinStringArray(PinyinHelper.toMPS2PinyinStringArray(chineseCharacter)));
592 unformattedYalePinyinTextArea.setText(concatPinyinStringArray(PinyinHelper.toYalePinyinStringArray(chineseCharacter)));
593 unformattedGwoyeuRomatzyhTextArea.setText(concatPinyinStringArray(PinyinHelper.toGwoyeuRomatzyhStringArray(chineseCharacter)));
594 }
595
596 /** *//**
597 * @param chineseCharacter
598 * @param toneSelection
599 * @param vcharSelection
600 * @param caseSelection
601 */
602 private void updateFormattedTextField(char chineseCharacter, String toneSelection, String vcharSelection, String caseSelection)
603 {
604 HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
605
606 if (toneTypes[0] == toneSelection)
607 {
608 outputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);
609 }
610 else if (toneTypes[1] == toneSelection)
611 {
612 outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
613 }
614 else if (toneTypes[2] == toneSelection)
615 {
616 outputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
617 }
618
619 if (vCharTypes[0] == vcharSelection)
620 {
621 outputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);
622 }
623 else if (vCharTypes[1] == vcharSelection)
624 {
625 outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
626 }
627 else if (vCharTypes[2] == vcharSelection)
628 {
629 outputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
630 }
631
632 if (caseTypes[0] == caseSelection)
633 {
634 outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
635 }
636 else if (caseTypes[1] == caseSelection)
637 {
638 outputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
639 }
640
641 String[] pinyinArray = null;
642 try
643 {
644 pinyinArray = PinyinHelper.toHanyuPinyinStringArray(chineseCharacter, outputFormat);
645 }
646 catch (BadHanyuPinyinOutputFormatCombination e1)
647 {
648 e1.printStackTrace();
649 }
650 String outputString = concatPinyinStringArray(pinyinArray);
651 formattedOutputField.setText(outputString);
652 }
653
654 /** *//**
655 * @param pinyinArray
656 * @return
657 */
658 private String concatPinyinStringArray(String[] pinyinArray)
659 {
660 StringBuffer pinyinStrBuf = new StringBuffer();
661
662 if ((null != pinyinArray) && (pinyinArray.length > 0))
663 {
664 for (int i = 0; i < pinyinArray.length; i++)
665 {
666 pinyinStrBuf.append(pinyinArray[i]);
667 pinyinStrBuf.append(System.getProperty("line.separator"));
668 }
669 }
670 String outputString = pinyinStrBuf.toString();
671 return outputString;
672 }
673 });
674
675 }
676 return convertButton;
677 }
678
679 /** *//**
680 * This method initializes jPanel2
681 *
682 * @return javax.swing.JPanel
683 */
684 private JPanel getButtonPanel()
685 {
686 if (buttonPanel == null)
687 {
688 buttonPanel = new JPanel();
689 buttonPanel.add(getConvertButton(), null); // Generated
690 }
691 return buttonPanel;
692 }
693
694 /** *//**
695 * This method initializes jTextArea1
696 *
697 * @return javax.swing.JTextArea
698 */
699 private JTextArea getFormattedOutputField()
700 {
701 if (formattedOutputField == null)
702 {
703 formattedOutputField = new JTextArea();
704 formattedOutputField.setEditable(false); // Generated
705 }
706 return formattedOutputField;
707 }
708
709 /** *//**
710 * This method initializes jComboBox
711 *
712 * @return javax.swing.JComboBox
713 */
714 private JComboBox getToneTypesComboBox()
715 {
716 if (toneTypesComboBox == null)
717 {
718 toneTypesComboBox = new JComboBox(toneTypes);
719 toneTypesComboBox.addActionListener(new java.awt.event.ActionListener()
720 {
721 public void actionPerformed(java.awt.event.ActionEvent e)
722 {
723 if (toneTypes[2] == (String) toneTypesComboBox.getSelectedItem())
724 {
725 vCharTypesComboBox.setSelectedIndex(2);
726 vCharTypesComboBox.setEnabled(false);
727 }
728 else
729 {
730 vCharTypesComboBox.setEnabled(true);
731 }
732 }
733 });
734 }
735 return toneTypesComboBox;
736 }
737
738 /** *//**
739 * This method initializes jComboBox1
740 *
741 * @return javax.swing.JComboBox
742 */
743 private JComboBox getVCharTypesComboBox()
744 {
745 if (vCharTypesComboBox == null)
746 {
747 vCharTypesComboBox = new JComboBox(vCharTypes);
748 }
749 return vCharTypesComboBox;
750 }
751
752 /** *//**
753 * This method initializes jComboBox2
754 *
755 * @return javax.swing.JComboBox
756 */
757 private JComboBox getCaseTypesComboBox()
758 {
759 if (caseTypesComboBox == null)
760 {
761 caseTypesComboBox = new JComboBox(caseTypes);
762 }
763 return caseTypesComboBox;
764 }
765
766 /** *//**
767 * @return
768 */
769 private String getChineseCharText()
770 {
771 return charTextField.getText();
772 }
773
774 String[] caseTypes = { "LOWERCASE", "UPPERCASE" };
775
776 String[] vCharTypes = { "WITH_U_AND_COLON", "WITH_V", "WITH_U_UNICODE" };
777
778 String[] toneTypes = { "WITH_TONE_NUMBER", "WITHOUT_TONE", "WITH_TONE_MARK" };
779
780 private JLabel toneLabel = null;
781
782 private JLabel charLabel = null;
783
784 private JTextField charTextField = null;
785
786 private JPanel unformattedCharPanel = null;
787
788 private JLabel unformattedHanyuPinyinLabel = null;
789
790 private JTextArea unformattedHanyuPinyinTextArea = null;
791
792 private JPanel unformattedHanyuPinyinPanel = null;
793
794 private JPanel unformattedTongyongPinyinPanel = null;
795
796 private JLabel unformattedTongyongPinyinLabel = null;
797
798 private JTextArea unformattedTongyongPinyinTextArea = null;
799
800 private JPanel unformattedWadePinyinPanel = null;
801
802 private JLabel unformattedWadePinyinLabel = null;
803
804 private JTextArea unformattedWadePinyinTextArea = null;
805
806 private JPanel unformattedMPS2PinyinPanel = null;
807
808 private JLabel unformattedMPS2PinyinLabel = null;
809
810 private JTextArea unformattedMPS2PinyinTextArea = null;
811
812 private JPanel unformattedYalePinyinPanel = null;
813
814 private JLabel unformattedYalePinyinLabel = null;
815
816 private JTextArea unformattedYalePinyinTextArea = null;
817
818 private JPanel unformattedGwoyeuRomatzyhPanel = null;
819
820 private JLabel unformattedGwoyeuRomatzyhLabel = null;
821
822 private JTextArea unformattedGwoyeuRomatzyhTextArea = null;
823
824 private JScrollPane unformattedMPS2PinyinScrollPane = null;
825
826 private JScrollPane unformattedHanyuPinyinScrollPane = null;
827
828 private JScrollPane unformattedTongyongPinyinScrollPane = null;
829
830 private JScrollPane unformattedWadePinyinScrollPane = null;
831
832 private JScrollPane unformattedYalePinyinScrollPane = null;
833
834 private JScrollPane unformattedGwoyeuRomatzyhScrollPane = null;
835} // @jve:decl-index=0:visual-constraint="10,10"
836