⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wordcheck.java

📁 检查单词拼写的小程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.edit;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.Vector;import javax.swing.JColorChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JRadioButtonMenuItem;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.event.CaretEvent;import javax.swing.event.CaretListener;import javax.swing.text.BadLocationException;class wordchecking extends JFrame implements ActionListener, KeyListener, MouseListener, MouseMotionListener,		CaretListener{	private JMenuBar bar;	private JMenu fileMenu, helpMenu, fontsetMenu;	private JMenuItem saveItem, exitItem, aboutItem, colorItem;	private JPopupMenu fixwordJPop;	private JPanel editPanel, labelPanel;	private JRadioButtonMenuItem popItems[];	private JTextArea editArea;	private JLabel showLabel, showallLabel;	private int countb = 0, countf = 0;	private Vector foundwords = new Vector();	private Vector dict = new Vector();	private int locate[] = new int[26];	private int char_locate[] = new int[2];	private int ready_count = 0, error_count = 0;// ready_count记录侯选词个数,error_count记录错误词个数	private Color c = new Color(0, 0, 0); // 字体颜色	private String nochar = " ,./;'[]\\`1234567890-=<>?:\"{}|~!@#$%^&*()_\n+\"";	private boolean found = false;	private boolean is_error = true;// 当前光标所选字母是否有错	private checkThread thread1;	wordchecking(String s)	{		// *********** wordchecking的构造方法 ******************		super(s);		fileMenu = new JMenu("文件(F)");		fileMenu.setMnemonic('F');		fontsetMenu = new JMenu("字体设定(T)");		fontsetMenu.setMnemonic('T');		helpMenu = new JMenu("帮助(H)");		helpMenu.setMnemonic('H');		saveItem = new JMenuItem("保存 Alt+S");		saveItem.setMnemonic('S');		fileMenu.add(saveItem);		exitItem = new JMenuItem("退出 Alt+X");		exitItem.setMnemonic('X');		fileMenu.add(exitItem);		aboutItem = new JMenuItem("关于");		helpMenu.add(aboutItem);		colorItem = new JMenuItem("文字颜色 Alt+C");		colorItem.setMnemonic('C');		fontsetMenu.addSeparator();		fontsetMenu.add(colorItem);		bar = new JMenuBar();		setJMenuBar(bar);		bar.add(fileMenu);		bar.add(fontsetMenu);		bar.add(helpMenu);		editPanel = new JPanel();		editArea = new JTextArea(10, 40);		editArea.setLineWrap(true);		editPanel.add(editArea);		editPanel.add(new JScrollPane(editArea));		labelPanel = new JPanel();		showLabel = new JLabel();		showallLabel = new JLabel();		showallLabel.setText("");		labelPanel.add(showLabel, BorderLayout.WEST);		labelPanel.add(showallLabel, BorderLayout.EAST);		Container con = getContentPane();		con.setLayout(new BorderLayout());		con.add(editPanel, BorderLayout.CENTER);		con.add(labelPanel, BorderLayout.SOUTH);		setSize(470, 280);		setVisible(true);		setResizable(false);		// 各组事件监听		exitItem.addActionListener(this);		colorItem.addActionListener(this);		aboutItem.addActionListener(this);		saveItem.addActionListener(this);		editArea.addKeyListener(this);		editArea.addMouseListener(this);		editArea.addMouseMotionListener(this);		editArea.addCaretListener(this);		try		{// ******* 将字典库载入到 dict 容器中 ******************			File read = new File("def.dict");			BufferedReader br = new BufferedReader(new FileReader(read));			String temp = null;			temp = br.readLine();			int num = 0;			for (int i = 0; i < 26; i++)			{// locat中读入字库中的定位信息				for (int j = 0; j < temp.length(); j++)				{// 转为数字					num += Math.pow(10, temp.length() - j - 1) * (int) (temp.charAt(j) - '0');				}				locate[i] = num;				num = 0;				temp = br.readLine();// temp继续指向下一行			}			while (temp != null)			{// dict中载入字库				dict.addElement(temp);				temp = br.readLine();// temp继续指向下一行			}// end while			br.close();		}		catch (FileNotFoundException e)		{ // 文件未找到			JOptionPane.showMessageDialog(null, "\n              无法找到字典文件def.dict" + "\n请在WordCheck.java的同目录下放入该文件",					"初始化失败", 1);		}		catch (IOException e)		{			JOptionPane.showMessageDialog(null, "\n无法打开字典文件def.dict", "IO错误", 1);		}	}// ******* wordchecking 构造方法结束 *******************************	// //////////////////////////////////////////////////////////////////	// **************** 菜单监听 ****************************************	private class ItemHandler implements ActionListener	{		public void actionPerformed(ActionEvent event)		{			for (int i = 0; i < popItems.length; i++)			{				if (event.getSource() == popItems[i])				{					String old = new String(editArea.getText());					int pos = editArea.getCaretPosition();					int start = pos + 1;					int li = start - 1, ri = start - 1;					while (nochar.indexOf(editArea.getText().charAt(li - 1)) == -1)					{						li--;						if (li == 0)						{							if ((nochar.indexOf(editArea.getText().charAt(li)) == -1))							{								li++;								break;							}							else break;						}					}// end while li					while (nochar.indexOf(editArea.getText().charAt(ri)) == -1)					{						ri++;						if (ri == editArea.getText().length())						{							if ((nochar.indexOf(editArea.getText().charAt(ri - 1)) == -1))							{								ri--;								break;							}							else break;						}					}// end while ri					editArea.setText(old.substring(0, li) + popItems[i].getText() + old.substring(ri, old.length()));				}// end if			}// end for		}// end actionPerformed	}	public void actionPerformed(ActionEvent e)	{		if (e.getSource() == exitItem)		{// 退出			System.exit(0);		}		if (e.getSource() == colorItem)		{// 调出JAVA调色版			Color newColor = JColorChooser.showDialog(this, "调色板", c);			c = newColor;		}		if (e.getSource() == aboutItem)		{			JOptionPane.showMessageDialog(null, "   ~有单词纠错功能的记事本~" + "\nCopyRight: 20041190020 刘智聪"					+ "\n              版本号Beta v0.1", "关于本程序", 1);		}		if (e.getSource() == saveItem)		{		}	}// /**************** 菜单监听结束 ****************************************	// ///////////////////////////////////////////////////////////////////////	// **************** 键盘事件监听 ******************************************	public void keyPressed(KeyEvent e)	{	}// end keyPressed	public void keyReleased(KeyEvent e)	{		if ((e.getKeyChar() <= 'z' && e.getKeyChar() >= 'a') || (e.getKeyChar() <= 'Z' && e.getKeyChar() >= 'A'))		{			showallLabel.setText("");			showLabel.setText("正在输入字母");		}		else if (nochar.indexOf(e.getKeyChar()) != -1)		{			int temp_count = editArea.getText().length() - 1;			int temp_count2 = temp_count;			while (nochar.indexOf(editArea.getText().charAt(temp_count - 1)) == -1)			{				temp_count--;				if (temp_count == 0)					break;			}// end while			if (temp_count != temp_count2)			{				temp_count++;				countb = temp_count - 1;				countf = temp_count2 - countb;				try				{					if (Check(editArea.getText(countb, countf)) == 1)					{						// 如果词库中有匹配的词						showallLabel.setText("");						showLabel.setText("已经在字库中找到:" + editArea.getText(countb, countf));					}// end if					else					{						// found = false;						// foundwords.clear();//初始化Vector						// showallLabel.setText("");						// showLabel.setText( "你想输入的是右边这些词吗");						// ready_count = 0 ;						thread1 = new checkThread(countb, countf);						thread1.start();						// checking( countb,countf );//************** 调用checking 查找备选词*****************						// if ( ready_count>6 ){						// showallLabel.setText("");						// showLabel.setText("~单词错误,备选词过多,选择该词后点右键,获得更多信息~");						// }						// if ( !found ) {						// showLabel.setText("词库中无法找到与之类似的词,要添加到词库请右键选择该词");						// }					}// end else				}// end try				catch (BadLocationException ed)				{					System.out.println(ed);				}// end catch			}// end if ( temp_count != temp_count2 )		}// end else if ( nochar.indexOf( e.getKeyChar() ) != -1 )	}// end keyReleased	public void keyTyped(KeyEvent e)	{	}	// **************** 键盘事件监听结束 **************************************	// ////////////////////////////////////////////////////////////////////////	// **************** 鼠标事件监听 ******************************************	public void mouseClicked(MouseEvent e)	{		if (e.getButton() == 1)		{// 如果点击左键			try			{				int pos = editArea.getCaretPosition();				int start = pos + 1;				int li = start - 1, ri = start - 1;				while (nochar.indexOf(editArea.getText().charAt(li - 1)) == -1)				{					li--;					if (li == 0)					{						if ((nochar.indexOf(editArea.getText().charAt(li)) == -1))						{							li++;							break;						}						else break;					}				}// end while li				while (nochar.indexOf(editArea.getText().charAt(ri)) == -1)				{					ri++;					if (ri == editArea.getText().length())					{						if ((nochar.indexOf(editArea.getText().charAt(ri - 1)) == -1))						{							ri--;							break;						}						else break;					}				}// end while ri				ri = ri - li;				if (ri != 0)				{					try					{						is_error = true;						if (Check(editArea.getText(li, ri)) == 1)						{							// 如果词库中有匹配的词							showallLabel.setText("");							showLabel.setText("已经在字库中找到:" + editArea.getText(li, ri));							is_error = false;						}// end if						else						{							// foundwords.clear();							// found = false;							// ready_count = 0;							// showallLabel.setText("");							thread1 = new checkThread(li, ri);							thread1.start();							// checking( li,ri);//************** 调用checking 查找备选词*****************							// if ( ready_count>6 ){							// showallLabel.setText("");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -