基本信息
源码名称:java 英汉双语小词典 源码下载
源码大小:4.24M
文件格式:.rar
开发语言:Java
更新时间:2016-12-27
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 4 元 
   源码介绍

package com.mingrisoft.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.apache.commons.collections.IterableMap;
import org.apache.commons.collections.MapIterator;

import com.mingrisoft.model.Word;
import com.mingrisoft.util.ConfigurationUtil;
import com.mingrisoft.util.DBHelper;
import com.mingrisoft.util.JTextPaneUtil;
import com.mingrisoft.util.SwingUtil;
import com.mingrisoft.util.WebsiteOpener;
import com.mingrisoft.util.WordListCellRenderer;

public class MainFrame extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = -7454723369403017689L;
    private static Font font = ConfigurationUtil.getSystemFont();// 获得系统字体
    private JPanel contentPane;
    private JTextField wordSearchTextField;
    private DefaultListModel model;
    private JList wordList;
    private Word word;

    private static Logger logger = Logger.getLogger(WebsiteOpener.class.getName());// 获得日志对象
    private static Level level = ConfigurationUtil.getLevel();// 从属性文件中读取日志等级
    private JTextPane textPane;
    private JScrollPane wordDetailScrollPane;
    static {
        logger.setLevel(level);// 使用静态块设置日志等级
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainFrame() {
        setTitle("英汉双语词典");// 设置窗体标题
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 当关闭窗体是退出程序
        setSize(600, 371);// 设置窗体大小
        setLocation(SwingUtil.centreContainer(getSize()));// 让窗体居中显示

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu dictionaryManagementMenu = new JMenu("词典管理");
        dictionaryManagementMenu.setFont(font);
        menuBar.add(dictionaryManagementMenu);

        JMenuItem wordAdditionMenuItem = new JMenuItem("添加单词");
        wordAdditionMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                do_wordAdditionMenuItem_actionPerformed(e);
            }
        });
        wordAdditionMenuItem.setFont(font);
        dictionaryManagementMenu.add(wordAdditionMenuItem);

        JMenuItem wordMotificationMenuItem = new JMenuItem("修改单词");
        wordMotificationMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                do_wordMotificationMenuItem_actionPerformed(e);
            }
        });
        wordMotificationMenuItem.setFont(font);
        dictionaryManagementMenu.add(wordMotificationMenuItem);

        JMenuItem wordDeletionMenuItem = new JMenuItem("删除单词");
        wordDeletionMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                do_wordDeletionMenuItem_actionPerformed(e);
            }
        });
        wordDeletionMenuItem.setFont(font);
        dictionaryManagementMenu.add(wordDeletionMenuItem);

        JMenuItem wordFrequencyMenuItem = new JMenuItem("查询统计");
        wordFrequencyMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                do_wordFrequencyMenuItem_actionPerformed(e);
            }
        });
        wordFrequencyMenuItem.setFont(font);
        dictionaryManagementMenu.add(wordFrequencyMenuItem);

        JMenu toolMenu = new JMenu("小工具");
        toolMenu.setFont(font);
        menuBar.add(toolMenu);

        JMenuItem notepadMenuItem = new JMenuItem("记事本");
        notepadMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                do_notepadMenuItem_actionPerformed(e);
            }
        });
        notepadMenuItem.setFont(font);
        toolMenu.add(notepadMenuItem);

        JMenuItem calculatorMenuItem = new JMenuItem("计算器");
        calculatorMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                do_calculatorMenuItem_actionPerformed(e);
            }
        });
        calculatorMenuItem.setFont(font);
        toolMenu.add(calculatorMenuItem);

        JMenu favoriteMenu = new JMenu("收藏夹");
        favoriteMenu.setFont(font);
        toolMenu.add(favoriteMenu);

        IterableMap websites = ConfigurationUtil.getFavorite();// 从XML文件读取网站信息
        MapIterator it = websites.mapIterator();// 获得MapIterator
        while (it.hasNext()) {
            String key = (String) it.next();// 获得网站名称
            final String value = (String) it.getValue();// 获得网站地址
            JMenuItem webPageMenuItem = new JMenuItem(key);// 使用网站名称创建菜单项
            webPageMenuItem.setFont(font);// 设置字体
            webPageMenuItem.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    WebsiteOpener.open(value);// 使用WebsiteOpener类打开网页
                }
            });
            favoriteMenu.add(webPageMenuItem);// 为菜单增加菜单项
        }

        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel wordSearchPanel = new JPanel();
        wordSearchPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
        contentPane.add(wordSearchPanel, BorderLayout.NORTH);

        JLabel wordSearchLabel = new JLabel("请输入要查询的单词:");
        wordSearchLabel.setFont(font);
        wordSearchPanel.add(wordSearchLabel);

        wordSearchTextField = new JTextField();
        wordSearchTextField.setFont(font);
        wordSearchPanel.add(wordSearchTextField);
        wordSearchTextField.setColumns(20);

        JButton wordSearchButton = new JButton("查询");
        wordSearchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                do_wordSearchButton_actionPerformed(e);
            }
        });
        wordSearchButton.setFont(font);
        wordSearchPanel.add(wordSearchButton);

        JPanel wordListPanel = new JPanel();
        wordListPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
        wordListPanel.setLayout(new BorderLayout(0, 0));
        contentPane.add(wordListPanel, BorderLayout.WEST);

        JScrollPane wordListScrollPane = new JScrollPane();
        wordListPanel.setPreferredSize(new Dimension(200, 0));
        wordListPanel.add(wordListScrollPane, BorderLayout.CENTER);

        wordList = new JList();
        wordList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                do_wordList_valueChanged(e);
            }
        });
        model = new DefaultListModel();// 创建表格模型
        List<Word> words = DBHelper.retrieveAllWords();// 获得表格中保存的全部单词
        for (Word word : words) {
            model.addElement(word);// 将单词全部增加到列表模型中
        }
        wordList.setModel(model);// 为列表设置列表模型
        wordList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 设置列表选择模式为单选
        wordList.setFont(font);// 为列表设置字体
        wordList.setCellRenderer(new WordListCellRenderer());// 为列表设置渲染器
        wordListScrollPane.setViewportView(wordList);// 在滚动窗格中显示列表

        JPanel wordDetailPanel = new JPanel();
        wordDetailPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
        wordDetailPanel.setLayout(new BorderLayout(0, 0));
        contentPane.add(wordDetailPanel, BorderLayout.CENTER);

        wordDetailScrollPane = new JScrollPane();
        wordDetailPanel.add(wordDetailScrollPane, BorderLayout.CENTER);

        word = DBHelper.retrieveRandomWord();
        textPane = new JTextPane();
        wordDetailScrollPane.setViewportView(JTextPaneUtil.getTextPane(textPane, word));
    }

    // 打开增加单词对话框
    protected void do_wordAdditionMenuItem_actionPerformed(ActionEvent e) {
        WordAdditionDialog dialog = new WordAdditionDialog(model);
        dialog.setVisible(true);
    }

    // 打开修改单词对话框
    protected void do_wordMotificationMenuItem_actionPerformed(ActionEvent e) {
        Word word = (Word) wordList.getSelectedValue();
        if (word == null) {
            JOptionPane.showMessageDialog(this, "请选择要修改的单词!", "提示信息", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        WordModificationDialog dialog = new WordModificationDialog(model, word);
        dialog.setVisible(true);
        wordDetailScrollPane.setViewportView(JTextPaneUtil.getTextPane(textPane, word));
    }

    // 删除单词
    protected void do_wordDeletionMenuItem_actionPerformed(ActionEvent e) {
        Word word = (Word) wordList.getSelectedValue();// 获得用户选择的单词
        if (word == null) {
            JOptionPane.showMessageDialog(this, "请选择要删除的单词!", "提示信息", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        int result = JOptionPane.showConfirmDialog(this, "确认删除单词"   word   "?", "确认信息", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (result == JOptionPane.YES_OPTION) {
            if (DBHelper.deleteSingleWord(word.getId()) != -1) {
                JOptionPane.showMessageDialog(this, "成功删除单词"   word   "!", "提示信息", JOptionPane.INFORMATION_MESSAGE);
                model.removeElement(word);// 在列表模型中删除该单词
                return;
            } else {
                JOptionPane.showMessageDialog(this, "删除单词"   word   "失败!", "警告信息", JOptionPane.WARNING_MESSAGE);
                return;
            }
        }
    }

    // 用于打开显示符合查询频率单词的窗体
    protected void do_wordFrequencyMenuItem_actionPerformed(ActionEvent e) {
        FrequencyTableDialog dialog = new FrequencyTableDialog();
        dialog.setVisible(true);
    }

    // 打开记事本
    protected void do_notepadMenuItem_actionPerformed(ActionEvent e) {
        Runtime runtime = Runtime.getRuntime();// 获得Runtime对象
        try {
            runtime.exec("notepad");// 运行系统命令
        } catch (IOException e1) {
            logger.log(Level.WARNING, "打开记事本失败!", e1);
        }
    }

    // 打开计算器
    protected void do_calculatorMenuItem_actionPerformed(ActionEvent e) {
        Runtime runtime = Runtime.getRuntime();// 获得Runtime对象
        try {
            runtime.exec("calc");// 运行系统命令
        } catch (IOException e1) {
            logger.log(Level.WARNING, "打开计算器失败!", e1);
        }
    }

    protected void do_wordSearchButton_actionPerformed(ActionEvent e) {
        String spelling = wordSearchTextField.getText();// 获得用户输入的单词
        if (spelling.isEmpty()) {// 判断单词是否为空
            JOptionPane.showMessageDialog(this, "请输入要查询的单词!", "提示信息", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        word = DBHelper.retrieveWordBySpellingForUser(spelling);// 在数据表中查询单词
        if (word == null) {// 如果查询是否则询问是否联网查询
            int result = JOptionPane.showConfirmDialog(this, "查询的单词并不存在,是否联网查询?", "提示信息", JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                WebsiteOpener.open("www.iciba.com/"   spelling);// 打开网页
            }
        } else {// 如果查询成功,显示查询到的单词细节
            wordDetailScrollPane.setViewportView(JTextPaneUtil.getTextPane(textPane, word));
        }
    }

    protected void do_wordList_valueChanged(ListSelectionEvent e) {
        Word word = (Word) wordList.getSelectedValue();// 获得用户选择的单词
        wordDetailScrollPane.setViewportView(JTextPaneUtil.getTextPane(textPane, word));// 在文本窗格中显示新单词
    }
}