notepad4.java

来自「一个文本编辑器,用JAVA程序编写的」· Java 代码 · 共 561 行 · 第 1/2 页

JAVA
561
字号
		centerPanel.add(Cstr);
		
		bottomPanel.add(su);
		bottomPanel.add(cancel);
		bottomPanel.add(anPanel);

		con.add(topPanel);
		con.add(centerPanel);
		con.add(bottomPanel);

		// 设置"统计"对话框的大小、可更改大小(否)、位置和可见性
		countDialog.setSize(350, 350);
		countDialog.setResizable(false);
		countDialog.setLocation(230, 250);
		countDialog.setVisible(true);
	}

	/* 方法myCount()结束 */

	// 实现ActionListener的事件处理方法public void actionPerformed(ActionEvent e)
	public void actionPerformed(ActionEvent e) {
		// 读入
		if (e.getSource() == FRead) {
			Text.requestFocus();
			String currentValue = Text.getText();
			boolean isTextChange = (currentValue.equals(oldValue)) ? false : true;

			if (isTextChange) {

				int saveChoose = JOptionPane.showConfirmDialog(this, "您的文件尚未保存。是否保存?", "提示", JOptionPane.YES_NO_CANCEL_OPTION);

				if (saveChoose == JOptionPane.YES_OPTION) {
					JFileChooser fileChooser = new JFileChooser();
					fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
					fileChooser.setApproveButtonText("确定");
					fileChooser.setDialogTitle("另存为");

					int result = fileChooser.showSaveDialog(this);

					if (result == JFileChooser.CANCEL_OPTION) {
						statusLabel1.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() + "  - 文本");
						statusLabel1.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) {
						statusLabel1.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 + "\15\12");
							}// endwhile
							this.setTitle(fileName.getName() + "  - 文本");
							statusLabel1.setText(" 当前打开文件:" + fileName.getAbsoluteFile());
							fr.close();
							isNewFile = false;
							currentFile = fileName;
							oldValue = Text.getText();
						} catch (IOException ioException) {
						}
					}
				} 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) {
					statusLabel1.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 + "\15\12");
						}// endwhile

						this.setTitle(fileName.getName() + "  - 文本");
						statusLabel1.setText(" 当前打开文件:" + fileName.getAbsoluteFile());
						fr.close();
						isNewFile = false;
						currentFile = fileName;
						oldValue = Text.getText();
					} catch (IOException ioException) {
					}
				}

			}
		}// "打开"处理结束

		// 保存
		else if (e.getSource() == FSave) {
			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) {
					statusLabel1.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() + "  - 记事本");
					statusLabel1.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 ioException) {
				}
			}
		}// "保存"处理结束

		// 退出
		else if (e.getSource() == FExit) {
			int exitChoose = JOptionPane.showConfirmDialog(this, "确定要退出么?", "退出提示", JOptionPane.OK_CANCEL_OPTION);
			if (exitChoose == JOptionPane.OK_OPTION) {
				checkText();
			} else {
				return;
			}
		}
		
		//字母统计
		else if (e.getSource() == mEdit_char){
			myCount();
		}
		
		//数字统计
		else if (e.getSource() == mEdit_num){
			myCount();
		}
		
		//空格统计
		else if(e.getSource() == mEdit_blank){
			myCount();
		}
		
		//字符串统计
		else if (e.getSource() == mEdit_string){
			myCount();
		}
		
		//删除串
		else if (e.getSource() == mEdit_delstr){
			Text.requestFocus();
			Text.replaceRange("", Text.getSelectionStart(), Text.getSelectionEnd());
		}

		// 帮助主题
		else if (e.getSource() == mHelp_HelpTopics) {
			JOptionPane.showMessageDialog(this, "文本编辑器\n" + " 新手制作\n", "帮助主题", JOptionPane.INFORMATION_MESSAGE);
		}

		// 关于
		else if (e.getSource() == mHelp_About) {
			JOptionPane.showMessageDialog(this, "       文本编辑器\n" + "       QQ:837238781\n" + "     JAVA图形界面练习\n", "关于文本编辑器", JOptionPane.INFORMATION_MESSAGE);
		}


	}/* 方法actionPerformed()结束 */


	public void removeUpdate(DocumentEvent e) {
	}

	public void insertUpdate(DocumentEvent e) {
	}

	public void changedUpdate(DocumentEvent e) {
	}

	// End of DocumentListener

	public static void main(String s[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		Text = new JTextArea();
		Text.setTransferHandler(new FileTransferHandler(Text));
		new Notepad4();
	}
}

class FileTransferHandler extends TransferHandler {
	JTextArea Text;

	public FileTransferHandler(JTextArea Text) {
		this.Text = Text;
	}

	public boolean importData(JComponent c, Transferable t) {
		try {
			List files = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
			addFilesToFilePathList(files);
			return true;
		} catch (UnsupportedFlavorException ufe) {
			ufe.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean canImport(JComponent c, DataFlavor[] flavors) {
		for (int i = 0; i < flavors.length; i++) {
			if (DataFlavor.javaFileListFlavor.equals(flavors[i])) {
				return true;
			}
		}
		return false;
	}

	private void addFilesToFilePathList(List files) {
		for (Iterator iter = files.iterator(); iter.hasNext();) {
			File file = (File) iter.next();
			String str = null;
			try {
				FileReader fr = new FileReader(file);
				BufferedReader bfr = new BufferedReader(fr);
				Text.setText("");
				while ((str = bfr.readLine()) != null) {
					Text.append(str + "\15\12");
				}
			} catch (Exception b) {
			}
		}
	}
}

⌨️ 快捷键说明

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