zoukankan      html  css  js  c++  java
  • 一个英汉字典例,某书课后题

    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 "查无此字";
        }
    }
  • 相关阅读:
    python note 30 断点续传
    python note 29 线程创建
    python note 28 socketserver
    python note 27 粘包
    python note 26 socket
    python note 25 约束
    Sed 用法
    python note 24 反射
    python note 23 组合
    python note 22 面向对象成员
  • 原文地址:https://www.cnblogs.com/qqjue/p/2620494.html
Copyright © 2011-2022 走看看