📄 notepad.java
字号:
isTitled = false;
fileName = "无标题";
setTitle(fileName + "-记事本");
}
}
// 新建的文件
else if (!isTitled) {
String strTemp = note.getText();
// 如果工作区中有文字,工作区不为空
if (strTemp.length() != 0) {
int isExit = JOptionPane.showConfirmDialog(null, "文件 "
+ fileName + " 的文字已经改变,想保存文件吗 ?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION);
if (isExit == JOptionPane.YES_OPTION) {
if (isTitled == false) {// 非打开的文件,没有文件名
int result = fileChooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
writeFile(file);
note.setText("");
isTitled = false;
fileName = "无标题";
filePath = null;
setTitle(fileName + "-记事本");
}
} else {// 打开的文件,文件名存在
File file = new File(filePath);
writeFile(file);
}
} else if (isExit == JOptionPane.NO_OPTION) {
note.setText("");
isTitled = false;
fileName = "无标题";
setTitle(fileName + "-记事本");
} else
return;
}
// 工作区为空,直接新建
else {
note.setText("");
isTitled = false;
fileName = "无标题";
setTitle(fileName + "-记事本");
}
}
}
/** ******打开文件********OK */
else if (e.getSource() == open) {
// 打开的文件
if (isTitled) {
int newHashCode = 0;
String fileContent = note.getText();
newHashCode = fileContent.hashCode();
// 如果文件内容被修改了,打开文件之前询问是否保存文件
if (hashCode != newHashCode) {
int isExit = JOptionPane.showConfirmDialog(null, "文件 "
+ filePath + " 的文字已经改变,想保存文件吗 ?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION);
if (isExit == JOptionPane.YES_OPTION) {
// save option
File fileToSave = new File(filePath);
writeFile(fileToSave);
// open option
fileChooser
.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
fileName = file.getName();
filePath = file.getAbsolutePath();
String title = fileName + "-记事本";
setTitle(title);
isTitled = true;
readFile(file);
str1 = note.getText();
}
} else if (isExit == JOptionPane.NO_OPTION) {
fileChooser
.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
fileName = file.getName();
filePath = file.getAbsolutePath();
String title = fileName + "-记事本";
setTitle(title);
isTitled = true;
readFile(file);
str1 = note.getText();
}
} else
return;
}
// 打开的文件,而且文件内容没有被修改,直接打开
else {
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
fileName = file.getName();
filePath = file.getAbsolutePath();
String title = fileName + "-记事本";
setTitle(title);
isTitled = true;
readFile(file);
str1 = note.getText();
}
}
}
// 新建的文件
else if (!isTitled) {
String strTemp = note.getText();
// 如果工作区中有文字,工作区不为空,打开文件时询问是否保存文件
if (strTemp.length() != 0) {
int isExit = JOptionPane.showConfirmDialog(null, "文件 "
+ fileName + " 的文字已经改变,想保存文件吗 ?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION);
if (isExit == JOptionPane.YES_OPTION) {
int result = fileChooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
// save option
File fileToSave = fileChooser.getSelectedFile();
writeFile(fileToSave);
// open option
fileChooser
.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result1 = fileChooser.showOpenDialog(null);
if (result1 == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
fileName = file.getName();
filePath = file.getAbsolutePath();
String title = fileName + "-记事本";
setTitle(title);
isTitled = true;
readFile(file);
str1 = note.getText();
}
}
} else if (isExit == JOptionPane.NO_OPTION) {
fileChooser
.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
fileName = file.getName();
filePath = file.getAbsolutePath();
String title = fileName + "-记事本";
setTitle(title);
isTitled = true;
readFile(file);
str1 = note.getText();
}
} else
return;
}
// 工作区为空,直接打开
else {
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
fileName = file.getName();
filePath = file.getAbsolutePath();
String title = fileName + "-记事本";
setTitle(title);
isTitled = true;
readFile(file);
str1 = note.getText();
}
}
}
}
/** ******保存文件*********OK */
else if (e.getSource() == save) {
if (!isTitled) {// 非打开的文件,文件名为空
int result = fileChooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
fileName = file.getName();
filePath = file.getAbsolutePath();
isTitled = true;
writeFile(file);
setTitle(fileName + "-记事本");
}
} else {// 打开的文件
File file = new File(filePath);
writeFile(file);
}
}
/** *******文件另存为**********OK* */
else if (e.getSource() == reSave) {
int result = fileChooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
fileName = file.getName();
filePath = file.getAbsolutePath();
isTitled = true;
writeFile(file);
setTitle(fileName + "-记事本");
}
}
/** *****复制操作*****OK */
else if (e.getSource() == copy || e.getSource() == popCopy) {
// 将选中的文本保存在剪切板上
String text = note.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
}
/** *****剪切操作*****OK */
else if (e.getSource() == cut || e.getSource() == popCut) {
String text = note.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
note.replaceRange("", note.getSelectionStart(), note
.getSelectionEnd());
}
/** *****粘贴操作*****OK */
else if (e.getSource() == paste || e.getSource() == popPaste) {
Transferable contents = clipboard.getContents(this);
if (contents == null)
return;
String text = "";
try {
text = (String) contents
.getTransferData(DataFlavor.stringFlavor);
} catch (Exception de) {
}
note.replaceRange(text, note.getSelectionStart(), note
.getSelectionEnd());
}
/** ****退出操作******OK */
else if (e.getSource() == quit) {
onWindowClosing();
}
/** ******格式菜单中 自动换行*******OK* */
else if (e.getSource() == wrap) {
if (wrap.isSelected()) {
note.setLineWrap(true);
note.setWrapStyleWord(true);//自动换行时不拆分单词
} else
note.setLineWrap(false);
}
/** ********全选************OK */
else if (e.getSource() == selectAll || e.getSource() == popSelectAll) {
note.selectAll();
}
/** ******时间和日期**********OK */
else if (e.getSource() == dateAndTime) {
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd",
Locale.SIMPLIFIED_CHINESE);
sdf.applyPattern("yyyy年MM月yy日-HH时mm分ss秒");
String timeStr = sdf.format(new Date());
note.insert(timeStr, note.getCaretPosition());
}
/** *****删除******OK* */
else if (e.getSource() == delete || e.getSource() == popDelete) {
// 将选中的文字用""代替
note.replaceSelection("");
}
/** *******查找******OK* */
else if (e.getSource() == find) {
findDlg = new FindDlg(this);
findDlg.setBounds(400, 300, 400, 140);
findDlg.setVisible(true);
}
/** *****查找下一个********OK */
else if (e.getSource() == findNext) {
findDlg = new FindDlg(this);
findDlg.setBounds(400, 300, 400, 140);
findDlg.setVisible(true);
}
/** ******替换*************OK* */
else if (e.getSource() == replace) {
replaceDlg = new ReplaceDlg(this);
replaceDlg.setBounds(400, 300, 400, 150);
replaceDlg.setVisible(true);
}
/** ********转到*************** */
else if (e.getSource() == goTo) {
goToDlg = new GoToDlg(this);
goToDlg.setBounds(400, 300, 350, 100);
goToDlg.setVisible(true);
}
/** ********字体**********OK** */
else if (e.getSource() == font) {
fontDlg = new FontDlg(this);
Dimension size = fontDlg.getSize();
fontDlg.setBounds(400, 300, size.width, size.height);
fontDlg.setVisible(true);
}
/********状态栏*******/
else if(e.getSource() == status){
if (status.isSelected()) {
statusBar.setVisible(true);
} else
statusBar.setVisible(false);
}
/*******撤销功能*********OK(只能撤销一次)*/
else if(e.getSource() == retract){
if(retractNum == 0){
str2 = note.getText();
note.setText(str1);
retractNum = 1;
}
else {
str1 = note.getText();
note.setText(str2);
retractNum = 0;
}
}
}
/**
* 主函数
*
* @param args
* 没有使用到这个参数
*/
public static void main(String args[]) {
NotePad notepad = new NotePad();
notepad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -