一.实验要求

二.设计界面

1.主界面Notepad.fxml

2.查找与替换界面Find.fxml

3.字体界面Font.fxml

三.主要功能

1.文件操作

1.1新建文件

 //文件新建@FXMLvoid OnActionNew(ActionEvent event) throws FileNotFoundException, IOException, InterruptedException {//没保存的话要保存if(!isSaved)RequireSave();noteTextArea.setText(null);}

1.2保存文件

//文件保存 获取TextArea区域的内容,打开文件存储@FXMLvoid OnActionSave(ActionEvent event) throws FileNotFoundException, IOException{String text=noteTextArea.getText();if(text.equals(""))return;FileChooser fileChooser = new FileChooser();//文件类型过滤FileChooser.ExtensionFilter extensionFilter = new FileChooser.ExtensionFilter("文本文档(*.txt)","*.txt");fileChooser.getExtensionFilters().add(extensionFilter);Stage stage=getStage();//获取得到的文件file = fileChooser.showOpenDialog(stage);if(file==null)return;fileOperation.WriteFile(file, "UTF-8", text);isSaved=true;}

1.3另存为文件

//文件另存为 获取TextArea区域的内容@FXMLvoid OnActionOtherSave(ActionEvent event) throws FileNotFoundException, IOException {String text=noteTextArea.getText();FileChooser fileChooser = new FileChooser();//文件类型过滤FileChooser.ExtensionFilter extensionFilter = new FileChooser.ExtensionFilter("文本文档(*.txt)","*.txt");fileChooser.getExtensionFilters().add(extensionFilter);Stage stage=getStage();//获取得到的文件file=fileChooser.showSaveDialog(stage);if(file==null)return;fileOperation.WriteFile(file, "UTF-8", text);isSaved=true;}

1.4打开文件

//文件打开 打开一个txt文件,显示在TextArea上@FXMLvoid OnActionOpen(ActionEvent event) throws IOException {//打开资源管理器FileChooser fileChooser = new FileChooser();//文件类型过滤FileChooser.ExtensionFilter extensionFilter = new FileChooser.ExtensionFilter("文本文档(*.txt)","*.txt");fileChooser.getExtensionFilters().add(extensionFilter);Stage stage=getStage();//获取得到的文件file = fileChooser.showOpenDialog(stage);if(file==null)return;String text=fileOperation.ReadFile(file,"UTF-8");noteTextArea.setText(text);isSaved=true;}

2.查找操作

2.1查找上一个

 // 查找上一个@FXMLvoid OnActionLast(ActionEvent event){String target=findTextField.getText();String noteText=noteTextArea.getText();if (target.isEmpty()){Warning("查找文本为空!");return;}//查找文本不为空if (noteText.contains(target)) {fromIndex=noteText.lastIndexOf(target, fromIndex);if (fromIndex == -1){Warning("到第一个了");return;}if (fromIndex >= 0 && fromIndex < noteText.length()) {System.out.println("fromindex:"+fromIndex);noteTextArea.selectRange(fromIndex, fromIndex+target.length());fromIndex -= target.length();}}elseWarning("找不到相关内容");}

2.2查找下一个

//查找下一个@FXMLvoid OnActionNext(ActionEvent event) {String target=findTextField.getText();String noteText=noteTextArea.getText();if (target.isEmpty()){Warning("查找文本为空!");return;}//查找文本不为空if (noteText.contains(target)) {System.out.println(startIndex);fromIndex=startIndex;startIndex = noteText.indexOf(target, startIndex);if (startIndex == -1){Warning("到最后一个了");return;}if (startIndex >= 0 && startIndex < noteText.length()) {noteTextArea.selectRange(startIndex, startIndex + target.length());startIndex += target.length();}}elseWarning("找不到相关内容");}

3.替换操作

3.1一个一个替换

//一个一个替换@FXMLvoid OnActionReplace(ActionEvent event) {String target=findTextField.getText();String replaceText=replaceTextField.getText();String noteText=noteTextArea.getText();if(replaceText.isEmpty()){Warning("替换文本不能为空!");return;}if(noteText.contains(target)){fromIndex=startIndex;startIndex = noteText.indexOf(target, startIndex);if (startIndex == -1){Warning("已经替换到最后一个了");return;}if (startIndex >= 0 && startIndex < noteText.length()) {noteTextArea.selectRange(startIndex, startIndex + target.length());startIndex += target.length();}noteTextArea.replaceSelection(replaceText);}elseWarning("找不到相关内容");}

3.2全部替换

//一个一个替换@FXMLvoid OnActionReplace(ActionEvent event) {String target=findTextField.getText();String replaceText=replaceTextField.getText();String noteText=noteTextArea.getText();if(replaceText.isEmpty()){Warning("替换文本不能为空!");return;}if(noteText.contains(target)){fromIndex=startIndex;startIndex = noteText.indexOf(target, startIndex);if (startIndex == -1){Warning("已经替换到最后一个了");return;}if (startIndex >= 0 && startIndex < noteText.length()) {noteTextArea.selectRange(startIndex, startIndex + target.length());startIndex += target.length();}noteTextArea.replaceSelection(replaceText);}elseWarning("找不到相关内容");}

4.字体操作

4.1改变字体

//改变字体@FXMLvoid fontChange(ActionEvent event) {FontType=fontComboBox.getValue();font=Font.font(FontType,FontSize);sampleTextArea.setFont(font);}

4.2改变样式

注意这个功能不是对所有字体都适用,所以不用纠结为什么有的效果不展示

//改变风格@FXMLvoid styleChange(ActionEvent event) {switch(styleComboBox.getValue()){case "常规":font=Font.font(FontType,FontWeight.NORMAL,FontPosture.REGULAR,FontSize);break;case "粗体":font=Font.font(FontType,FontWeight.BOLD,FontPosture.REGULAR,FontSize);break;case "斜体":font=Font.font(FontType,FontWeight.NORMAL,FontPosture.ITALIC,FontSize);break;case "粗斜体":font=Font.font(FontType,FontWeight.BOLD,FontPosture.ITALIC,FontSize);break;}sampleTextArea.setFont(font);}

4.3改变大小

 //改变大小@FXMLvoid sizeChange(ActionEvent event) {FontSize=Integer.parseInt(sizeComboBox.getValue());font=Font.font(FontType,FontSize);sampleTextArea.setFont(font);}

四.全部代码

1.主界面

