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

📄 bookeditor.java

📁 本程序用Java描述了一个类似windows自带的记事本的功能
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
						currentFile = saveFileName;
						oldValue = Text.getText();
						this.setTitle(saveFileName.getName() + "  - 记事本");
						statusLabel.setText(" 当前打开文件:" + saveFileName.getAbsoluteFile());
					}
				} else if (saveChoose == JOptionPane.NO_OPTION) {
					String str = null;
					JFileChooser fileChooser = new JFileChooser();    //定义文件选择框
					fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
					fileChooser.setApproveButtonText("确定");
					fileChooser.setDialogTitle("打开文件");
					int result = fileChooser.showOpenDialog(this);
					if (result == JFileChooser.CANCEL_OPTION) {
						statusLabel.setText("您没有选择任何文件");
						return;
					}
					fileName = fileChooser.getSelectedFile();
					if (fileName == null || fileName.getName().equals(""))
						JOptionPane.showMessageDialog(this, "不合法的文件名", "不合法的文件名", JOptionPane.ERROR_MESSAGE);
					else {
						try {
							FileReader fr = new FileReader(fileName);
							BufferedReader bfr = new BufferedReader(fr);
							Text.setText("");
							while ((str = bfr.readLine()) != null) {   // 每次读取一行,直到文件结束
								Text.append(str + "\n");                //读完一行后添加换行,保证文本正常显示
							}
							this.setTitle(fileName.getName() + "  - 记事本");
							statusLabel.setText(" 当前打开文件:" + fileName.getAbsoluteFile());
							fr.close();
							isNewFile = false;                           //判断是否更改文件未保存设置的修改
							currentFile = fileName;
							oldValue = Text.getText();
						} catch (IOException io) {}
					}
				} else {
					return;
				}
			}

			else {
				String str = null;
				JFileChooser fileChooser = new JFileChooser();    //定义文件选择框
				fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				fileChooser.setApproveButtonText("确定");
				fileChooser.setDialogTitle("打开文件");
				int result = fileChooser.showOpenDialog(this);
				if (result == JFileChooser.CANCEL_OPTION) {
					statusLabel.setText(" 您没有选择任何文件");
					return;
				}
				fileName = fileChooser.getSelectedFile();
				if (fileName == null || fileName.getName().equals(""))
					JOptionPane.showMessageDialog(this, "不合法的文件名", "不合法的文件名", JOptionPane.ERROR_MESSAGE);
				else {
					try {
						FileReader fr = new FileReader(fileName);
						BufferedReader bfr = new BufferedReader(fr);
						Text.setText("");
						while ((str = bfr.readLine()) != null) {  // 每次读取一行,直到文件结束
							Text.append(str + "\n");               //读完一行后添加换行,保证文本正常显示
						}

						this.setTitle(fileName.getName() + "  - 记事本");
						statusLabel.setText(" 当前打开文件:" + fileName.getAbsoluteFile());
						fr.close();
						isNewFile = false;                              //判断是否更改文件未保存设置的修改
						currentFile = fileName;
						oldValue = Text.getText();
					} catch (IOException io) {
					}
				}

			}
			Text.setCaretPosition(0);                 //控制输入文本时将首先显示在文本初始位置
		}

		// 保存
		else if (e.getSource() == mFile_Save){
			Text.requestFocus();
			if (isNewFile) {                                        //判断是否更改文件未保存
				JFileChooser fileChooser = new JFileChooser();    //定义文件选择框
				fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				fileChooser.setApproveButtonText("确定");
				fileChooser.setDialogTitle("另存为");
				int result = fileChooser.showSaveDialog(this);
				if (result == JFileChooser.CANCEL_OPTION) {
					statusLabel.setText(" 您没有选择任何文件");
					return;
				}
				saveFileName = fileChooser.getSelectedFile();
				if (saveFileName == null || saveFileName.getName().equals(""))
					JOptionPane.showMessageDialog(this, "不合法的文件名", "不合法的文件名", JOptionPane.ERROR_MESSAGE);
				else {
					saveFile();                                           //调用保存方法
					isNewFile = false;                           //判断是否更改文件未保存设置的修改
					currentFile = saveFileName;
					oldValue = Text.getText();
					this.setTitle(saveFileName.getName() + "  - 记事本");
					statusLabel.setText(" 当前打开文件:" + saveFileName.getAbsoluteFile());
				}
			} else {
				try {
					FileWriter fw = new FileWriter(currentFile);
					BufferedWriter bfw = new BufferedWriter(fw);
					bfw.write(Text.getText(), 0, Text.getText().length());
					bfw.flush();
					fw.close();
				} catch (IOException io) {}
			}
		}

		// 另存为
		else if (e.getSource() == mFile_ASave){
			Text.requestFocus();
			JFileChooser fileChooser = new JFileChooser();    //定义文件选择框
			fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
			fileChooser.setApproveButtonText("确定");
			fileChooser.setDialogTitle("另存为");
			int result = fileChooser.showSaveDialog(this);
			if (result == JFileChooser.CANCEL_OPTION) {
				statusLabel.setText(" 您没有选择任何文件");
				return;
			}
			saveFileName = fileChooser.getSelectedFile();
			if (saveFileName == null || saveFileName.getName().equals(""))
				JOptionPane.showMessageDialog(this, "不合法的文件名", "不合法的文件名", JOptionPane.ERROR_MESSAGE);
			else {
				saveFile();                                             //调用保存方法
				isNewFile = false;                                    //判断是否更改文件未保存设置的修改
				currentFile = saveFileName;
				oldValue = Text.getText();
				this.setTitle(saveFileName.getName() + "  - 记事本");
				statusLabel.setText(" 当前打开文件:" + saveFileName.getAbsoluteFile());
			}

		}

		// 退出
		else if (e.getSource() == mFile_Exit) {
			int exitChoose = JOptionPane.showConfirmDialog(this, "确定要退出么?", "退出提示", JOptionPane.OK_CANCEL_OPTION);
			if (exitChoose == JOptionPane.OK_OPTION) {
				checkText();                                  //推出前判断文本是否改动后未保存
			} else {
				return;
			}
		}

		// 撤消
		else if (e.getSource() == mEdit_Undo || e.getSource() == rightclickMenu_Undo){
			Text.requestFocus();
			if (undo.canUndo()) {                      //判断是否可撤销
				try {
					undo.undo();                       //撤销操作
				} catch (CannotUndoException cue) {}
				if (!undo.canUndo()) {
					mEdit_Undo.setEnabled(false);      //若不可撤销设按键不可用
					rightclickMenu_Undo.setEnabled(false);
				}
			}
		}

		// 剪切
		else if (e.getSource() == mEdit_Cut || e.getSource() == rightclickMenu_Cut){
			Text.requestFocus();
			String text = Text.getSelectedText();
			StringSelection selection = new StringSelection(text);
			clipBoard.setContents(selection, null);          //复制该段被选部分到剪切板
			Text.replaceRange("", Text.getSelectionStart(), Text.getSelectionEnd()); //若是剪切,则清空该选中部分
			checkMenuItemEnabled();                           //调用方法设置剪切、复制、粘贴等功能的可用性
		}

		// 复制
		else if (e.getSource() == mEdit_Copy || e.getSource() == rightclickMenu_Copy){
			Text.requestFocus();
			String text = Text.getSelectedText();
			StringSelection selection = new StringSelection(text);
			clipBoard.setContents(selection, null);          //复制该段被选部分到剪切板
			checkMenuItemEnabled();                           //调用方法设置剪切、复制、粘贴等功能的可用性
		}

		// 粘贴
		else if (e.getSource() == mEdit_Paste || e.getSource() == rightclickMenu_Paste){
			Text.requestFocus();
			Transferable contents = clipBoard.getContents(this);     
			if (contents == null)
				return;
			String text;
			text = "";

			try {
				text = (String)contents.getTransferData(DataFlavor.stringFlavor);
			} catch (Exception exception) {
			}

			Text.replaceRange(text, Text.getSelectionStart(), Text.getSelectionEnd());
			checkMenuItemEnabled();                        //调用方法设置剪切、复制、粘贴等功能的可用性
		}
		
		// 查找与替换
		else if (e.getSource() == mEdit_Search){
			Text.requestFocus();
			searchAndreplace();
		}

		// 跳转到
		else if (e.getSource() == mEdit_Turnto) {
			final JDialog gotoDialog = new JDialog(this, "转到下列行");
			JLabel gotoLabel = new JLabel("行数:");
			final JTextField linenum = new JTextField(8);
			linenum.setText("1");
			linenum.selectAll();
			
			JButton okButton = new JButton("确定");           //点击确定后添加其响应
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int totalLine = Text.getLineCount();     //定义文本区中所包含的行数
					int[] lineNumber = new int[totalLine + 1]; //定义每行字符数的记录器
					int pos = 0, t = 0;

					while (true) {
						pos = Text.getText().indexOf('\n', pos); //每次找到换行符,若已全部遍历则返回-1
						if (pos == -1)                           //根据pos判断是否已全部遍历
							break;
						lineNumber[t++] = pos++;                //因为每次都到换行为止,所以要加上换行符
					}
					int gt = 1;
					try {
						gt = Integer.parseInt(linenum.getText());  //获取文本框为整型数并根据try,catch判断其真假
					} catch (NumberFormatException nfe) {
						JOptionPane.showMessageDialog(null, "行数不能为空!", "错误提示", JOptionPane.WARNING_MESSAGE);
						linenum.requestFocus(true);                //聚焦回行数输入框
						return;                                   //回到行数输入控件
					}

					if (gt < 2 || gt >= totalLine) {
						if (gt < 2)
							Text.setCaretPosition(0);             //设置光标位置
						else
							Text.setCaretPosition(Text.getText().length());
					} 
					else
						Text.setCaretPosition(lineNumber[gt - 2] + 1); //根据每个数组都记录的该行所在位置而跳转
					gotoDialog.dispose();                  //dispose将该对话框操作所使用内存返回,并使对话框不可显示
				}
			});

			JButton cancel = new JButton("取消");
			cancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					gotoDialog.dispose();                   //dispose将该对话框操作所使用内存返回,并使对话框不可显示
				}
			});

			Container con = gotoDialog.getContentPane();  //添加容器包容控件
			con.setLayout(new FlowLayout());             //显示控件 
			con.add(gotoLabel);                          //按顺序添加控件
			con.add(linenum);
			con.add(okButton);
			con.add(cancel);

			gotoDialog.setSize(160, 100);                 //设置对话框大小
			gotoDialog.setResizable(false);              //设置对话框为大小不可变
			gotoDialog.setLocation(200, 200);             //设置显示时的屏幕初始位置坐标

⌨️ 快捷键说明

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