日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

基于Java實(shí)現(xiàn)記事本功能

瀏覽:31日期:2022-08-18 18:29:00

本文實(shí)例為大家分享了Java實(shí)現(xiàn)記事本的具體代碼,供大家參考,具體內(nèi)容如下

編寫一個(gè)具有菜單以及編輯、查找、替換、復(fù)制、粘貼功能,且具有新建、打開(kāi)和保存文件功能的記事本(MyNotepad)。

package ch7;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.Optional;import javafx.scene.control.TextField;import javafx.application.Application;import javafx.event.ActionEvent;import javafx.geometry.Insets;import javafx.scene.Scene;import javafx.scene.control.Alert;import javafx.scene.control.Alert.AlertType;import javafx.scene.control.Button;import javafx.scene.control.ButtonBar;import javafx.scene.control.ButtonType;import javafx.scene.control.Label;import javafx.scene.control.Menu;import javafx.scene.control.MenuBar;import javafx.scene.control.MenuItem;import javafx.scene.control.SeparatorMenuItem;import javafx.scene.control.TextArea;import javafx.scene.input.Clipboard;import javafx.scene.input.ClipboardContent;import javafx.scene.input.DataFormat;import javafx.scene.input.KeyCombination;import javafx.scene.layout.BorderPane;import javafx.scene.layout.HBox;import javafx.scene.layout.VBox;import javafx.stage.FileChooser;import javafx.stage.FileChooser.ExtensionFilter;import javafx.stage.Stage;import javafx.stage.WindowEvent;//編寫一個(gè)具有菜單以及編輯、查找、替換、復(fù)制、粘貼功能,且具有新建、打開(kāi)和保存文件功能的記事本(MyNotepad)。public class MyNotePad extends Application { public static TextArea textArea;//文本框的范圍 public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { // 文件選取器 final FileChooser fileChooser = new FileChooser(); fileChooser.getExtensionFilters().addAll(new ExtensionFilter('Text Files', '*.txt'),//加入格式 new ExtensionFilter('Java Sourse Files', '*.java')); // 創(chuàng)建MenuBar MenuBar menuBar = new MenuBar();// menuBar.setStyle('-fx-background-color:lightgray'); /************************************ * 創(chuàng)建 Menu, 文件菜單條 ************************************/ Menu menuFile = new Menu('文件(F)'); //1.新建 MenuItem menuNew = new MenuItem('新建'); menuNew.setAccelerator(KeyCombination.valueOf('Ctrl+N')); menuNew.setOnAction((final ActionEvent e)-> { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); // 創(chuàng)建一個(gè)消息對(duì)話框,僅僅提供確定按鈕 alert.setHeaderText('新建文件'); // 設(shè)置對(duì)話框的頭部文本 // 設(shè)置對(duì)話框的內(nèi)容文本 alert.setContentText('確定新建文件嗎??'); //alert.show(); // 顯示對(duì)話框 Optional<ButtonType> buttonType = alert.showAndWait(); // 判斷返回的按鈕類型是確定還是取消,再據(jù)此分別進(jìn)一步處理 if (buttonType.get().getButtonData().equals(ButtonBar.ButtonData.OK_DONE)) { // 單擊了確定按鈕OK_DONE textArea.setText(' ');//清空文本框內(nèi) primaryStage.setTitle('新建文件'); } }); //2.打開(kāi) MenuItem menuOpen = new MenuItem('打開(kāi)(O)...'); // 設(shè)置menuItem的快捷鍵 menuOpen.setAccelerator(KeyCombination.valueOf('Ctrl+O')); menuOpen.setOnAction((final ActionEvent e) -> { File file = fileChooser.showOpenDialog(primaryStage); if (file != null) { openFile(file); } }); //3.保存 MenuItem menuSave = new MenuItem('保存(S)'); menuSave.setAccelerator(KeyCombination.valueOf('Ctrl+S')); menuSave.setOnAction((final ActionEvent e) -> { FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter('TXT files (*.txt)', '*.txt'); File file = fileChooser.showSaveDialog(primaryStage); saveFile(file); if(file.getAbsolutePath()!=null) { System.out.print(file.getName()+'已經(jīng)報(bào)存在:'+file); } else System.out.println(' 此文件未保存'); }); //4.另存 MenuItem menuSaveAs = new MenuItem('另存(A)...'); menuSaveAs.setOnAction((final ActionEvent e) -> { FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter('TXT files (*.txt)', '*.txt'); File file = fileChooser.showSaveDialog(primaryStage); saveFile(file); if(file.getAbsolutePath()!=null) { System.out.print(file.getName()+'已經(jīng)另存在:'+file); } else System.out.println(' 此文件未保存'); }); // 創(chuàng)建分割線 SeparatorMenuItem separator1 = new SeparatorMenuItem(); SeparatorMenuItem separator2 = new SeparatorMenuItem(); MenuItem menuExit = new MenuItem('退出'); menuExit.setOnAction((ActionEvent e) -> { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); // 創(chuàng)建一個(gè)確認(rèn)對(duì)話框 //判斷文本框是否為空 if(!textArea.getText().isEmpty()) { alert.setHeaderText('還有內(nèi)容未保存,你確定要退出嗎?'); }// 設(shè)置對(duì)話框的頭部文本 else alert.setHeaderText('確定要退出嗎?'); // 設(shè)置對(duì)話框的內(nèi)容文本// alert.setContentText('確定要退出MyNotePad嗎?'); // 顯示對(duì)話框,并等待按鈕返回 Optional<ButtonType> buttonType = alert.showAndWait(); // 判斷返回的按鈕類型是確定還是取消,再據(jù)此分別進(jìn)一步處理 if (buttonType.get().getButtonData().equals(ButtonBar.ButtonData.OK_DONE)) { // 單擊了確定按鈕OK_DONE System.exit(0); } }); // 將MenuItem放在對(duì)應(yīng)的Menu上e menuFile.getItems().addAll(menuNew, menuOpen, separator1, menuSave, menuSaveAs, separator2, menuExit);// 將分割線加進(jìn)來(lái) /************************************ * 創(chuàng)建 Menu, 編輯菜單條 ************************************/ Menu menuEdit = new Menu('編輯(E)'); / MenuItem menuSelctAll = new MenuItem('全選(A)'); menuSelctAll.setAccelerator(KeyCombination.valueOf('Ctrl+A')); menuSelctAll.setOnAction((final ActionEvent e)->{ selectAll(); }); MenuItem menuCut = new MenuItem('剪切'); menuCut.setOnAction((final ActionEvent e)->{ cutMethod(); }); menuCut.setAccelerator(KeyCombination.valueOf('Ctrl+X')); MenuItem menuCopy = new MenuItem('復(fù)制(C)'); menuCopy.setOnAction((final ActionEvent e)->{ copyMethod(); }); menuCopy.setAccelerator(KeyCombination.valueOf('Ctrl+C')); / MenuItem menuPaste = new MenuItem('粘貼(P)'); menuPaste.setAccelerator(KeyCombination.valueOf('Ctrl+V')); menuPaste.setOnAction((final ActionEvent e)->{ pasteMethod(); }); // 創(chuàng)建分割線 SeparatorMenuItem separator3 = new SeparatorMenuItem(); // 查找替換菜單項(xiàng) MenuItem menuFind = new MenuItem('查找(F)'); menuFind.setOnAction((final ActionEvent e)->{ findMethod(); }); MenuItem menuReplace = new MenuItem('替換(R)...'); menuReplace.setOnAction((final ActionEvent e)->{ replaceMethod(); }); menuEdit.getItems().addAll(menuSelctAll, menuCut,menuCopy, menuPaste, separator3, menuFind, menuReplace); // 創(chuàng)建 幫助子菜單 Menu Menu menuHelp = new Menu('幫助(H)'); MenuItem menuGuide = new MenuItem('指南(D)'); menuGuide.setOnAction((ActionEvent e) -> { // 設(shè)置按鈕的單擊事件 Alert alert = new Alert(Alert.AlertType.INFORMATION); // 創(chuàng)建一個(gè)消息對(duì)話框 alert.setHeaderText('指南'); // 設(shè)置對(duì)話框的頭部文本 // 設(shè)置對(duì)話框的內(nèi)容文本 alert.setContentText('指南正在努力編寫中,敬請(qǐng)期待。。。'); alert.show(); // 顯示對(duì)話框 }); MenuItem menuAbout = new MenuItem('關(guān)于(A)'); menuAbout.setOnAction((ActionEvent e) -> { // 設(shè)置按鈕的單擊事件 Alert alert = new Alert(Alert.AlertType.INFORMATION); // 創(chuàng)建一個(gè)消息對(duì)話框 alert.setHeaderText('關(guān)于本軟件'); // 設(shè)置對(duì)話框的頭部文本 // 設(shè)置對(duì)話框的內(nèi)容文本 alert.setContentText('JAVA記事本 版權(quán)所有 @2035'); alert.show(); // 顯示對(duì)話框 }); menuHelp.getItems().addAll(menuGuide, menuAbout); // MenuBar,裝入各菜單條 menuBar.getMenus().addAll(menuFile, menuEdit, menuHelp); // 創(chuàng)建MenuItem類 // 還可以對(duì)MenuItem設(shè)置圖標(biāo) // 將menuBar加入到布局類mainPane上 // 文本編輯組件 textArea = new TextArea(); // 創(chuàng)建布局類, 放置編輯區(qū)域 BorderPane mainPane = new BorderPane(); mainPane.setTop(menuBar); mainPane.setCenter(textArea); // 創(chuàng)建場(chǎng)景圖// Scene scene = new Scene(anchorPane); Scene scene = new Scene(mainPane); primaryStage.setScene(scene); primaryStage.setHeight(800); primaryStage.setWidth(700); primaryStage.setTitle('無(wú)標(biāo)題-記事本'); // 用戶點(diǎn)擊關(guān)窗按鈕時(shí) ...... primaryStage.setOnCloseRequest((WindowEvent event) -> { // 嚴(yán)格的話,需判斷文件保存與否,或確認(rèn)是否退出 Alert alert = new Alert(Alert.AlertType.INFORMATION); // 創(chuàng)建一個(gè)確認(rèn)對(duì)話框,提供一個(gè)確認(rèn)圖像,和確認(rèn)取消按鈕 alert.setHeaderText('確定要退出記事本嗎?'); // 設(shè)置對(duì)話框的頭部文本 // 設(shè)置對(duì)話框的內(nèi)容文本// alert.setContentText('確定要退出MyNotePad嗎?'); // 顯示對(duì)話框,并等待按鈕返回 Optional<ButtonType> buttonType = alert.showAndWait(); // 判斷返回的按鈕類型是確定還是取消,再據(jù)此分別進(jìn)一步處理 if (buttonType.get().getButtonData().equals(ButtonBar.ButtonData.OK_DONE)) { // 單擊了確定按鈕OK_DONE System.exit(0); }//不知道怎么用取消返回,所以改成了INFORMATION }); primaryStage.show(); } //打開(kāi) private void openFile(File file) { textArea.setText(''); try { BufferedReader in = new BufferedReader(new FileReader(file)); String line; while ((line = in.readLine()) != null) textArea.appendText(line + 'n'); in.close(); textArea.positionCaret(0); } catch (IOException ioe) { System.err.println(ioe); } } //保存 public void saveFile(File file) { try { PrintWriter print=new PrintWriter(new BufferedWriter(new FileWriter(file))); print.write(textArea.getText()); print.flush(); print.close(); }catch(IOException ioe) { ioe.printStackTrace();; } } //復(fù)制 public void copyMethod() { //獲取剪切板 Clipboard clipboard=Clipboard.getSystemClipboard(); ClipboardContent content=new ClipboardContent();// //選取文本 String temp=textArea.getSelectedText();//獲得已經(jīng)選取的內(nèi)容 //將獲取的內(nèi)容放到系統(tǒng)剪切板 content.putString(temp); //把內(nèi)容放在文本剪切板 clipboard.setContent(content); } //剪切 public void cutMethod() { //獲得系統(tǒng)剪切板 Clipboard clip=Clipboard.getSystemClipboard(); ClipboardContent content=new ClipboardContent(); //獲取選中 String temp=textArea.getSelectedText(); //把選中放入剪切板 content.putString(temp); //放入文本剪貼板 clip.setContent(content); //選中內(nèi)容用''代替 textArea.replaceSelection(''); } //粘貼 public void pasteMethod() { Clipboard clip=Clipboard.getSystemClipboard(); ClipboardContent content=new ClipboardContent(); Clipboard c=clip.getSystemClipboard(); if(c.hasContent(DataFormat.PLAIN_TEXT)); { String s=c.getContent(DataFormat.PLAIN_TEXT).toString(); if(textArea.getSelectedText()!=null) {//選中不為空 textArea.replaceSelection(s); } else {//如果鼠標(biāo)為選中,從后邊貼 int mouse=textArea.getCaretPosition();//插入符號(hào)在文本中當(dāng)前位置 textArea.insertText(mouse, s); } } } //全選 public void selectAll() { textArea.selectAll();//全選 } //查找,參照網(wǎng)頁(yè)代碼 int startIndex=0; public void findMethod() { HBox h1=new HBox(); h1.setPadding(new Insets(20,5,20,5)); h1.setSpacing(5); Label label1=new Label('查找內(nèi)容(N):'); TextField tf1=new TextField(); h1.getChildren().addAll(label1,tf1); VBox v1=new VBox(); v1.setPadding(new Insets(20,5,20,10)); Button btn1=new Button('查找下一個(gè)'); 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=textArea.getText(); String tfString=tf1.getText(); if(!tf1.getText().isEmpty()) { if(textString.contains(tfString)) { if(startIndex==-1) { Alert alert1=new Alert(AlertType.WARNING); alert1.titleProperty().set('提示'); alert1.headerTextProperty().set('我找不著'); alert1.show(); } startIndex=textArea.getText().indexOf(tf1.getText(),startIndex); if(startIndex>=0&&startIndex<textArea.getText().length()) { textArea.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('出錯(cuò)'); alert1.headerTextProperty().set('輸入內(nèi)容為空。'); alert1.show(); } }); } //這段是參照網(wǎng)頁(yè)代碼 public void replaceMethod() { HBox h1 = new HBox(); h1.setPadding(new Insets(20, 5, 10, 8)); h1.setSpacing(5); Label label1 = new Label('查找下一個(gè)(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èi)容(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('查找下一個(gè)'); 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 = textArea.getText(); // 獲取記事本文本域的字符串 String tfString = tf1.getText(); // 獲取查找內(nèi)容的字符串 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('已經(jīng)找不到相關(guān)內(nèi)容了!!!'); alert1.show(); } startIndex = textArea.getText().indexOf(tf1.getText(),startIndex); if (startIndex >= 0 && startIndex < textArea.getText().length()) { textArea.selectRange(startIndex, startIndex+tf1.getText().length()); startIndex += tf1.getText().length(); } btn2.setOnAction((ActionEvent e2) -> { if(tf2.getText().isEmpty()) { //替換內(nèi)容為空時(shí) Alert alert1 = new Alert(AlertType.WARNING); alert1.titleProperty().set('出錯(cuò)了'); alert1.headerTextProperty().set('替換內(nèi)容為空'); alert1.show(); }else { //替換內(nèi)容不為空則替換 textArea.replaceSelection(tf2.getText()); } }); } if (!textString.contains(tfString)) { Alert alert1 = new Alert(AlertType.WARNING); alert1.titleProperty().set('提示'); alert1.headerTextProperty().set('找不到相關(guān)內(nèi)容了!!!'); alert1.show(); } } else if (tf1.getText().isEmpty()) { Alert alert1 = new Alert(AlertType.WARNING); alert1.titleProperty().set('出錯(cuò)了'); alert1.headerTextProperty().set('輸入內(nèi)容為空'); alert1.show(); } }); } }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
男人的天堂久久精品| 欧美精品黄色| 蜜臀va亚洲va欧美va天堂| 国产精品7m凸凹视频分类| 激情不卡一区二区三区视频在线| 美女视频免费精品| 欧美激情福利| 国产 日韩 欧美 综合 一区| 精品视频97| 国产精品亚洲一区二区三区在线观看| 欧美xxxx中国| 久久九九精品| 91精品福利| 亚洲九九精品| 青草综合视频| 久久99精品久久久野外观看| 国产精品欧美大片| 久久精品国产网站| bbw在线视频| 成人午夜亚洲| 欧洲亚洲一区二区三区| 午夜国产精品视频| 亚洲精品字幕| 久久中文在线| 日韩精品网站| 国产精品日本| 91亚洲精品视频在线观看| 国产精品久久| 在线中文字幕播放| 欧美福利在线| 亚洲精品激情| 久久精品国产亚洲aⅴ| 99精品视频在线观看免费播放| 午夜精品影院| 日韩精品国产欧美| 成人综合一区| 久久国产精品久久久久久电车 | 国产不卡人人| 夜夜精品视频| 日韩av一区二区三区| 国产在线一区不卡| 一区三区视频| 国产剧情在线观看一区| 欧洲一级精品| 蜜臀久久99精品久久久画质超高清 | 日韩大片在线| 激情五月色综合国产精品| 亚洲精品国模| 国产在线看片免费视频在线观看| 黄色亚洲免费| 国产三级精品三级在线观看国产| 桃色av一区二区| 免费成人在线影院| 久久精品国产在热久久| av亚洲免费| 奇米狠狠一区二区三区| 99久久亚洲精品蜜臀| 日韩精品久久理论片| 欧美成a人国产精品高清乱码在线观看片在线观看久 | 亚洲欧美日韩国产综合精品二区 | 日韩一区二区三区免费视频| 精品女同一区二区三区在线观看| 国产精品91一区二区三区| 欧美亚洲三级| 99精品在线免费在线观看| 日本色综合中文字幕| 久久久水蜜桃av免费网站| 日本色综合中文字幕| 精品中文字幕一区二区三区av| 国产精久久久| 免费日本视频一区| 福利视频一区| 日韩av一区二| 日韩午夜一区| 国产va在线视频| 国产日韩欧美一区在线| 欧美特黄一区| 三上亚洲一区二区| 日本国产欧美| aⅴ色国产欧美| 欧美不卡高清一区二区三区| 国产精品一区免费在线| 亚洲精品一级二级三级| 99香蕉国产精品偷在线观看| 欧美www视频在线观看| 国产乱码精品一区二区三区亚洲人 | 免费在线小视频| 日本午夜免费一区二区| 亚洲激情黄色| 欧美成人精品三级网站| 久久激情五月激情| 亚洲一区二区av| 久久久久国产一区二区| 精品伊人久久久| 91成人福利| 国产亚洲网站| 亚洲成人av观看| 福利精品一区| 精品国产91| 久久精品亚洲一区二区| 91精品国产自产精品男人的天堂| 久久99伊人| 亚洲专区一区| 模特精品在线| 91tv亚洲精品香蕉国产一区| 国产一区二区三区四区| 麻豆成人综合网| 国产精品美女午夜爽爽| 日本伊人久久| 免费在线视频一区| 一本色道精品久久一区二区三区| 久久免费大视频| 美女网站一区| 99久久99久久精品国产片果冰| 中文一区一区三区高中清不卡免费| 国产精品久久久久久模特| 国产调教一区二区三区| 国产欧美二区| 欧美亚洲tv| 国产乱人伦丫前精品视频| 欧美亚洲三区| 国产精品亚洲片在线播放| 国产伦理久久久久久妇女| 国产精品久久乐| 欧美激情福利| 精品久久久久中文字幕小说| 亚洲区第一页| 日本va欧美va精品发布| 国产毛片久久久| 久久这里只有| 国产成人久久| 国产高潮在线| 欧美日韩一二三四| 伊人精品视频| 中文不卡在线| 日韩和欧美一区二区三区| 欧美日本不卡| 久久的色偷偷| 福利一区二区三区视频在线观看| 欧美好骚综合网| 亚洲日本网址| 欧美sm一区| 99日韩精品| 日韩精品91亚洲二区在线观看| 日韩av午夜在线观看| 欧美国产另类| 日韩欧美一区二区三区在线观看| 欧美在线资源| 日韩激情av在线| 久久久久97| 成人美女视频| 好看的av在线不卡观看| 亚洲三级观看| 麻豆91小视频| 天堂8中文在线最新版在线| 在线精品视频在线观看高清| 亚洲综合国产| 国产精品亚洲一区二区在线观看| av中文字幕在线观看第一页| 激情久久五月| 清纯唯美亚洲综合一区| 中文在线中文资源| 国产日韩专区| 日韩国产在线观看一区| 久久久久免费| 蜜桃久久av一区| 麻豆精品av| 女主播福利一区| 欧美亚洲网站| 日本久久成人网| 中文字幕一区二区精品区| 亚洲aa在线| 黄毛片在线观看| 男女精品网站| 国产一区二区三区探花| 黄色亚洲免费| 国产乱码精品一区二区亚洲| 久久久久99| 亚洲婷婷丁香| 国产第一亚洲| 亚洲深深色噜噜狠狠爱网站| 美女视频黄免费的久久| 欧美在线亚洲综合一区| 日韩国产精品久久久久久亚洲| 91看片一区| 午夜视频一区二区在线观看| 老司机免费视频一区二区| 亚洲一级黄色| 欧美激情视频一区二区三区免费 | 国产日韩中文在线中文字幕| 日韩精品影视| 蜜桃久久av一区| 精品国产麻豆| 日韩一级不卡| 国产在线日韩精品| 在线精品观看| 日韩精品第一区| 日韩av一区二区在线影视| 久久久久一区| 国产精品亚洲一区二区在线观看|