1.1FileController

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Optional;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;
import javafx.stage.Stage;/*** @author xuchi* 2022年5月15日*/
public class FileController {@FXMLprivate TextArea noteTextArea;@FXMLprivate MenuItem backMenuItem;@FXMLprivate MenuItem redoMenuItem;@FXMLprivate MenuItem findMenuItem;@FXMLprivate MenuItem replaceMenuItem;@FXMLprivate Label posLabel;@FXMLprivate Label scaleLabel;File file;FileOperation fileOperation;public boolean isReplace=false;public boolean isChanged=false;public boolean isSaved=false;// 初始化@FXMLvoid initialize(){//把controller加到hashmap:controllers里Main.controllers.put(this.getClass().getSimpleName(), this);//实例化文件操作类fileOperation=new FileOperation();//初始化撤销重做,查找按钮backMenuItem.setDisable(true);redoMenuItem.setDisable(true);findMenuItem.setDisable(true);replaceMenuItem.setDisable(true);//释放键盘之后,撤销键可用noteTextArea.setOnKeyReleased(e->{//对于撤销重做backMenuItem.setDisable(false);//对于查找if(!noteTextArea.getText().isEmpty()){findMenuItem.setDisable(false);replaceMenuItem.setDisable(false);}else{findMenuItem.setDisable(true);replaceMenuItem.setDisable(true);}});//只要文本值改变了noteTextArea.textProperty().addListener(new ChangeListener<String>() {@Overridepublic void changed(ObservableValue<? extends String> observable,String oldValue, String newValue) {isChanged=true;}});//点击查找findMenuItem.setOnAction(e->{isReplace=false;try{ShowFXML("Find.fxml","查找");} catch (IOException e1){// TODO Auto-generated catch blocke1.printStackTrace();}});replaceMenuItem.setOnAction(e->{isReplace=true;try{ShowFXML("Find.fxml","替换");} catch (IOException e1){// TODO Auto-generated catch blocke1.printStackTrace();}});//当前光标的位置noteTextArea.caretPositionProperty().addListener((o,oldValue,newValue)->{int num= newValue.intValue();String textContain=noteTextArea.getText();int line=1,col=1;for(int i=0;i<num;i++){if(textContain.charAt(i)=='\n'){line++;col=1;}elsecol++;}String posText=String.format("第 %d 行,第 %d 列",line,col);posLabel.setText(posText);});}public TextArea getTextArea(){return noteTextArea;}public boolean RequireSave() throws FileNotFoundException, IOException, InterruptedException {// TODO Auto-generated method stubif(!isChanged)return false;Alert alert = new Alert(AlertType.CONFIRMATION);alert.setHeaderText("记事本");if(isSaved&&isChanged)alert.setContentText("是否将更改保存到"+file.getPath()+"?");else if(!isSaved)alert.setContentText("是否保存?");ButtonType save = new ButtonType("保存");ButtonType notsave = new ButtonType("不保存");ButtonType cancel = new ButtonType("取消");alert.getButtonTypes().setAll(save, notsave,cancel);Optional<ButtonType> result = alert.showAndWait();if(result.get()==notsave)return false;if(result.get()==cancel)return true;if(isSaved&&isChanged)fileOperation.WriteFile(file, "UTF-8", noteTextArea.getText());else if(!isSaved){String text=noteTextArea.getText();FileChooser fileChooser = new FileChooser();//文件类型过滤FileChooser.ExtensionFilter extensionFilter = new FileChooser.ExtensionFilter("文本文档(*.txt)","*.txt");fileChooser.getExtensionFilters().add(extensionFilter);Stage stage=getStage();//获取得到的文件file=fileChooser.showSaveDialog(stage);//字体关闭,取消回到原位,if(file==null)return false;fileOperation.WriteFile(file, "UTF-8", text);isSaved=true;}return false;}//右键菜单->复制@FXMLvoid OnActionCopy(ActionEvent event) {noteTextArea.copy();}//右键菜单->粘贴@FXMLvoid OnActionPaste(ActionEvent event) {noteTextArea.paste();}//右键菜单->全选@FXMLvoid OnActionSelectAll(ActionEvent event) {noteTextArea.selectAll();}//右键菜单->剪切@FXMLvoid OnActionCut(ActionEvent event) {noteTextArea.cut();}//帮助@FXMLvoid OnActionHelp(ActionEvent event) throws IOException {ShowFXML("Help.fxml", "帮助");}//缩放->字体变大@FXMLvoid OnActionBig(ActionEvent event) {Font CurrentFont = noteTextArea.getFont();double NewSize = CurrentFont.getSize() + 1;Font NewFont = Font.font(CurrentFont.getName(), NewSize);noteTextArea.setFont(NewFont);int scale=Integer.parseInt(scaleLabel.getText());scale+=10;scaleLabel.setText(Integer.toString(scale));}//缩放->字体变小@FXMLvoid OnActionSmall(ActionEvent event) {Font CurrentFont = noteTextArea.getFont();double NewSize = CurrentFont.getSize() - 1;Font NewFont = Font.font(CurrentFont.getName(), NewSize);noteTextArea.setFont(NewFont);int scale=Integer.parseInt(scaleLabel.getText());scale-=10;scaleLabel.setText(Integer.toString(scale));}//展示查找与替换界面private void ShowFXML(String FxmlName,String title) throws IOException{// 初始化fxml界面Stage FindStage = new Stage();Parent root = FXMLLoader.load(getClass().getResource(FxmlName));Scene scene = new Scene(root, 350, 70);FindStage.setScene(scene);FindStage.setTitle(title);FindStage.show();// 查找}//自动换行@FXMLvoid OnActionWrap(ActionEvent event) {noteTextArea.wrapTextProperty().set(true);}//重做@FXMLvoid OnActionRedo(ActionEvent event) {noteTextArea.redo();redoMenuItem.setDisable(true);backMenuItem.setDisable(false);}//撤销一步 @FXMLvoid OnActionBack(ActionEvent event) {noteTextArea.undo();backMenuItem.setDisable(true);redoMenuItem.setDisable(false);}//文件新建@FXMLvoid OnActionNew(ActionEvent event) throws FileNotFoundException, IOException, InterruptedException {//没保存的话要保存if(!isSaved)RequireSave();noteTextArea.setText(null);}//文件另存为 获取TextArea区域的内容@FXMLvoid OnActionOtherSave(ActionEvent event) throws FileNotFoundException, IOException {String text=noteTextArea.getText();FileChooser fileChooser = new FileChooser();//文件类型过滤FileChooser.ExtensionFilter extensionFilter = new FileChooser.ExtensionFilter("文本文档(*.txt)","*.txt");fileChooser.getExtensionFilters().add(extensionFilter);Stage stage=getStage();//获取得到的文件file=fileChooser.showSaveDialog(stage);if(file==null)return;fileOperation.WriteFile(file, "UTF-8", text);isSaved=true;}//文件保存 获取TextArea区域的内容,打开文件存储@FXMLvoid OnActionSave(ActionEvent event) throws FileNotFoundException, IOException{String text=noteTextArea.getText();if(text.equals(""))return;FileChooser fileChooser = new FileChooser();//文件类型过滤FileChooser.ExtensionFilter extensionFilter = new FileChooser.ExtensionFilter("文本文档(*.txt)","*.txt");fileChooser.getExtensionFilters().add(extensionFilter);Stage stage=getStage();//获取得到的文件file = fileChooser.showOpenDialog(stage);if(file==null)return;fileOperation.WriteFile(file, "UTF-8", text);isSaved=true;}//文件打开 打开一个txt文件,显示在TextArea上@FXMLvoid OnActionOpen(ActionEvent event) throws IOException {//打开资源管理器FileChooser fileChooser = new FileChooser();//文件类型过滤FileChooser.ExtensionFilter extensionFilter = new FileChooser.ExtensionFilter("文本文档(*.txt)","*.txt");fileChooser.getExtensionFilters().add(extensionFilter);Stage stage=getStage();//获取得到的文件file = fileChooser.showOpenDialog(stage);if(file==null)return;String text=fileOperation.ReadFile(file,"UTF-8");noteTextArea.setText(text);isSaved=true;}// 获取当前stageprivate Stage getStage(){return (Stage) noteTextArea.getScene().getWindow();}//进入字体界面@FXMLvoid OnActionFont(ActionEvent event) throws IOException{// 初始化fxml界面Stage FindStage = new Stage();Parent root = FXMLLoader.load(getClass().getResource("Font.fxml"));Scene scene = new Scene(root, 400, 400);FindStage.setScene(scene);FindStage.setTitle("字体");FindStage.show();}
}

