java记事本功能代码

近段时间周末一直在学java,前两天没事仿照Windows记事本自己写了个记事本。

把各部分功能贴上来,记录一下,以备自己以后使用。

 

控件

private void initComponents() {

		jFileChooser1 = new javax.swing.JFileChooser();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTextArea1 = new javax.swing.JTextArea();   //编辑区域
		jMenuBar1 = new javax.swing.JMenuBar();	//菜单部分
		jMenu1 = new javax.swing.JMenu();
		jNewText = new javax.swing.JMenuItem();
		jMenuItem1 = new javax.swing.JMenuItem();
		jMenuItemSave = new javax.swing.JMenuItem();
		jSeparator2 = new javax.swing.JSeparator();
		jMenuItem3 = new javax.swing.JMenuItem();
		jMenu2 = new javax.swing.JMenu();
		cutTextMenu = new javax.swing.JMenuItem();
		jMenuItem5 = new javax.swing.JMenuItem();
		jMenuItem4 = new javax.swing.JMenuItem();
		DeleteText = new javax.swing.JMenuItem();
		jSeparator1 = new javax.swing.JSeparator();
		selectAll = new javax.swing.JMenuItem();
		research = new javax.swing.JMenuItem();
		jMenu4 = new javax.swing.JMenu();
		jMenuItem2 = new javax.swing.JMenuItem();
		jChangeLine = new javax.swing.JCheckBoxMenuItem();
		jMenu3 = new javax.swing.JMenu();
		jMenuItem6 = new javax.swing.JMenuItem();
		jMenuItem7 = new javax.swing.JMenuItem();

/**
	 * 设置自动换行
	 **/
	private void jChangeLineMouseReleased(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
		if (resuleChangeLine) {
			jTextArea1.setLineWrap(true);
			resuleChangeLine = false;
		} else {
			jTextArea1.setLineWrap(false);
			resuleChangeLine = true;
		}
	}
	/**
	 * 删除功能
	 * */
	private void DeleteText_menu(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
		if (jTextArea1.getSelectedText() == null) {
			jTextArea1.setSelectionStart(jTextArea1.getCaretPosition() - 1);
			jTextArea1.setSelectionEnd(jTextArea1.getCaretPosition());
		}
		jTextArea1.replaceSelection("");
	}

/**
	 * 查找对话框
	 */
	private void researchMouseReleased(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
		final research rese = new research(null, false);
		rese.setModalityType(ModalityType.MODELESS);
		rese.setFocusableWindowState(true);
		rese.setVisible(true);
		rese.jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				rese.jButton1MouseClicked(evt);
				find(rese.getText(), rese.isCompareUpperLetter, findIndex,
						rese.isUpResearch);

			}
		});
		rese.jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				rese.jButton2MouseReleased(evt);
				rese.setVisible(false);
				findIndex = 0;
			}
		});
	}
/**
	 * 查找 内容
	 * */
	private void find(String str, boolean isCompareUpperLetter, int TextIndex,
			boolean isUpResearch) {
		boolean isfind = false;
		String Text = jTextArea1.getText();// 全文
		int length = str.length();
		char[] TextChar = Text.toCharArray();
		String Temp = "";
		if (isUpResearch) {
			for (int i = 0; i < findIndex - 1; i++) {
				Temp = new String(TextChar, i, length);
				if (isCompareUpperLetter) {// 区分大小写
					String TempLower = Temp.toLowerCase();
					String TempUpper = Temp.toUpperCase();
					if (TempLower.equals(str)) {
						setSelectText(i, i + length);
						findIndex = i;
						isfind = true;
						break;
					}
					if (TempUpper.equals(str)) {
						setSelectText(i, i + length);
						findIndex = i;
						isfind = true;
						break;
					}
				}
				if (Temp.equals(str)) {// 不区分大小写
					setSelectText(i, i + str.length());
					findIndex = i;
					isfind = true;
					break;
				}
			}
			if (!isfind) // 没有找到
				findIndex = 0;
		} else {
			for (int i = findIndex; i < Text.length() - str.length(); i++) {

				Temp = new String(TextChar, i, length);
				if (isCompareUpperLetter) {// 区分大小写
					String TempLower = Temp.toLowerCase();
					String TempUpper = Temp.toUpperCase();
					if (TempLower.equals(str)) {
						setSelectText(i, i + length);
						findIndex = i + str.length();
						isfind = true;
						break;
					}
					if (TempUpper.equals(str)) {
						setSelectText(i, i + length);
						findIndex = i + str.length();
						isfind = true;
						break;
					}
				}
				if (Temp.equals(str)) {// 不区分大小写
					setSelectText(i, i + str.length());
					findIndex = i + str.length();
					isfind = true;
					break;
				}
			}
			if (!isfind) // 没有找到
				findIndex = 0;
		}
	}

	private void setSelectText(int start, int end) {

		jTextArea1.setSelectionStart(start);
		jTextArea1.setSelectionEnd(end);

	}

/**
	 * 新建功能
	 * */
	private void jNewTextMouseReleased(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
		setText("");
		this.setTitle("UnsaveText");
	}

	/**
	 * 选择字体
	 */

	private void jMenuItem2MouseClicked(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:

		FontsChooser fonts = new FontsChooser(null, true);
		fonts.setVisible(true);
		if (fonts.resule)
			jTextArea1.setFont(fonts.getfont());
	}

	/**
	 * 删除字符操作
	 */
	private void DeleteTextMouseClicked(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
		if (jTextArea1.getSelectedText().equalsIgnoreCase(""))
			jTextArea1.replaceSelection("");

	}

	/**
	 * 对复制内容进行粘贴 *
	 */
	private void jMenuItem4MouseReleased(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
		jTextArea1.paste();
	}

	/**
	 * 全选功能
	 */
	private void selectAllMouseReleased(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
		jTextArea1.selectAll();
	}

	private void jMenuItem4MouseClicked(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:

	}

	/**
	 * 对选中内容继续复制
	 */
	private void jMenuItem5MouseReleased(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
		jTextArea1.copy();
	}
/**
	 * 保存至.txt
	 * */
	private void saveText() throws IOException { 
		savaDialogShow();
		BufferedWriter bw = null;
		String[] strTextSave = getText().split("\n");// 将字符串分成原文换行,进行保存
		
		if(pathSaveAddress.charAt(pathSaveAddress.length()-4)!='.')
			pathSaveAddress=pathSaveAddress+".txt"; 
		bw = new BufferedWriter(new FileWriter(pathSaveAddress));
		for (int i = 0; i < strTextSave.length; i++) {
			bw.write(strTextSave[i]);
			bw.newLine();
			bw.flush();
		}
	}

	/**
	 * 保存对话框
	 */

	private void savaDialogShow() {
		jFileChooser2.setAcceptAllFileFilterUsed(false);
		jFileChooser2.addChoosableFileFilter(new TextFileFilter("txt"));
		int result = jFileChooser2.showSaveDialog(null);
		if (result == JFileChooser.APPROVE_OPTION) {
			pathSaveAddress = jFileChooser2.getSelectedFile().getAbsolutePath();
		}
	}

	/**
	 * 读取目标文本
	 **/
	private void openText() throws IOException {
		openDialogShow();

		if (!pathOpenAddress.equalsIgnoreCase("")) {// 判断地址不为空
			BufferedReader fw = null;
			String tempstr = "";
			StringBuffer strArray = new StringBuffer("");
			fw = new BufferedReader(new FileReader(pathOpenAddress));
			while ((tempstr = fw.readLine()) != null) {// 从目标文件中读取字符
				strArray.append(tempstr);
				strArray.append('\r');
				strArray.append('\n');
			}
			setText(strArray.toString());
		} else {
			JOptionPane.showMessageDialog(null, "打开失败,请重试。");
		}

	}

	/**
	 * 调用打开对话框
	 */
	private void openDialogShow() {
		jFileChooser1.setAcceptAllFileFilterUsed(false);
		jFileChooser1.addChoosableFileFilter(new TextFileFilter("txt"));
		int result = jFileChooser1.showOpenDialog(null);
		if (result == JFileChooser.APPROVE_OPTION) {
			pathOpenAddress = jFileChooser1.getSelectedFile().getAbsolutePath();
		}
		setTitle(pathOpenAddress);

	}


你可能感兴趣的:(java)