import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class DictDemo { public static void main(String[] args) { new DictFrame(); } } class DictFrame extends JFrame implements ActionListener{ JTextField text1,text2; JButton bt; DictFrame(){ setTitle("Dict"); setSize(300,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); bt = new JButton("翻 译"); bt.addActionListener(this); text1 = new JTextField(10); text2 = new JTextField(10); text2.setEditable(false); add(text1,BorderLayout.WEST); add(text2,BorderLayout.EAST); add(bt,BorderLayout.SOUTH); pack(); this.setLocationRelativeTo(null); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { text2.setText(Dicts.getMean(text1.getText().trim().toLowerCase())); } } class Dicts { static int id ; static String[][] dict ; static { dict = new String[100][2]; id = 0; insertWord("china","中国"); insertWord("jpanese","日本"); insertWord("你好","hello"); } private Dicts(){ } public static void insertWord(String str1,String str2){ dict[id][0] = str1.toLowerCase(); dict[id][1] = str2.toLowerCase(); id++; } public static String getMean(String str){ for(int i=0;i<id;i++) { if (dict[i][0].equals(str)) return dict[i][1]; if (dict[i][1].equals(str)) return dict[i][0]; } return "查无此字"; } }