1.2FileOperation

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;/*** @author xuchi* 2022年5月15日*/
public class FileOperation {//读文件public String ReadFile(File file,String CharType) throws IOException{String text = "";// 1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称InputStreamReader isr = new InputStreamReader(new FileInputStream(file), CharType);// 2.使用InputStreamReader对象中的方法read读取文件int ch = 0;while ((ch = isr.read()) != -1)text += (char) ch;// 3.释放资源。isr.close();return text;}//写文件public void WriteFile(File file,String CharType,String content) throws IOException, FileNotFoundException{OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(file), CharType);osw.write(content);osw.close();}
}

1.3Notepad.fxml

<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?><BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FileController"><top><MenuBar BorderPane.alignment="CENTER"><menus><Menu mnemonicParsing="false" text="文件"><items><MenuItem mnemonicParsing="false" onAction="#OnActionNew" text="新建" /><MenuItem mnemonicParsing="false" onAction="#OnActionSave" text="保存" /><MenuItem mnemonicParsing="false" onAction="#OnActionOtherSave" text="另存为" /><MenuItem mnemonicParsing="false" onAction="#OnActionOpen" text="打开" /></items></Menu><Menu mnemonicParsing="false" text="编辑"><items><MenuItem fx:id="backMenuItem" mnemonicParsing="false" onAction="#OnActionBack" text="撤销" /><MenuItem fx:id="redoMenuItem" mnemonicParsing="false" onAction="#OnActionRedo" text="重做" /><MenuItem fx:id="findMenuItem" mnemonicParsing="false" text="查找" /><MenuItem fx:id="replaceMenuItem" mnemonicParsing="false" text="替换" /><MenuItem mnemonicParsing="false" onAction="#OnActionFont" text="字体" /></items></Menu><Menu mnemonicParsing="false" text="查看"><items><MenuItem mnemonicParsing="false" onAction="#OnActionHelp" text="帮助" /><MenuItem mnemonicParsing="false" onAction="#OnActionWrap" text="自动换行" /><Menu mnemonicParsing="false" text="缩放"><items><MenuItem mnemonicParsing="false" onAction="#OnActionBig" text="放大" /><MenuItem mnemonicParsing="false" onAction="#OnActionSmall" text="缩小" /></items></Menu></items></Menu></menus></MenuBar></top><center><TextArea fx:id="noteTextArea" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER"><contextMenu><ContextMenu><items><MenuItem mnemonicParsing="false" onAction="#OnActionCopy" text="复制" /><MenuItem mnemonicParsing="false" onAction="#OnActionPaste" text="粘贴" /><MenuItem mnemonicParsing="false" onAction="#OnActionSelectAll" text="全选" /><MenuItem mnemonicParsing="false" onAction="#OnActionCut" text="剪切" /></items></ContextMenu></contextMenu></TextArea></center><bottom><HBox prefHeight="36.0" prefWidth="600.0" BorderPane.alignment="CENTER"><children><Label fx:id="posLabel" prefHeight="34.0" prefWidth="225.0" text="行1,列1" /><Separator orientation="VERTICAL" prefHeight="36.0" prefWidth="0.0" /><Label fx:id="scaleLabel" prefHeight="36.0" prefWidth="39.0" text="100" /><Label prefHeight="37.0" prefWidth="51.0" text="\%" /><Separator orientation="VERTICAL" prefHeight="200.0" /><Label prefHeight="36.0" prefWidth="161.0" text="Windows(CRLF)" /><Separator orientation="VERTICAL" prefHeight="200.0" /><Label prefHeight="40.0" prefWidth="55.0" text="UTF-8" /></children></HBox></bottom>
</BorderPane>

