【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