Java课程设计--用JavaFX实现简单记事本

记录一下本次课程设计用JAVA实现的简单记事本。
要求:用图形用户界面时间。至少实现编辑、保存、另存为、查找替换,以及剪切、复制、粘贴等功能。
提示:使用文件输入输出流。

1.先对记事本进行功能分析
在这里插入图片描述
在这里,主要实现文件和编辑功能。
菜单栏中的文件内的子菜单功能有:新建、打开、保存、另存为、退出
菜单栏中的编辑内的子菜单功能有:复制、粘贴、剪贴、删除、查找、替换
2.几个基本功能的实现

在记事本类里面定义:
private String path = null;// 装打开的文件的路径
boolean save = false; // 没有保存
int startIndex = 0; ///字符串开始的位置

2.1 打开功能

    // 打开操作
		openMenuItem.setOnAction(event -> {
			openMethod();
		});
		
    // 打开方法
	private void openMethod() {
		FileChooser fc = new FileChooser(); // fileChooser打开一个目录
		fc.setTitle("选择文件-打开"); // 设置文件选择框的标题
		// 过滤文件
		fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("TXT", "*.txt"));
		File file = fc.showOpenDialog(stage); // 打开文件
		if (file != null && file.exists()) { // 文件存在
			try {
				// 读取数据 放进多行文本框
				FileInputStream in = new FileInputStream(file); // 用File对象创建文件输入流对象
				byte[] bs = new byte[(int) file.length()];
				in.read(bs);

				// 将内容设置到多行文本框
				ta.setText(new String(bs));
				in.close();
				// 将文件路径 保留到成员变量path中
				path = file.getPath();
				// 更改窗口标题
				int lastIndex = path.lastIndexOf("\\"); // 查找字符串最后一次出现的位置
				String title = path.substring(lastIndex + 1); // 从下标lastIndex开始到结束产生一个新的字符串
				setTitle(title + "-我的记事本"); // 利用新的字符串重新设置窗口的标题
			} catch (Exception e) {
			}
		}
	}

2.2 新建功能

// 新建操作
		newMenuItem.setOnAction(event -> {
			newMethod();
		});
// 新建方法
	private void newMethod() {
		if (save) {  //如果内容已经保存了直接新建一个文件夹
			// 清除文本框的内容
			ta.setText("");
			// 更改窗口标题
			setTitle("新文件-我的记事本");
			// 将开打的文件的路径设置为null
			path = null;
		} else if (!save && !ta.getText().isEmpty()) { //文本域内有内容且文本没有保存时跳出提示
			Stage newStage = new Stage();
			VBox vBox = new VBox();
			vBox.setPadding(new Insets(5,10,5,20));
			vBox.setSpacing(5);
			Label label1 = new Label("你还没有保存内容!!!");
			Label label2 = new Label("按“确定”则新建一个文本,");
			Label label3= new Label("按“取消”则退出该窗口。");
			vBox.getChildren().addAll(label1,label2,label3);
			Button ok = new Button("确定");
			Button cancel = new Button("取消");
			HBox hBox = new HBox();
			hBox.setPadding(new Insets(20,10,5,80));
			hBox.setSpacing(30);
			hBox.getChildren().addAll(ok,cancel);
			BorderPane rootNode = new BorderPane();
			rootNode.setTop(vBox);
			rootNode.setCenter(hBox);
			
			Scene scene = new Scene(rootNode,300,150);
			newStage.setTitle("提示");
			newStage.setScene(scene);
			newStage.show();
			
			ok.setOnAction(event->{
				// 清除文本框的内容
				ta.setText("");
				// 更改窗口标题
				setTitle("新文件-我的记事本");
				// 将开打的文件的路径设置为null
				path = null;
				newStage.close();
			});
			
			cancel.setOnAction(event->{
				newStage.close(); 
			});
			
		} 	
	}

2.3 保存功能

 // 保存操作
		saveMenuItem.setOnAction(event -> {
			saveMethod();
		});
// 保存方法
	private void saveMethod() {
		// 需要判断是新建之后的保存 还是打开之后的保存
		// 新建之后的保存
		if (path == null) {
			FileChooser fc = new FileChooser();
			fc.setTitle("选择文件-保存");
			// 获取被用户选择的文件
			File file = fc.showSaveDialog(stage);
			// 如果用户选了 而且文件是存在的
			if (file != null && !file.exists()) {
				// 将多行文本框中的内容写到file指向的文件中去
				try {
					// 创建输出流
					FileOutputStream out = new FileOutputStream(file);
					out.write(ta.getText().getBytes());
					out.flush();
					out.close();
					save = true; // 已经保存了
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} else {// 打开之后的保存
			try {
				// 创建输出流
				FileOutputStream out = new FileOutputStream(path);
				out.write(ta.getText().getBytes());
				out.flush();
				out.close();
				save = true;// 已经保存了
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

2.4 另存为功能

// 另存为操作
		saveAsMenuItem.setOnAction(event -> {
			saveAsMethod();
		});
// 另存为方法
	private void saveAsMethod() {
		FileChooser fc = new FileChooser();
		fc.setTitle("保存文件");
		File file = fc.showSaveDialog(stage);

		if (file != null) {
			try {
				FileOutputStream out = new FileOutputStream(file);
				String str = ta.getText();
				byte[] bs = str.getBytes();// 字符串转换成字节数组
				out.write(bs);
				out.flush();
				out.close();
				save = true;
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

2.5 退出操作

// 退出操作
		exitMenuItem.setOnAction(event -> {
			exitMethod();
		});
// 退出方法
	private void exitMethod() {
		if (save) {
			Platform.exit();
		} else if (!save && !ta.getText().isEmpty()) {
			Alert alert = new Alert(AlertType.WARNING);
			alert.titleProperty().set("提示");
			alert.headerTextProperty().set("你还没有保存已编辑的内容!!!");
			alert.show();
		}
	}

2.6 复制功能

// 复制操作
		copyMenuItem.setOnAction(enent -> {
			copyMethod();
		});
// 复制方法
	private void copyMethod() {
		// 获取系统剪贴板
		Clipboard clipboard = Clipboard.getSystemClipboard();
		ClipboardContent content = new ClipboardContent();
		// 选取文本
		String temp = ta.getSelectedText();
		// 将获取到的文本放到系统剪贴板中
		content.putString(temp);
		// 把内容放到文本剪贴板中
		clipboard.setContent(content);
	}

2.7剪切功能

// 剪切操作
		cutMenuItem.setOnAction(event -> {
			cutMethod();
		});
// 剪切方法
	private void cutMethod() {
		// 获取系统剪贴板
		Clipboard clipboard = Clipboard.getSystemClipboard();
		ClipboardContent content = new ClipboardContent();
		// 选取文本
		String temp = ta.getSelectedText();
		// 将获取到的文本放到系统剪贴板中
		content.putString(temp);
		// 把内容放到文本剪贴板中
		clipboard.setContent(content);
		// 用空字符串来代替选中的字符串
		ta.replaceSelection("");
	}

2.8 粘贴功能

// 粘贴操作
		pastMenuItem.setOnAction(event -> {
			pasteMethod();
		});
// 粘贴方法
	private void pasteMethod() {
		// 获取系统剪贴板
		Clipboard clipboard = Clipboard.getSystemClipboard();
		ClipboardContent content = new ClipboardContent();
		Clipboard c = clipboard.getSystemClipboard();
		if (c.hasContent(DataFormat.PLAIN_TEXT)) {
			String s = c.getContent(DataFormat.PLAIN_TEXT).toString();
			if (ta.getSelectedText() != null) { // 如果要粘贴时,鼠标已经选取了一段字符
				ta.replaceSelection(s);
			} else { // 如果要粘贴时,鼠标没有选取字符,直接从光标后开始粘贴
				int mouse = ta.getCaretPosition(); // 获取文本域鼠标所在位置
				ta.insertText(mouse, s);
			}
		}
	}

2.9 删除功能

// 删除操作
		deleteMenuItem.setOnAction(event -> {
			deleteMethod();
		});
// 删除方法
	private void deleteMethod() {
		// 用空字符串来代替选中的字符串
		ta.replaceSelection("");
	}

2.10 查找功能

// 查找操作
		findMenuItem.setOnAction(event -> {
			findMethod();
		});
// 查找方法
	private void findMethod() {
		HBox h1 = new HBox();
		h1.setPadding(new Insets(20, 5, 20, 5));
		h1.setSpacing(5);
		Label lable1 = new Label("查找内容(N):");
		TextField tf1 = new TextField();
		h1.getChildren().addAll(lable1, tf1);

		VBox v1 = new VBox();
		v1.setPadding(new Insets(20, 5, 20, 10));
		Button btn1 = new Button("查找下一个(F)");
		v1.getChildren().add(btn1);

		HBox findRootNode = new HBox();
		findRootNode.getChildren().addAll(h1, v1);

		Stage findStage = new Stage();
		Scene scene1 = new Scene(findRootNode, 450, 90);
		findStage.setTitle("查找");
		findStage.setScene(scene1);
		findStage.setResizable(false); // 固定窗口大小
		findStage.show();

		btn1.setOnAction((ActionEvent e) -> {
			String textString = ta.getText(); // 获取记事本文本域的字符串
			String tfString = tf1.getText(); // 获取要查找的字符串
			if (!tf1.getText().isEmpty()) {
				if (textString.contains(tfString)) {
					// 查找方法
						if (startIndex == -1) {// not found
							Alert alert1 = new Alert(AlertType.WARNING);
							alert1.titleProperty().set("提示");
							alert1.headerTextProperty().set("已经找不到相关内容了!!!");
							alert1.show();
						}
						startIndex = ta.getText().indexOf(tf1.getText(),startIndex);
						if (startIndex >= 0 && startIndex < ta.getText().length()) {
							ta.selectRange(startIndex, startIndex+tf1.getText().length());
							startIndex += tf1.getText().length(); 
						}
				}
				if (!textString.contains(tfString)) {
					Alert alert1 = new Alert(AlertType.WARNING);
					alert1.titleProperty().set("提示");
					alert1.headerTextProperty().set("找不到相关内容了!!!");
					alert1.show();
				}
			} else if (tf1.getText().isEmpty()) {
				Alert alert1 = new Alert(AlertType.WARNING);
				alert1.titleProperty().set("出错了");
				alert1.headerTextProperty().set("输入内容为空");
				alert1.show();
			}
		});
	}

2.11 替换功能

// 替换操作
		replaceMenuItem.setOnAction(event -> {
			replaceMethod();
		});
// 替换方法
	private void replaceMethod() {
		HBox h1 = new HBox();
		h1.setPadding(new Insets(20, 5, 10, 8));
		h1.setSpacing(5);
		Label label1 = new Label("查找下一个(F)");
		TextField tf1 = new TextField();
		h1.getChildren().addAll(label1, tf1);

		HBox h2 = new HBox();
		h2.setPadding(new Insets(5, 5, 20, 8));
		h2.setSpacing(5);
		Label label2 = new Label("替换内容(N):");
		TextField tf2 = new TextField();
		h2.getChildren().addAll(label2, tf2);

		VBox v1 = new VBox();
		v1.getChildren().addAll(h1, h2);

		VBox v2 = new VBox();
		v2.setPadding(new Insets(21, 5, 20, 10));
		v2.setSpacing(13);
		Button btn1 = new Button("查找下一个");
		Button btn2 = new Button("替换为");
		v2.getChildren().addAll(btn1, btn2);

		HBox replaceRootNode = new HBox();
		replaceRootNode.getChildren().addAll(v1, v2);

		Stage replaceStage = new Stage();
		Scene scene = new Scene(replaceRootNode, 430, 120);
		replaceStage.setTitle("替换");
		replaceStage.setScene(scene);
		replaceStage.setResizable(false); // 固定窗口大小
		replaceStage.show();

		btn1.setOnAction((ActionEvent e) -> {
			String textString = ta.getText(); // 获取记事本文本域的字符串
			String tfString = tf1.getText(); // 获取查找内容的字符串
			if (!tf1.getText().isEmpty()) {
				if (textString.contains(tfString)) {
					if (startIndex == -1) {// not found
						Alert alert1 = new Alert(AlertType.WARNING);
						alert1.titleProperty().set("提示");
						alert1.headerTextProperty().set("已经找不到相关内容了!!!");
						alert1.show();
					}
					startIndex = ta.getText().indexOf(tf1.getText(),startIndex);
					if (startIndex >= 0 && startIndex < ta.getText().length()) {
						ta.selectRange(startIndex, startIndex+tf1.getText().length());
						startIndex += tf1.getText().length(); 
					}
					btn2.setOnAction((ActionEvent e2) -> {
						if(tf2.getText().isEmpty()) {  //替换内容为空时
							Alert alert1 = new Alert(AlertType.WARNING);
							alert1.titleProperty().set("出错了");
							alert1.headerTextProperty().set("替换内容为空");
							alert1.show();
						}else {     //替换内容不为空则替换
							ta.replaceSelection(tf2.getText());
						}
						
					});
				}
				if (!textString.contains(tfString)) {
					Alert alert1 = new Alert(AlertType.WARNING);
					alert1.titleProperty().set("提示");
					alert1.headerTextProperty().set("找不到相关内容了!!!");
					alert1.show();
				}
				
			} else if (tf1.getText().isEmpty()) {
				Alert alert1 = new Alert(AlertType.WARNING);
				alert1.titleProperty().set("出错了");
				alert1.headerTextProperty().set("输入内容为空");
				alert1.show();
			}
		});
	}


  • 15
    点赞
  • 100
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值