2.查找与替换

2.1FindController

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;/*** @author xuchi* 2022年5月18日*/
public class FindController {@FXMLprivate TextField findTextField;@FXMLpublic TextField replaceTextField;@FXMLpublic Button replaceButton;@FXMLpublic Button replaceallButton;TextArea noteTextArea;int startIndex=0;int fromIndex;//初始化,得到filecontroller@FXMLvoid initialize(){FileController controller = (FileController) Main.controllers.get(FileController.class.getSimpleName());noteTextArea=controller.getTextArea();fromIndex=noteTextArea.getText().length();if(controller.isReplace){replaceallButton.setVisible(true);replaceButton.setVisible(true);replaceTextField.setVisible(true);}}// 用于弹出警示private void Warning(String warningInfo){Alert alert = new Alert(AlertType.WARNING);alert.setTitle("Warning Dialog");alert.setContentText(warningInfo);alert.showAndWait();}// 查找上一个@FXMLvoid OnActionLast(ActionEvent event){String target=findTextField.getText();String noteText=noteTextArea.getText();if (target.isEmpty()){Warning("查找文本为空!");return;}//查找文本不为空if (noteText.contains(target)) {fromIndex=noteText.lastIndexOf(target, fromIndex);if (fromIndex == -1){Warning("到第一个了");return;}if (fromIndex >= 0 && fromIndex < noteText.length()) {System.out.println("fromindex:"+fromIndex);noteTextArea.selectRange(fromIndex, fromIndex+target.length());fromIndex -= target.length();}}elseWarning("找不到相关内容");} //查找下一个@FXMLvoid OnActionNext(ActionEvent event) {String target=findTextField.getText();String noteText=noteTextArea.getText();if (target.isEmpty()){Warning("查找文本为空!");return;}//查找文本不为空if (noteText.contains(target)) {System.out.println(startIndex);fromIndex=startIndex;startIndex = noteText.indexOf(target, startIndex);if (startIndex == -1){Warning("到最后一个了");return;}if (startIndex >= 0 && startIndex < noteText.length()) {noteTextArea.selectRange(startIndex, startIndex + target.length());startIndex += target.length();}}elseWarning("找不到相关内容");}//一个一个替换@FXMLvoid OnActionReplace(ActionEvent event) {String target=findTextField.getText();String replaceText=replaceTextField.getText();String noteText=noteTextArea.getText();if(replaceText.isEmpty()){Warning("替换文本不能为空!");return;}if(noteText.contains(target)){fromIndex=startIndex;startIndex = noteText.indexOf(target, startIndex);if (startIndex == -1){Warning("已经替换到最后一个了");return;}if (startIndex >= 0 && startIndex < noteText.length()) {noteTextArea.selectRange(startIndex, startIndex + target.length());startIndex += target.length();}noteTextArea.replaceSelection(replaceText);}elseWarning("找不到相关内容");}//全部替换@FXMLvoid OnActionReplaceAll(ActionEvent event) {String target=findTextField.getText();String replaceText=replaceTextField.getText();String noteText=noteTextArea.getText();noteTextArea.setText(noteText.replaceAll(target, replaceText));}
}

2.2Find.fxml

<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?><AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="187.0" prefWidth="470.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FindController"><children><GridPane layoutX="8.0" layoutY="4.0"><columnConstraints><ColumnConstraints hgrow="SOMETIMES" maxWidth="116.0" minWidth="10.0" prefWidth="110.0" /><ColumnConstraints hgrow="SOMETIMES" maxWidth="95.0" minWidth="10.0" prefWidth="90.0" /><ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /></columnConstraints><rowConstraints><RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /><RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /></rowConstraints><children><TextField fx:id="findTextField" promptText="查找" /><Button mnemonicParsing="false" onAction="#OnActionLast" text="上一条" GridPane.columnIndex="1" /><Button mnemonicParsing="false" onAction="#OnActionNext" text="下一条" GridPane.columnIndex="2" /><TextField fx:id="replaceTextField" promptText="替换" visible="false" GridPane.rowIndex="1" /><Button fx:id="replaceButton" mnemonicParsing="false" onAction="#OnActionReplace" text="替换" visible="false" GridPane.columnIndex="1" GridPane.rowIndex="1" /><Button fx:id="replaceallButton" mnemonicParsing="false" onAction="#OnActionReplaceAll" text="全部替换" visible="false" GridPane.columnIndex="2" GridPane.rowIndex="1" /></children></GridPane></children>
</AnchorPane>

3.字体

3.1FontController

import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;/*** @author xuchi* 2022年5月18日*/
public class FontController {@FXMLprivate ComboBox<String> fontComboBox;@FXMLprivate ComboBox<String> styleComboBox;@FXMLprivate ComboBox<String> sizeComboBox;@FXMLprivate TextArea sampleTextArea;TextArea noteTextArea;//用于实时记录字体,大小private String FontType="System Regular";private int FontSize=12;private Font font;//初始化文本和combobox@FXMLvoid initialize(){//向combobox里加入字体,样式,大小List<String> FontList=Font.getFontNames();fontComboBox.getItems().addAll(FontList);ObservableList<String> StyleList=FXCollections.observableArrayList("常规","粗体","斜体","粗斜体");styleComboBox.setItems(StyleList);for(int i=2;i<=72;i=i+2)sizeComboBox.getItems().add(i+"");//初始化测试文本sampleTextArea.setText("这是测试文本");FileController controller = (FileController) Main.controllers.get(FileController.class.getSimpleName());noteTextArea=controller.getTextArea();}//改变字体@FXMLvoid fontChange(ActionEvent event) {FontType=fontComboBox.getValue();font=Font.font(FontType,FontSize);sampleTextArea.setFont(font);}//改变风格@FXMLvoid styleChange(ActionEvent event) {switch(styleComboBox.getValue()){case "常规":font=Font.font(FontType,FontWeight.NORMAL,FontPosture.REGULAR,FontSize);break;case "粗体":font=Font.font(FontType,FontWeight.BOLD,FontPosture.REGULAR,FontSize);break;case "斜体":font=Font.font(FontType,FontWeight.NORMAL,FontPosture.ITALIC,FontSize);break;case "粗斜体":font=Font.font(FontType,FontWeight.BOLD,FontPosture.ITALIC,FontSize);break;}sampleTextArea.setFont(font);}//改变大小@FXMLvoid sizeChange(ActionEvent event) {FontSize=Integer.parseInt(sizeComboBox.getValue());font=Font.font(FontType,FontSize);sampleTextArea.setFont(font);}//确认后自动关闭设置字体的界面@FXMLvoid notepadFontChange(ActionEvent event) {noteTextArea.setFont(font);Stage stage=(Stage)sampleTextArea.getScene().getWindow();stage.close();}
}

3.2Font.fxml

<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?><AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FontController"><children><GridPane layoutX="51.0" layoutY="43.0"><columnConstraints><ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /><ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /><ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /></columnConstraints><rowConstraints><RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /><RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /></rowConstraints><children><ComboBox fx:id="fontComboBox" onAction="#fontChange" prefWidth="150.0" GridPane.rowIndex="1" /><ComboBox fx:id="styleComboBox" onAction="#styleChange" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="1" /><ComboBox fx:id="sizeComboBox" onAction="#sizeChange" prefWidth="150.0" GridPane.columnIndex="2" GridPane.rowIndex="1" /><Label text="字体" /><Label text="样式" GridPane.columnIndex="1" /><Label text="大小" GridPane.columnIndex="2" /></children></GridPane><TextArea fx:id="sampleTextArea" layoutX="43.0" layoutY="135.0" prefHeight="156.0" prefWidth="312.0" /><Button layoutX="158.0" layoutY="307.0" mnemonicParsing="false" onAction="#notepadFontChange" text="确认" /></children>
</AnchorPane>

4.帮助

4.1HelpController

import javafx.fxml.FXML;
import javafx.scene.control.Label;/*** @author xuchi* 2022年5月20日*/
public class HelpController {@FXMLprivate Label helpLabel;@FXMLvoid initialize(){helpLabel.setText("author:xuchi");}
}

4.2Help.fxml

<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?><AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.HelpController"><children><Label fx:id="helpLabel" layoutX="14.0" layoutY="14.0" text="Label" /></children>
</AnchorPane>

5.Main

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.scene.Parent;
import javafx.scene.Scene;public class Main extends Application {//创建一个Controller容器public static Map<String, Object> controllers = new HashMap<String, Object>();@Overridepublic void start(Stage primaryStage) {try {Parent root =FXMLLoader.load(getClass().getResource("Notepad.fxml"));Scene scene = new Scene(root,600,400);scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());primaryStage.setScene(scene);primaryStage.setTitle("新建");primaryStage.show();FileController controller = (FileController) Main.controllers.get(FileController.class.getSimpleName());primaryStage.setOnCloseRequest((EventHandler<WindowEvent>) new EventHandler<WindowEvent>() {@Overridepublic void handle(WindowEvent event){try{  //监听窗口关闭,询问是否保存,点击取消时,主界面继续运行,不能关闭if(controller.RequireSave())event.consume();} catch (IOException | InterruptedException e){// TODO Auto-generated catch blocke.printStackTrace();}}});} catch(Exception e) {e.printStackTrace();}}public static void main(String[] args) {launch(args);}
}

五.实验总结

1.不同controller之间的相互通信

我在做查找与替换的功能的时候,不知道怎么在FindController里面获取到FileController里的noteTextArea,也不知道怎么在一个controller里对另一个controller里的控件进行操作
解决办法:
在FileController里的initialize函数里写

Main.controllers.put(this.getClass().getSimpleName(), this);

然后在FindController里写

FileController controller = (FileController) Main.controllers
.get(FileController.class.getSimpleName());
noteTextArea=controller.getTextArea();

2.监听窗口关闭函数

primaryStage.setOnCloseRequest((EventHandler<WindowEvent>) new EventHandler<WindowEvent>() {@Overridepublic void handle(WindowEvent event){//这里写监听到之后要做的}});

3.展示其他fxml

 //展示查找与替换界面private void ShowFXML(String FxmlName,String title) throws IOException{// 初始化fxml界面Stage FindStage = new Stage();Parent root = FXMLLoader.load(getClass().getResource(FxmlName));Scene scene = new Scene(root, 350, 70);FindStage.setScene(scene);FindStage.setTitle(title);FindStage.show();}

4.文件类型过滤

//文件类型过滤
FileChooser.ExtensionFilter extensionFilter = new FileChooser.ExtensionFilter
("文本文档(*.txt)","*.txt");
fileChooser.getExtensionFilters().add(extensionFilter);

java期末大作业:记事本相关推荐

  1. java期末大作业设计_java程序设计-期末大作业报告模板.doc

    云南大学软件学院报告 Java programming – final Report School of Software, Yunnan University 个人成绩 序号学号姓名成绩12345 ...

  2. Java期末大作业——六级词汇学习系统

    1.任务描述: 用JavaSocket编程开发英语六级词汇学习游戏系统 (1) 在网上自行下载英语六级词汇表.存到文件中,格式自定. (2)系统支持两人连到服务器,测试学生对英语六级词汇的熟悉程度. ...

  3. Java期末大作业-工资系统平台(实验报告内附代码)

    目录 一.实践目的 二.实验环境 使用 三.实验内容 第一阶段(基础信息维护): 第二阶段(工资数据维护): 第三阶段(报表管理): 四.实验步骤(图文方式叙述) 第一阶段(基础信息维护): 第二阶段 ...

  4. JAVA期末大作业之学生信息管理简洁版系统

    一.界面化登录窗体,实现主要的界面布局 //package newpackage; import javax.swing.*; import java.awt.*; import java.awt.e ...

  5. Java期末大作业基础项目--在线学生选课系统

  6. 东华大学java_东华大学2020秋《Java程序设计》期末大作业

    东华大学继续教育学院 2020年秋季学期 远程学历教育<Java程序设计>期末大作业 一.选择题(本大题共10小题,每小题 1分, 共10分) 1.    下列哪个不是面向对象程序设计的基 ...

  7. 东华大学java_东华大学继续教育学院 2020年春季学期 远程学历教育《Java程序设计》期末大作业...

    2020年春季学期 远程学历教育<Java程序设计>期末大作业 班级            姓名           学号            成绩 项目        一        ...

  8. Java电话薄期末大作业

    标Java电话薄期末大作业 //主类 package phone;import java.io.*; import java.util.ArrayList; import java.util.Scan ...

  9. java期末作业_JAVA期末大作业 中国跳棋

    PS:这学期期末成绩差不多出完了,接下来会陆续把这学期课程中的代码陆续扔到这里来以便后人****,同时自己也留个纪念. 本学期选了java选修,期末大作业面向GitHub编程写了个中国跳棋.代码中还有 ...

  10. uml系统设计期末大作业_梳理一下计算机期末大作业

    H T M L 用过很多语言,其中就属html最难搞. 它的设计逻辑相对于C\C++和java不同,采用标记的方式.不过这也是它的设计初衷所体现出来的. 看一下作业,不仅要有设计,有排版,还要有程序动 ...

最新文章

  1. 交叉编译VIM并移植到ARM嵌入式Linux系统
  2. 让pip使用国内镜像,解决下载速度慢的问题
  3. python在中小学教学中的应用-为什么越来越多人学习python?中小学都要开始了?...
  4. jQuery框架的简单使用(H5)
  5. OpenGL 绘制grass草的实例
  6. 不能调试的问题的解决
  7. 1431. 拥有最多糖果的孩子
  8. [cocos2d-x]深入--几个代表性的类
  9. 一位女孩对男孩的忠告(转贴)
  10. 初始化和清理(构造器+重载/重写+this关键字)
  11. 小白快速入门Laravel 5.8框架
  12. CC2540蓝牙开发一BLE例程
  13. 工程项目成本管控,不知从何下手?
  14. 罗辑回归,Logistic Regression(or sigmoid function)
  15. 穆迪将收购GCR Ratings多数股权以拓展非洲业务
  16. DPDK:UDP 协议栈的实现
  17. Spring Security 配置 Remember Me
  18. Linux 系统管理(上部分)测试题
  19. 使用Flex实现常见布局的思路总结
  20. 麒麟OS 强制设置短密码

热门文章

  1. java jdom jar_jdom jar下载_jdom jar官方下载-太平洋下载中心
  2. windows微信协议|PC微信协议829版
  3. 大数据与AI平台:物联网时代的数据智能 PPT分享
  4. ERP企业管理系统与CRM客户关系管理系统集成套路
  5. [转] 你没看过的囧人囧事大集合
  6. Top 10 JavaScript编辑器,你在用哪个?
  7. 正点原子阿尔法linux开发板USB烧录裸机例程
  8. python电影爬取并下载_python爬取电影并下载
  9. 移动端身份证件OCR识别
  10. 基于树莓派的蓝牙音频接收器