📄 notepad.java
字号:
flow.setAlignment(FlowLayout.RIGHT);
statusBar = new JPanel();
statusBar.setLayout(flow);
tfStatus = new JTextField("这是状态栏,还未处理好,见谅!",20);
tfStatus.setEditable(false);
statusBar.add(tfStatus);
add(statusBar,BorderLayout.SOUTH);
statusBar.setVisible(false);
//状态栏完毕
/** ***********添加事件监听*********** */
// ----文件菜单
build.addActionListener(this); // OK
open.addActionListener(this); // OK
save.addActionListener(this); // OK
reSave.addActionListener(this); // OK
setPage.addActionListener(this);
print.addActionListener(this);
quit.addActionListener(this); // OK
// ----编辑菜单
retract.addActionListener(this);// mostly OK
cut.addActionListener(this); // OK
copy.addActionListener(this); // OK
paste.addActionListener(this); // OK
delete.addActionListener(this); // OK
find.addActionListener(this); // OK
findNext.addActionListener(this); // OK
replace.addActionListener(this); // OK
goTo.addActionListener(this); // not compeleted
selectAll.addActionListener(this); // OK
dateAndTime.addActionListener(this); // OK
// ----格式菜单
wrap.addActionListener(this); // OK
font.addActionListener(this); // OK
// ----查看菜单
status.addActionListener(this); // not compeleted
// ----帮助菜单
title.addActionListener(this);
about.addActionListener(this);
// 右键菜单
popRetract.addActionListener(this);
popCut.addActionListener(this); // OK
popCopy.addActionListener(this); // OK
popPaste.addActionListener(this); // OK
popDelete.addActionListener(this); // OK
popSelectAll.addActionListener(this); // OK
}
/**
* 转到文档中相应的行
*
* @param line
* 待转到行号
* @return 跳转成功返回true,否则返回false
*/
public boolean goTo(int line) {
int documentLine = note.getLineCount();
if (line > documentLine)
return false;
else {
note.setCaretPosition(line);
return true;
}
}
/**
* 将文件加载到记事本中
*
* @param file
* 要加载的文件对象
*/
public void readFile(File file) {
try {
FileReader readIn = new FileReader(file);
int size = (int) file.length();
int charsRead = 0;
char content[] = new char[size];
while (readIn.ready()) {
charsRead += readIn.read(content, charsRead, size - charsRead);
}
readIn.close();
String fileContent = new String(content, 0, charsRead);
hashCode = fileContent.hashCode();
note.setText(fileContent);
} catch (IOException ee) {
}
}
/**
* 将记事本中的文本写入文件中
*
* @param file
* 要写入的文件对象
*/
public void writeFile(File file) {
try {
FileWriter writeFile = new FileWriter(file);
writeFile.write(note.getText());
writeFile.close();
} catch (IOException eeee) {
}
}
/**
* 用replaceTo替换文档中全部的key
*
* @param key
* 被替换的关键字
* @param replaceTo
* 被替换成的文字
* @param ignoreCase
* 是否忽略大小写
* @return 替换成功返回true,否则返回false
*/
public void replaceAll(String key, String replaceTo, boolean ignoreCase) {
boolean hasKey = false;
String content = note.getText();
int contentLength = content.length();
int keyLength = key.length();
String first = String.valueOf(key.charAt(0));
// 先搜索看看是否存在key在文档中
for (int i = mileStone; i < contentLength; i++) {
String temp = String.valueOf(content.charAt(i));
if (ignoreCase) {
if (first.equalsIgnoreCase(temp)) {
temp = content.substring(i, i + keyLength);
if (temp.equalsIgnoreCase(key)) {
hasKey = true;
break;
}
}
} else {
if (first.equals(temp)) {
temp = content.substring(i, i + keyLength);
if (temp.equals(key)) {
hasKey = true;
break;
}
}
}
}
if (!hasKey) {
JOptionPane.showMessageDialog(null, "找不到关键字 \"" + key + "\"");
} else {
for (int i = 0; i < contentLength; i++) {
if (!OnFindMenuItem(key, ignoreCase)) {
return;
} else {
note.replaceSelection(replaceTo);
}
}
}
}
/**
* 处理 ‘替换’ 菜单项中查找工作的函数
*
* @param key
* 要查找的关键字
* @param ignoreCase
* 是否忽略大小写
* @return 如果没找到该关键字,返回false,否则返回true
*/
public boolean OnFindMenuItem(String key, boolean ignoreCase) {
String content = note.getText();
int contentLength = content.length();
int keyLength = key.length();
String first = String.valueOf(key.charAt(0));
for (int i = mileStone; i < contentLength; i++) {
String temp = String.valueOf(content.charAt(i));
if (ignoreCase) {
if (first.equalsIgnoreCase(temp)) {
temp = content.substring(i, i + keyLength);
if (temp.equalsIgnoreCase(key)) {
note.select(i, i + keyLength);
mileStone = i + 1;
return true;
}
}
} else {
if (first.equals(temp)) {
temp = content.substring(i, i + keyLength);
if (temp.equals(key)) {
note.select(i, i + keyLength);
mileStone = i + 1;
return true;
}
}
}
}
return false;
}
/**
* 处理 ‘查找’ 菜单项中查找工作的函数
*
* @param key
* 查找的关键字
* @param ignoreCase
* 是否忽略大小写
* @param isUp
* 是否从下到上查找
* @return 如果没找到该关键字,返回false,否则返回true
*/
public boolean OnFindMenuItem(String key, boolean ignoreCase, boolean isUp) {
String content = note.getText();
int contentLength = content.length();
int keyLength = key.length();
int caretPosition = note.getCaretPosition();
if (!isUp) {// 向下搜索
String first = String.valueOf(key.charAt(0));
for (int i = caretPosition; i < contentLength; i++) {
String temp = String.valueOf(content.charAt(i));
if (ignoreCase) {
if (first.equalsIgnoreCase(temp)) {
temp = content.substring(i, i + keyLength);
if (temp.equalsIgnoreCase(key)) {
note.select(i, i + keyLength);
return true;
}
}
} else {
if (first.equals(temp)) {
temp = content.substring(i, i + keyLength);
if (temp.equals(key)) {
note.select(i, i + keyLength);
return true;
}
}
}
}
} else {// 向上搜索
String last = String.valueOf(key.charAt(keyLength - 1));
// 查找对话框出现之后 第一次 调用本函数
if (isFirstCall) {
for (int i = caretPosition - 1; i > 0; i--) {
String temp = String.valueOf(content.charAt(i));
if (ignoreCase) {
if (last.equalsIgnoreCase(temp)) {
temp = content.substring(i - keyLength + 1, i + 1);
if (temp.equalsIgnoreCase(key)) {
mileStone = i - 1;
isFirstCall = false;
note.select(i - keyLength + 1, i + 1);
return true;
}
}
} else {
if (last.equals(temp)) {
temp = content.substring(i - keyLength + 1, i + 1);
if (temp.equals(key)) {
mileStone = i - 1;
isFirstCall = false;
note.select(i - keyLength + 1, i + 1);
return true;
}
}
}
}
}
// 查找对话框出现之后不是 第一次 调用本函数
else {
for (int i = mileStone; i > 0; i--) {
String temp = String.valueOf(content.charAt(i));
if (ignoreCase) {
if (last.equalsIgnoreCase(temp)) {
temp = content.substring(i - keyLength + 1, i + 1);
if (temp.equalsIgnoreCase(key)) {
mileStone = i - 1;
isFirstCall = false;
note.select(i - keyLength + 1, i + 1);
return true;
}
}
} else {
if (last.equals(temp)) {
temp = content.substring(i - keyLength + 1, i + 1);
if (temp.equals(key)) {
mileStone = i - 1;
isFirstCall = false;
note.select(i - keyLength + 1, i + 1);
return true;
}
}
}
}
}
}
return false;
}
/**
* 菜单栏事件处理函数
*/
public void actionPerformed(ActionEvent e) {
/** ***新建文件*****OK */
if (e.getSource() == build) {
// 打开的文件
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) {
File file = new File(filePath);
writeFile(file);
note.setText("");
isTitled = false;
fileName = "无标题";
filePath = null;
setTitle(fileName + "-记事本");
} else if (isExit == JOptionPane.NO_OPTION) {
note.setText("");
isTitled = false;
fileName = "无标题";
setTitle(fileName + "-记事本");
} else
return;
}
// 打开的文件,而且文件内容没有被修改,直接新建
else {
note.setText("");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -