【java】简单实现中英文互翻译功能界面


 1 import java.awt.*;
 2 import java.awt.event.*;
 3 import javax.swing.*;
 4 
 5 /*
 6 有两个文本框,第一个框输入英文,再第二个文本框中自动显示汉语解释;
 7 在第一个框输入中文,第二个框给出英文翻译
 8 */
 9 class MyFrame58 extends JFrame implements ActionListener{
10     JLabel engl;
11     JLabel chnl;
12     JTextField tfEng;
13     JTextField tfChn;
14     String [][] dictArr = 
15     {
16         {"hello","你好"},
17         {"frame","框架"},
18         {"label","标签"},
19         {"text","文本"},
20         {"textfield","文本区域"}
21     };
22 
23     MyFrame58(){
24         // Constructor
25         engl = new JLabel("输入英文或中文:");
26         chnl = new JLabel("翻译结果:");
27         tfEng = new JTextField();
28         tfChn = new JTextField();
29 
30         setSize(400,200);
31         setTitle("简单中英文互翻译");
32         Container conPane = getContentPane();
33         conPane.setBackground(Color.WHITE);
34         conPane.setLayout(new GridLayout(2,2));
35         conPane.add(engl);
36         conPane.add(tfEng);
37         conPane.add(chnl);
38         conPane.add(tfChn);
39         tfEng.addActionListener(this);
40     }
41 
42     public void actionPerformed(ActionEvent e){
43         int i=0;
44         if (e.getSource() == tfEng){ //文本框获得事件源
45             while (i<dictArr.length){
46                 if (tfEng.getText().equals(dictArr[i][0])){ //字符串判断内容是否相等应该使用str1.equals(str2)方法
47                         tfChn.setText(dictArr[i][1]);
48                         break;
49                 }
50                 else if (tfEng.getText().equals(dictArr[i][1])){
51                     tfChn.setText(dictArr[i][0]);
52                     break;
53                 }     
54                 else{
55                             tfChn.setText("无匹配");
56                             i++;
57                         }    
58             }
59         }
60 
61     }
62 }
63 
64 public class Ex_5_8{
65     public static void main(String[] args) {
66         MyFrame58 mf = new MyFrame58();
67         mf.setVisible(true);
68     }
69 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM