📄 wordcheck.java
字号:
// showLabel.setText("~单词错误,备选词过多,选择该词后点右键,获得更多信息~"); // } // if ( !found ) { // showallLabel.setText("词库中无法找到与之类似的词"); // } }// end else }// end try catch (BadLocationException ed) { System.out.println(ed); }// end catch } }// end try mouse catch (Exception ex) { System.out.println(e); }// end catch mouse }// END IF GETBUTTON if (e.getButton() == 3 && is_error == true) {// 右键点击 ItemHandler handler = new ItemHandler(); fixwordJPop = new JPopupMenu(); popItems = new JRadioButtonMenuItem[foundwords.size()]; for (int i = 0; i < foundwords.size(); i++) { popItems[i] = new JRadioButtonMenuItem(foundwords.get(i).toString()); fixwordJPop.add(popItems[i]); popItems[i].addActionListener(handler); } fixwordJPop.show(e.getComponent(), e.getX(), e.getY()); }// END IF 右键 }// end mouseClicked public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } // **************** 鼠标事件监听结束 ************************************** // //////////////////////////////////////////////////////////////////////// // **************** 光标位置监听 ****************************************** public void caretUpdate(CaretEvent e) { } // **************** 光标位置监听结束 ************************************** // //////////////////////////////////////////////////////////////////////// // **************** Check方法 ********************************************* public int Check(String aWord) { locate_word(aWord); int c = char_locate[0]; while (c <= char_locate[1]) { if (dict.get(c).toString().equalsIgnoreCase(aWord)) { found = true; return 1; } c++; }// end while return 0; }// ************* end Check() ************************************ // /////////////////////////////////////////////////////////////// // ***************** int CheckDel(String) 方法******************** public int CheckDel(String aWord) { String old; String t; int de = 1; int c = 0; while (c < dict.size()) { while (de + 1 < aWord.length()) { if (de == 1 && aWord.charAt(de) == aWord.charAt(de + 1)) ; else if (aWord.charAt(de) == aWord.charAt(de - 1)) { de++; continue; } t = new String(aWord.substring(0, de) + aWord.substring(de + 1, aWord.length())); if (t.equalsIgnoreCase(dict.get(c).toString())) { foundwords.addElement(dict.get(c).toString());// **************把temp加入vector found = true; ready_count++; old = new String(showallLabel.getText()); showallLabel.setText(old + "→" + t); }// end if de++; }// end changing_while de = 1; c++;// c继续指向下一行 }// end while return 0; }// ********************** end CheckDel()***************************** // /////////////////////////////////////////////////////////////////// // **************** CheckExChange方法 ******************************** public int CheckExChange(String aWord) { String old; String t; int ex = 0; int c = 0; while (c < dict.size()) { while (ex + 1 < aWord.length()) { if (aWord.charAt(ex) == aWord.charAt(ex + 1)) { ex++; continue; } // 交换相邻字符 t = new String(aWord.substring(0, ex) + aWord.substring(ex + 1, ex + 2) + aWord.substring(ex, ex + 1) + aWord.substring(ex + 2, aWord.length())); if (t.equalsIgnoreCase(dict.get(c).toString())) { foundwords.addElement(dict.get(c).toString());// **************把temp加入vector found = true; ready_count++; old = new String(showallLabel.getText()); showallLabel.setText(old + "→" + t); }// end if ex++; }// end changing_while ex = 0; c++;// temp继续指向下一行 }// end while return 0; }// ************* end CheckExChange() *********************************** // ////////////////////////////////////////////////////////////////////// // ***************** int CheckInsert(String) 方法************************ public int CheckInsert(String aWord) { char ins = 'a'; String old; String t; int in = 0; int c = 0; while (c < dict.size()) { while (in < aWord.length()) { while (ins <= 'z') { t = new String(aWord.substring(0, in) + (ins++) + aWord.substring(in, aWord.length())); if (t.equalsIgnoreCase(dict.get(c).toString())) { foundwords.addElement(dict.get(c).toString());// **************把temp加入vector found = true; ready_count++; old = new String(showallLabel.getText()); showallLabel.setText(old + "→" + t); }// end if }// end while( ins ) ins = 'a'; in++; }// end changing_while in = 0; c++; }// end while return 0; }// ********************** end CheckInsert()***************************** // /////////////////////////////////////////////////////////////////////// // ***************** int CheckReplace(String) 方法************************ public int CheckReplace(String aWord) { int re = 0; char ins = 'a'; String old; String t; int c = 0; while (c < dict.size()) { while (re < aWord.length()) { while (ins <= 'z') { t = new String(aWord.substring(0, re) + (ins++) + aWord.substring(re + 1, aWord.length())); if (t.equalsIgnoreCase(dict.get(c).toString())) { foundwords.addElement(dict.get(c).toString());// **************把temp加入vector found = true; ready_count++; old = new String(showallLabel.getText()); showallLabel.setText(old + "→" + t); }// end if }// end while( ins ) ins = 'a'; re++; }// end changing_while re = 0; c++; }// end while return 0; }// ********************** end CheckReplace()***************************** public void locate_word(String aWord) {// 由单词的首字母限制该词在词典中的查找范围 if (aWord.charAt(0) == 'a' || aWord.charAt(0) == 'A') char_locate = new int[] { 27 - 26, locate[0] - 26 }; else if (aWord.charAt(0) == 'b' || aWord.charAt(0) == 'B') char_locate = new int[] { locate[0] + 1 - 26, locate[1] - 26 }; else if (aWord.charAt(0) == 'c' || aWord.charAt(0) == 'C') char_locate = new int[] { locate[1] + 1 - 26, locate[2] - 26 }; else if (aWord.charAt(0) == 'd' || aWord.charAt(0) == 'D') char_locate = new int[] { locate[2] + 1 - 26, locate[3] - 26 }; else if (aWord.charAt(0) == 'e' || aWord.charAt(0) == 'E') char_locate = new int[] { locate[3] + 1 - 26, locate[4] - 26 }; else if (aWord.charAt(0) == 'f' || aWord.charAt(0) == 'F') char_locate = new int[] { locate[4] + 1 - 26, locate[5] - 26 }; else if (aWord.charAt(0) == 'g' || aWord.charAt(0) == 'G') char_locate = new int[] { locate[5] + 1 - 26, locate[6] - 26 }; else if (aWord.charAt(0) == 'h' || aWord.charAt(0) == 'H') char_locate = new int[] { locate[6] + 1 - 26, locate[7] - 26 }; else if (aWord.charAt(0) == 'i' || aWord.charAt(0) == 'I') char_locate = new int[] { locate[7] + 1 - 26, locate[8] - 26 }; else if (aWord.charAt(0) == 'j' || aWord.charAt(0) == 'J') char_locate = new int[] { locate[8] + 1 - 26, locate[9] - 26 }; else if (aWord.charAt(0) == 'k' || aWord.charAt(0) == 'K') char_locate = new int[] { locate[9] + 1 - 26, locate[10] - 26 }; else if (aWord.charAt(0) == 'l' || aWord.charAt(0) == 'L') char_locate = new int[] { locate[10] + 1 - 26, locate[11] - 26 }; else if (aWord.charAt(0) == 'm' || aWord.charAt(0) == 'M') char_locate = new int[] { locate[11] + 1 - 26, locate[12] - 26 }; else if (aWord.charAt(0) == 'n' || aWord.charAt(0) == 'N') char_locate = new int[] { locate[12] + 1 - 26, locate[13] - 26 }; else if (aWord.charAt(0) == 'o' || aWord.charAt(0) == 'O') char_locate = new int[] { locate[13] + 1 - 26, locate[14] - 26 }; else if (aWord.charAt(0) == 'p' || aWord.charAt(0) == 'P') char_locate = new int[] { locate[14] + 1 - 26, locate[15] - 26 }; else if (aWord.charAt(0) == 'q' || aWord.charAt(0) == 'Q') char_locate = new int[] { locate[15] + 1 - 26, locate[16] - 26 }; else if (aWord.charAt(0) == 'r' || aWord.charAt(0) == 'R') char_locate = new int[] { locate[16] + 1 - 26, locate[17] - 26 }; else if (aWord.charAt(0) == 's' || aWord.charAt(0) == 'S') char_locate = new int[] { locate[17] + 1 - 26, locate[18] - 26 }; else if (aWord.charAt(0) == 't' || aWord.charAt(0) == 'T') char_locate = new int[] { locate[18] + 1 - 26, locate[19] - 26 }; else if (aWord.charAt(0) == 'u' || aWord.charAt(0) == 'U') char_locate = new int[] { locate[19] + 1 - 26, locate[20] - 26 }; else if (aWord.charAt(0) == 'v' || aWord.charAt(0) == 'V') char_locate = new int[] { locate[20] + 1 - 26, locate[21] - 26 }; else if (aWord.charAt(0) == 'w' || aWord.charAt(0) == 'W') char_locate = new int[] { locate[21] + 1 - 26, locate[22] - 26 }; else if (aWord.charAt(0) == 'x' || aWord.charAt(0) == 'X') char_locate = new int[] { locate[22] + 1 - 26, locate[23] - 26 }; else if (aWord.charAt(0) == 'y' || aWord.charAt(0) == 'Y') char_locate = new int[] { locate[23] + 1 - 26, locate[24] - 26 }; else char_locate = new int[] { locate[24] + 1 - 26, locate[25] - 26 }; } public void checking(int b, int f) { try { // 调用 int CheckChange(String),断开某个字符,然后从字库匹配 CheckDel(editArea.getText(b, f)); // 调用 int CheckExChange(String),将相邻两个字符交换后,从字库匹配 CheckExChange(editArea.getText(b, f)); // 调用 int CheckInsert(String),往每两个字符之间依次插入字符后,从字库匹配 CheckInsert(editArea.getText(b, f)); // 调用 int CheckReplace(String),依次替换单词中的字符,然后匹配 CheckReplace(editArea.getText(b, f)); } catch (BadLocationException ed) { System.out.println(ed); } } // *************************************************************************************** // /////////////////////////////////////////////////////////////////////////////////////// // ************************ 线程操作开始 ********************************************** public class checkThread extends Thread { int bb; int ff; public checkThread(int b, int f) { bb = b; ff = f; } public void run() { found = false; foundwords.clear();// 初始化Vector showallLabel.setText(""); showLabel.setText("checking..."); ready_count = 0; checking(bb, ff); showLabel.setText("你想输入的是右边这些词吗"); if (ready_count > 6) { showallLabel.setText(""); showLabel.setText("~单词错误,备选词过多,选择该词后点右键,获得更多信息~"); } if (!found) { showallLabel.setText(""); showLabel.setText("词库中无法找到与之类似的词,要添加到词库请右键选择该词"); } } } // *********************** 线程操作结束 ******************************************}// ****************** end wordchecking ****************************************// ///////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////// **********************************************************************************public class WordCheck{ public static void main(String args[]) {// ************************** main 方法 ****************************************** wordchecking WC = new wordchecking("~有单词纠错功能的记事本~Beta v0.1~"); WC.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -