电子词典

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class eDict extends JFrame implements ActionListener,KeyListener {
    private JTextField inField;//输入框
    private JTextArea outField;//输出框,显示结果
    private JButton button1,button2;

    //
    //    构造
    //    基本界面
    //
    public eDict(){
        this.setTitle("英文小字典");
        this.setBounds(60,80,500,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);

        JLabel label = new JLabel("输入单词:");
        inField = new JTextField(21);
        inField.setCaretPosition(0);//让光标停留在文本框的特定位置
        inField.addKeyListener(this);//键盘监听
        button1 = new JButton("查找");
        button1.addActionListener(this);
        button2 = new JButton("清空");
        button2.addActionListener(this);
        outField = new JTextArea("\n    ●欢迎使用英文小字典\n",17,42);
        Color c = new Color(238,238,238);
        outField.setBackground(c);
        outField.setLineWrap(true);//使其自动换行
        outField.setEditable(false); //不可编辑,可选中文字
        outField.setBorder(BorderFactory.createLineBorder(c));//设置边框颜色
        JScrollPane scroll = new JScrollPane(outField);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); //垂直滚动条
        scroll.setRequestFocusEnabled(false);
        scroll.setBorder(BorderFactory.createLineBorder(c));//设置边框颜色

        JPanel up = new JPanel();
        up.add(label);
        up.add(inField);
        up.add(button1);
        up.add(button2);
        this.add(up,"North");
        JPanel down = new JPanel();
        down.add(scroll);
        this.add(down);
        this.setVisible(true);
    }//    构造 结束

    //
    //    查找函数
    //    键盘和按钮查找部分
    //    本地查找
    //
    private String find(String letter){
        String meaning = null;
        boolean flags = false;
        // 连接数据库
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=E:\\天天\\Java\\电子词典(数据库版)\\词库.accdb";
            Connection conn = DriverManager.getConnection(url, "username", "password");
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from 字典");
            while(rs.next()) {
                if(letter.equalsIgnoreCase(rs.getString(1))){
                    meaning = rs.getString(2);
                    flags = true;
                }
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        if(flags){
            return meaning;
        }else{
            return "词库中没有该单词!";
        }
    }

    //
    //    键盘监听
    //    按下Enter键后执行查找功能
    //
    public void keyPressed(KeyEvent e){
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
            String letter = inField.getText();
            if(letter.equalsIgnoreCase("")){
                outField.setText("\n  ●输入不能为空");
            }else{
                outField.setText("\n  ●"+find(letter));
            }
            inField.requestFocus();//设置焦点位置
            inField.requestFocus();//设置焦点位置
        }// end of "getKeyCode"
    }// end of "keyPressed"
    public void keyReleased(KeyEvent e){
        //
    }
    //
    //    使输入框中只接受字母
    //
    public void keyTyped(KeyEvent e){
        if(inField.getText().length() > 15){
            e.consume();
        }
        char c = e.getKeyChar();//获取按键字符,也可以是e.getKeyCode()
        if(!isLetter(c))
        //if( !Character.isLetter(c) || (c>127&&c!=45) )//isLetter指的是各种字符,包括汉字
        //if(!Character.isDigit(c))
            e.consume();//丢弃由键盘产生的事件
    }
    public void showKeyEventMsg(KeyEvent e){
        //
    }
    //    键盘监听    结束

    //
    //    判断读入的字符是否为字母
    //
    public static boolean isLetter(char ch){
        if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
            return true;
        }
        else
            return false;
    }

    //
    //    按钮事件监听
    //
    public void actionPerformed(ActionEvent e) {
        if(button1 == e.getSource()){
            String letter = inField.getText();
            if(letter.equalsIgnoreCase("")){
                outField.setText("  ●输入不能为空");
            }else{
                outField.setText(""+find(letter));
            }
            inField.requestFocus();//设置焦点位置
        }//end of "button1"

        if(button2 == e.getSource()){
            inField.setText("");
            inField.requestFocus();//设置焦点位置
            //inField.setCaretPosition(0);//让光标停留在文本框的特定位置
        }
    }//    事件监听 结尾

    //
    //    主函数
    //
    public static void main(String[] args){
        // TODO Auto-generated method stub
        eDict e = new eDict();
    }
}

 

posted @ 2013-01-06 21:41  放学后的茶会  阅读(92)  评论(0)    收藏  举报