使用的jar包为jxl.jar,单词数据文件words.xls,格式:第一列日语单词,第二列汉语。
Words.java
package com.gary.gui; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import com.gary.util.japanese.RandomWord; import com.gary.util.japanese.RandomWords; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; /** * 日语学习小工具 * @author gary * */ public class Words extends JFrame implements ActionListener { private static final long serialVersionUID = 3308314925505359304L; private JLabel textLabel; private JButton nextButton; private JMenuItem jmiHelp, jmiAbout, jmiExit; private JComboBox select; public Words(String title) { super(title); Container cp = this.getContentPane(); cp.setLayout(new FlowLayout()); select = new JComboBox(); select.setModel(new DefaultComboBoxModel(new String[] { "全部", "单词", "汉语", "五十音图", "假名", "平假名", "片假名", "罗马音" })); cp.add(select); cp.add(nextButton = new JButton("下一个")); nextButton.addActionListener(this); cp.add(textLabel = new JLabel("Welcome!")); JMenuBar jmb = new JMenuBar(); setJMenuBar(jmb); JMenu fileMenu = new JMenu("File", false); JMenu helpMenu = new JMenu("Help", false); jmb.add(fileMenu); jmb.add(helpMenu); fileMenu.add(jmiExit = new JMenuItem("Exit")); helpMenu.add(jmiHelp = new JMenuItem("Help")); helpMenu.add(jmiAbout = new JMenuItem("About")); jmiExit.addActionListener(this); jmiHelp.addActionListener(this); jmiAbout.addActionListener(this); } public void actionPerformed(ActionEvent e) { switch (select.getSelectedIndex()) { case 0: textLabel.setText(RandomWords.randomAll()); break; case 1: textLabel.setText(RandomWords.randomJapanese()); break; case 2: textLabel.setText(RandomWords.randomChinese()); break; case 3: textLabel.setText(RandomWord.randomAll()); break; case 4: textLabel.setText(RandomWord.randomAllWords()); break; case 5: textLabel.setText(RandomWord.randomHiranagana()); break; case 6: textLabel.setText(RandomWord.randomKatakana()); break; case 7: textLabel.setText(RandomWord.randomSound()); break; } if (e.getSource() == jmiHelp) { JOptionPane.showMessageDialog(this, "把words.xls文件放在本程序目录下,xls格式:第一列为外语,第二列为汉语。本程序自带日语五十音图数据。"); } else if (e.getSource() == jmiAbout) { JOptionPane.showMessageDialog(this, "日语学习工具V1.0 Powered by gary.QQ:408036296"); } else if (e.getSource() == jmiExit) System.exit(0); } public static void main(String[] args) { Words frame = new Words("日语"); frame.setSize(180, 120); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
JapaneseUtil.java
package com.gary.util.japanese; import java.util.Arrays; /** * 日语工具 * @author gary * */ public class JapaneseUtil { /** * 片假名 */ public static final String[][] katakana_words = { {"ア","イ","ウ","エ","オ"}, {"カ","キ","ク","ケ","コ"}, {"サ","シ","ス","セ","ソ"}, {"タ","チ","ツ","テ","ト"}, {"ナ","ニ","ヌ","ネ","ノ"}, {"ハ","ヒ","フ","へ","ホ"}, {"マ","ミ","ム","メ","モ"}, {"ヤ"," ","ユ"," ","ヨ"}, {"ラ","リ","ル","レ","ロ"}, {"ワ"," "," "," ","ヲ"}, {"ン"} }; /* 空 */ public static final String emptyStr = " "; /** * 罗马音 */ public static final String[][] sounds = { {"a","i","u","e","o"}, {"ka","ki","ku","ke","ko"}, {"sa","si","su","se","so"}, {"ta","ti","tu","te","to"}, {"na","ni","nu","ne","no"}, {"ha","hi","hu","he","ho"}, {"ma","mi","mu","me","mo"}, {"ya"," ","yu"," ","yo"}, {"ra","ri","ru","re","ro"}, {"wa"," "," "," ","wo"}, {"n"} }; /** * 平假名 */ public static final String[][] hiranagana_words = { {"あ","い","う","え","お"}, {"か","き","く","け","こ"}, {"さ","し","す","せ","そ"}, {"た","ち","つ","て","と"}, {"な","に","ぬ","ね","の"}, {"は","ひ","ふ","へ","ほ"}, {"ま","み","む","め","も"}, {"や"," ","ゆ"," ","よ"}, {"ら","り","る","れ","こ"}, {"わ"," "," "," ","を"}, {"ん"} }; /** * 根据假名查找 * @param word 假名 * @return */ public static String findByWords(String word){ String katakanaSound = find(katakana_words, sounds, word); String hiranaganaSound = find(hiranagana_words, sounds, word); if(katakanaSound != null){ return katakanaSound; }else if(hiranaganaSound != null){ return hiranaganaSound; }else{ return "未找到"; } } /** * 根据罗马音查找 * @param sound 罗马音 * @return */ public static String findBySounds(String sound){ return find(sounds, hiranagana_words, sound) + " " + find(sounds, katakana_words, sound); } /** * 查找 * @param sourceData 源数组 * @param targetData 目标数组 * @param key 关键字 * @return */ public static String find(String[][] sourceData, String[][] targetData, String key){ for (int i = 0; i < sourceData.length; i++) { for (int j = 0; j < sourceData[i].length; j++) { if(sourceData[i][j].equals(key)){ return targetData[i][j]; } } } return null; } /** * 输出 * @param wordsArray * @param soundsArray */ public static void print(String[][] wordsArray, String[][] soundsArray){ final String space = " "; for (int i = 0; i < wordsArray.length; i++) { for (int j = 0; j < wordsArray[i].length; j++) { if(j == wordsArray[i].length-1){ if(wordsArray[i][j].equals(emptyStr)){ System.out.println(space); }else{ System.out.println(wordsArray[i][j]+soundsArray[i][j]); } }else{ if(wordsArray[i][j].equals(emptyStr)){ System.out.print(space); }else{ System.out.print(wordsArray[i][j]+soundsArray[i][j]+", "); } } } } } /** * 输出 * @param wordsArrayA * @param wordsArrayB * @param soundsArray */ public static void print(String[][] wordsArrayA,String[][] wordsArrayB , String[][] soundsArray){ final String space = " "; for (int i = 0; i < wordsArrayA.length; i++) { for (int j = 0; j < wordsArrayA[i].length; j++) { if(j == wordsArrayA[i].length-1){ if(wordsArrayA[i][j].equals(emptyStr)){ System.out.println(space); }else{ System.out.println(wordsArrayA[i][j]+wordsArrayB[i][j]+soundsArray[i][j]); } }else{ if(wordsArrayA[i][j].equals(emptyStr)){ System.out.print(space); }else{ System.out.print(wordsArrayA[i][j]+wordsArrayB[i][j]+soundsArray[i][j]+", "); } } } } } /** * 输出数组 * @param array */ public static void printArray(String[][] array){ for (int i = 0; i < array.length; i++) { System.out.println(Arrays.toString(array[i])); } } }
剩余代码见 日语学习小工具(GUI版)(二)
附件为源码+exe版程序