实例介绍
【实例截图】


【核心代码】
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));// 在文本窗格中显示新单词
}
}
标签: java
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论