前几天,偶然看见软件世界上有篇文章说,做个带词法解析得东西,有点象eclipse,用的是rtext,我也试着做了带拼写检查的,用了著名的开源的jazz来做拼写检查器,本意嘛,就是想做个自己的编辑器,然后自己用,其实好简单的,做了个关键的模块,实现了这个功能。在javaresearch上有人也发过贴子,只是它没有贴出源代码来,结果让我好找哦,在我的代码里为了要实现什么软件工程得松散耦合,同时也为了以后的继续修改,分了些块,也修改了jazzy的源代码,细心的朋友一定能看出修改的地方的,不细说了,不想让大家偷懒。
代码如下:
MYTestPane.java
package com.hb.Editorface;
import javax.swing.*;
import java.io.File;
import java.awt.event.*;
import javax.swing.event.*;
import com.swabunga.spell.engine.*;
import com.swabunga.spell.swing.JTextComponentSpellChecker;
/*
* MYTextPane
* @author Administrator
*
* TODO CopRight by VvSoft.Team
*
* this TextPane is include SpellChecker and it is MultiThread.
*
*/
public class MYTextPane extends JTextPane implements ActionListener,MenuListener{
private static final String Endict="dict/eng_com.dic";
private static final String phonetic="dict/ise.dic";
public SpellDictionary dictionary=null; /*字典类*/
JTextComponentSpellChecker sc=null;
JPopupMenu popup;
MYMenu menu;
public MYTextPane(String dicPath,String phoPath){
File dicFile=null,
phoFile=null;
menu=new MYMenu();
popup = menu.makePopupMenu(
new Object[] {"自动拼写检查"}, this);
//getContentPane().addMouseListener
this.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent evt) {
if (evt.isPopupTrigger())
popup.show(evt.getComponent(), evt.getX(), evt.getY());
}
});
if(dicPath==null){
dicFile=new File(Endict);
}else{
dicFile=new File(dicPath);
}
if(phoFile!=null)
phoFile=new File(phoPath);
try{System.out.println("ok");
dictionary = new SpellDictionaryHashMap(dicFile, phoFile);/*dicFile是字典文件,phoFile是词法构造的文件,这个我的翻译不准确,代码中可以看出这个参数一般为空*/
sc=new JTextComponentSpellChecker(dictionary);/*文本控件的拼写检查类,是jazzy自带的*/
sc.setPane(this);/*设置面板*/
System.out.println("ok");
}catch(Exception e){
e.printStackTrace();
}
}
public class SpellThread extends Thread{
public void run(){
sc.startAutoSpellCheck();/*在textpane中执行拼写检查,对于jazzy源代码有改动*/
}
}
public void actionPerformed(ActionEvent evt) {
String arg = evt.getActionCommand();
System.out.println(arg);
if(arg.equals("Exit"))
System.exit(0);
else{
SpellThread th=new SpellThread();
th.start();
}
}
public void menuSelected(MenuEvent evt) {
}
public void menuDeselected(MenuEvent evt) {
}
public void menuCanceled(MenuEvent evt) {
}
}
Text.java
package com.hb.Editorface;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/** This class shows an example of how to use the spell checking capability
* on a JTextComponent.
*
* @author Robert Gustavsson (robert@lindesign.se)
*/
public class Text extends JFrame {
JButton spell = null;
MYTextPane text=null;
public Text(String dictPath, String phonetPath) {
// INIT GUI
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
initGUI(dictPath,phonetPath);
this.pack();
this.setVisible(true);
}
private void initGUI(String dictPath, String phonetPath) {
Container frame = this.getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
frame.setLayout(gridbag);
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 1.0;
c.weighty = 1.0;
text = new MYTextPane(dictPath,phonetPath);
addToFrame(frame, new JScrollPane(text), gridbag, c, 0, 1, 1, 1);
JMenuBar menuBar=new JMenuBar();
JMenu menu=null;
int menuCount=0;
Action[] actions=text.getEditorKit().getActions();
for(int i=0;i<actions.length;i++){
if(i%20==0){
menu=new JMenu("Actions "+menuCount);
menuCount++;
menuBar.add(menu);
}
menu.add(actions[i]);
}
this.setJMenuBar(menuBar);
}
// Helps build gridbaglayout.
private void addToFrame(Container f, Component c, GridBagLayout gbl, GridBagConstraints gbc, int x, int y, int w, int h) {
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
gbl.setConstraints(c, gbc);
f.add(c);
}
public static void main(String[] args) {
String dictPath=null,
phonetPath=null;
if(args.length>0)
dictPath=args[0];
if(args.length>1)
phonetPath=args[1];
new Text(dictPath,phonetPath);
}
}
MYMenu.java
package com.hb.Editorface;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class MYMenu{
public MYMenu(){}
public JMenuItem makeMenuItem(Object item, Object target)
{
JMenuItem r = null;
if (item instanceof String)
r = new JMenuItem((String)item);
else if (item instanceof JMenuItem)
r = (JMenuItem)item;
else return null;
if (target instanceof ActionListener)
r.addActionListener((ActionListener)target);
return r;
}
public JPopupMenu makePopupMenu(Object[] items, Object target)
{
JPopupMenu m = new JPopupMenu();
for (int i = 0; i < items.length; i++) {
if (items[i] == null)
m.addSeparator();
else
m.add(makeMenuItem(items[i], target));
}
return m;
}
}
截图如下: