• 一个英汉字典例,某书课后题


    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 "查无此字";
        }
    }
  • 相关阅读:
    MYSQL定时任务 触发器
    mybatis 学习
    SSM 记录
    环境变量配置
    servlet 拦截器 (filter)
    验证码
    jquery $.ajax({});参数详解
    maven打包忽略静态资源解决办法,dispatchServlet拦截静态资源请求的解决办法
    switch..case..
    HDU 1005 题解
  • 原文地址:https://www.cnblogs.com/qqjue/p/2620494.html
Copyright © 2020-2023  润新知