📄 spellchecker.java
字号:
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(SpellChecker.this,"文本编辑分析大师 V1.0\n华南农业大学02级计算机1班\n 101工作室制作\n组长:刘继忠 \n组员:陈炯远 何泽荣\n热诚欢迎您能对此软件提出宝贵意见!!!","关于",JOptionPane.INFORMATION_MESSAGE,new ImageIcon("images/about.png"));
}
});
menuItem.setMnemonic(KeyEvent.VK_A);
menu.add(menuItem);
}
private void stylechose(String str){
try {//设置窗口风格为WINDOWS风格
UIManager.setLookAndFeel(str);
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception ex){
JOptionPane.showMessageDialog(contentPane,
"the LookAndFeel class load failed!",
"发生异常",
JOptionPane.WARNING_MESSAGE
);
}
}
class startWindow extends Thread{
private JWindow startwin;
private JLabel imgLabel;
private JLabel textLabel;
public startWindow(){
startwin=new JWindow(SpellChecker.this);
Container windowcontainer= startwin.getContentPane();
//startwin.getContentPane().
imgLabel=new JLabel();
imgLabel.setIcon(new ImageIcon("images/start.jpg"));
windowcontainer.add(imgLabel,BorderLayout.CENTER);
textLabel=new JLabel("正在初始化运行环境...");
textLabel.setHorizontalAlignment(JLabel.CENTER);
windowcontainer.add(textLabel,BorderLayout.SOUTH);
startwin.setSize(420,334);
startwin.setLocation(160,120);
startwin.getContentPane().setBackground(Color.white);
startwin.setVisible(true);
}
public synchronized void run(){
this.suspend();
textLabel.setText("正在读取词典文件...");
this.suspend();
textLabel.setText("正在初始化工作区...");
this.suspend();
textLabel.setText("正在载入源文件...");
this.suspend();
textLabel.setText("正在分析工作区文件...");
this.suspend();
textLabel.setText("正在启动主界面窗口...");
this.suspend();
startwin.setVisible(false);
this.startwin.dispose();
this.stop();
}
}
class SpellMenuListener implements ActionListener{
public void actionPerformed(ActionEvent actionevent){
if(actionevent.getActionCommand().equals("标记错误单词")){
String text=null;
try{
String textTemp=styledpad.getEditor().getDocument().getText(0, styledpad.getEditor().getDocument().getLength());//获取整个文本中的字符
text=textTemp.concat(" ");
}
catch(BadLocationException ex){
System.out.println("获取整个文本产成异常!\n"+ex.getClass()+"\n"+ex.getMessage());
}
CheckPorcessor checkProcThread=new CheckPorcessor(text,wrongwordlog,styledpad.getEditor().getDocument(),statusbar,table,dirProc);
checkProcThread.start(); //启动文本分析线程
}
if(actionevent.getActionCommand().equals("错误智能更正")){
String text=null;
try{
String textTemp=styledpad.getEditor().getDocument().getText(0, styledpad.getEditor().getDocument().getLength());//获取刚修改的文本中的字符
text=textTemp.concat(" ");
}
catch(BadLocationException ex){
System.out.println("获取整个文本产成异常!\n"+ex.getClass()+"\n"+ex.getMessage());
}
cortDlg=new CorrecterDlg(styledpad.getEditor().getDocument(),SpellChecker.this);
correctProcThread=new CheckPorcessor(text,styledpad.getEditor().getDocument(),table,cortDlg,dirProc);
cortDlg.getDepdendThread(correctProcThread);
correctProcThread.start(); //启动文本分析线程
}
}
}
SpellMenuListener spellmenuListener=new SpellMenuListener();
class StyleListener implements ActionListener{
String str1 = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
String str2 = "javax.swing.plaf.metal.MetalLookAndFeel";
String str3 = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="Windows") stylechose(str1);
if(e.getActionCommand()=="Metal") stylechose(str2);
if(e.getActionCommand()=="Motif") stylechose(str3);
}
}
private void srcFileLoader(File file){
if (file.isFile() && file.canRead()) {
if(styledpad.getEditor().getDocument() != null)
styledpad.getEditor().getDocument().removeUndoableEditListener(styledpad.undoHandler);
styledpad.getEditor().setDocument( new DefaultStyledDocument());
FileLoaderProc(file, styledpad.getEditor().getDocument());
}
else {
System.out.println("源文件载入失败!");
}
}
private void srcFileLoader(){
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
int retval = chooser.showOpenDialog(SpellChecker.this);
if (retval != JFileChooser.APPROVE_OPTION)
return;
File f = chooser.getSelectedFile();
if (f.isFile() && f.canRead()) {
srcfile=f;
if(styledpad.getEditor().getDocument() != null)
styledpad.getEditor().getDocument().removeUndoableEditListener(styledpad.undoHandler);
styledpad.getEditor().setDocument( new DefaultStyledDocument());
FileLoaderProc(f, styledpad.getEditor().getDocument());
}
else {
JOptionPane.showMessageDialog(SpellChecker.this,
"Could not open file: " + f,
"Error opening file",
JOptionPane.ERROR_MESSAGE);
}
}
private void FileLoaderProc(File f, Document doc) {
try {
// try to start reading
Reader in = new FileReader(f);
char[] buff = new char[4096];
int charsLength;
while ((charsLength = in.read(buff, 0, buff.length)) != -1) {
doc.insertString(doc.getLength(), new String(buff, 0, charsLength), null);
}
//每当文件打开就创建文档的监听器
styledpad.getEditor().getDocument().addDocumentListener(new DocumentListener(){
public void insertUpdate(DocumentEvent event){
isDocumentModified=true;
isChecked=false;
}
public void removeUpdate(DocumentEvent event){
isDocumentModified=true;
isChecked=false;
}
public void changedUpdate(DocumentEvent event){
isDocumentModified=true;
}
});
isDocumentModified=false;//新打开的文档将设为没修改
}
catch (IOException e) {
final String msg = e.getMessage();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(SpellChecker.this,
"Could not open file: " + msg,
"Error opening file",
JOptionPane.ERROR_MESSAGE);
}
});
}
catch (BadLocationException e) {
System.err.println(e.getMessage());
}
doc.addUndoableEditListener(styledpad.undoHandler);
styledpad.resetUndoManager();
}
private void FileSaver(File file){
FileSaverProc(file, styledpad.getEditor().getDocument());
}
private void FileSaver(){
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
int retval = chooser.showSaveDialog(SpellChecker.this);
if (retval != JFileChooser.APPROVE_OPTION) {
return;
}
File f = chooser.getSelectedFile();
FileSaverProc(f, styledpad.getEditor().getDocument());
}
private void FileSaverProc(File file, Document doc) {
try {
// start writing
FileWriter out = new FileWriter(file);
Segment text = new Segment();
text.setPartialReturn(true);//Flag to indicate that partial returns are valid.
int charsLeft = doc.getLength();
int offset = 0;
while (charsLeft > 0) {
doc.getText(offset, Math.min(4096, charsLeft), text);
out.write(text.array, text.offset, text.count);
charsLeft -= text.count;
offset += text.count;
try {
Thread.sleep(99);//100ms
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
out.flush();
out.close();
}
catch (IOException e) {
final String msg = e.getMessage();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(SpellChecker.this,
"无法完成文件保存: " + msg,
"文件保存错误",
JOptionPane.ERROR_MESSAGE);
}
});
}
catch (BadLocationException e) {
System.err.println(e.getMessage());
}
Toolkit.getDefaultToolkit().beep();
}
private void logFileSaver(File file){
FileWriter fw=null;
try{fw=new FileWriter(file);}
catch(IOException ex){
System.out.println(ex.getClass()+ex.getMessage());
}
wrongwordlog.trimToSize();//将vector的容量调整为其实际所存元素的数量
try{fw.write("源文件名:"+srcfile.getName()+" ,分析使用词典名:"+directoryfile.getName());}
catch(IOException ex){
System.out.println(ex.getClass()+ex.getMessage());
}
for(int i=0;i<wrongwordlog.capacity();i+=2){
try{
fw.write("\r\n");
fw.write(wrongwordlog.get(i).toString());
fw.write(" ");
fw.write(wrongwordlog.get(i+1).toString());
}
catch(IOException ex){
System.out.println(ex.getClass()+ex.getMessage());
}
}
try{
fw.close();
}
catch(IOException ex){System.out.println(ex.getClass()+ex.getMessage());}
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if(!isChecked&&isNeedAutoCheck){
String text=null;
try{
String textTemp=styledpad.getEditor().getDocument().getText(0, styledpad.getEditor().getDocument().getLength());//获取刚修改的文本中的字符
text=textTemp.concat(" ");
}
catch(BadLocationException ex){
System.out.println("获取字符产成异常!\n"+ex.getClass()+"\n"+ex.getMessage());
}
CheckPorcessor AutoCheckProcThread=new CheckPorcessor(text,styledpad.getEditor().getDocument(),table,dirProc);
AutoCheckProcThread.start();
isChecked=true;
}
}
}
class StatusBar extends JPanel {
public StatusBar() {
super();
this.setPreferredSize(new Dimension(835,33));
this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED ) );
}
}
private final String directoryPath=new String("zd.txt");
private final String defaultTxt=new String("SrcFile\\src.txt");
private final int TIME_TRIGGER_SPAN=10000;
private boolean isChecked;
private boolean isNeedAutoCheck;
private boolean isDocumentModified;
private Color ForeGroundColor;
private Vector wrongwordlog;
//private CheckPorcessor AutoCheckProcThread;
private DictionaryProcessor dirProc;
private File srcfile;
private File directoryfile;
private javax.swing.Timer timer;
private JButton toolbutton;
private JComponent statusbar;
private JCheckBoxMenuItem cbMenuItem_auto;
private JCheckBoxMenuItem cbMenuItem_show;
private JPanel text_tablePanel;
private JPopupMenu popupmenu;
private JMenuItem colorcustome;
private JMenuItem undomenuItem,redomenuItem;
private JScrollPane scroller_text;
private JScrollPane scroller_table;
private StringBuffer strBuf;
private JTable table;
private JToolBar toolbar;
private Container contentPane;
private StyledPad styledpad;
private CorrecterDlg cortDlg;
private ShowJLabelThread showlabelThread;
private CheckPorcessor correctProcThread;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -