📄 notepad.java
字号:
}
}
}
else if(message == 2)//重新回到编辑窗口
{
return;
}
}
try
{
fdg.setVisible(true);
fileDir = fdg.getDirectory();
fileName = fdg.getFile();
br = new BufferedReader(new FileReader(new File(fileDir, fileName)));
jta.setText("");
while((str = br.readLine()) != null)
{
textStrb = textStrb.append(str).append("\n");
}
jta.setText(textStrb.toString());
lastContent = jta.getText();
br.close();
Notepad.this.setTitle("CoffeeEditor " + "[" +
fileDir + fileName + "]");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(fileOpenItem,
ex.toString() + ":\n读取文件出错!",
"出错啦!", JOptionPane.INFORMATION_MESSAGE);
}
}
});
//新建文件菜单项
newFileItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FileDialog fdgNew = new FileDialog(Notepad.this, "新建", FileDialog.SAVE);
FileDialog fdg = new FileDialog(Notepad.this, "保存", FileDialog.SAVE);
String str = jta.getText();
BufferedWriter bw = null;
if(!str.equals(lastContent))
{
int message = JOptionPane.showConfirmDialog(newFileItem,
"是否保存文件?","保存",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if(message == 0)
{
if((fileName == null) || (fileDir == null))
{
fdg.setVisible(true);
fileDir = fdg.getDirectory();
fileName = fdg.getFile();
try
{
bw = new BufferedWriter(new FileWriter(new File(fileDir, fileName)));
bw.write(str);
bw.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(newFileItem,
ex.toString() + ":\n写入文件出错!",
"出错啦!", JOptionPane.INFORMATION_MESSAGE);
}
}
else
{
try
{
bw = new BufferedWriter(new FileWriter(new File(fileDir, fileName)));
bw.write(str);
bw.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(newFileItem,
ex.toString() + ":\n写入文件出错!",
"出错啦!", JOptionPane.INFORMATION_MESSAGE);
}
}
}
else if(message == 2)//重新回到编辑窗口
{
return;
}
}
//创建新文件
fdgNew.setVisible(true);
fileDir = fdgNew.getDirectory();
fileName = fdgNew.getFile();
try
{
bw = new BufferedWriter(new FileWriter(new File(fileDir, fileName)));
jta.setText("");
bw.close();
Notepad.this.setTitle("CoffeeEditor " + "[" +
fileDir + fileName + "]");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(newFileItem,
ex.toString() + ":\n新建文件出错!",
"出错啦!", JOptionPane.INFORMATION_MESSAGE);
}
}
});
fileMenu.add(fileOpenItem);
fileMenu.add(newFileItem);
fileMenu.add(saveItem);
fileMenu.add(fileSaveItem);
fileMenu.add(systemExitMenuItem);
menuBar.add(fileMenu);
//复制菜单项
copyItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.copy();
pasteItem.setEnabled(true);
popPasteItem.setEnabled(true);
}
});
//粘贴菜单项
pasteItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.paste();
}
});
//剪切菜单项
cutItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.cut();
pasteItem.setEnabled(true);
popPasteItem.setEnabled(true);
}
});
//删除菜单项
deleteItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.cut();
pasteItem.setEnabled(false);
popPasteItem.setEnabled(false);
}
});
//查找菜单项
findItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String tempWord = JOptionPane.showInputDialog(findItem,
"要查找的单词:", "查找", JOptionPane.PLAIN_MESSAGE);
String content = jta.getText();
int findLength = tempWord.length();
int conLength = content.length();
if(findWord.equals(tempWord))
{
if((start <= conLength - findLength) && (start >=0))
{
start = content.indexOf(findWord, start);
if(start == -1)
throw new Exception();
jta.select(start, start + findLength);
start = start + findLength;
}
}
else
{
start = 0;
findWord = tempWord;
start = content.indexOf(findWord, start);
if(start == -1)
throw new Exception();
jta.select(start, start + findLength);
start = start + findLength;
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(findItem,
ex.toString() + ":\n没有找到!", "出错啦!",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
//定位菜单项
gotoItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
int start0 = 0;
String textTemp = jta.getText();
int index = Integer.parseInt(JOptionPane.showInputDialog(gotoItem,
"定位到行数:", "定位到行", JOptionPane.PLAIN_MESSAGE));
for(int i = 1; i < index; i++)
{
start0 = textTemp.indexOf("\n", start0) + 1;
if(start0 == 0)
throw new Exception("行数越界:");
}
jta.select(start0, textTemp.indexOf("\n", start0));
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(gotoItem,
ex.toString() + ":\n输入有误!", "出错啦!",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
//全选菜单项
selectItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.selectAll();
}
});
//单个替换菜单项
subSingleItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String textTemp = jta.getText();
int selectStart = jta.getSelectionStart();
int selectEnd = jta.getSelectionEnd();
String sourceStr = "";
String substitudeStr = "";
try
{
if(textTemp.substring(selectStart, selectEnd).equals(""))
{
sourceStr = JOptionPane.showInputDialog(subSingleItem,
"要替换的文本:", "替换第一个", JOptionPane.PLAIN_MESSAGE);
substitudeStr = JOptionPane.showInputDialog(subSingleItem,
"替换为文本:", "替换为", JOptionPane.PLAIN_MESSAGE);
textTemp = textTemp.replaceFirst(sourceStr, substitudeStr);
jta.setText(textTemp);
int length = substitudeStr.length();
selectStart = textTemp.indexOf(substitudeStr);
selectEnd = selectStart + length;
jta.select(selectStart, selectEnd);
}
else
{
sourceStr = textTemp.substring(selectStart, selectEnd);
substitudeStr = JOptionPane.showInputDialog(subSingleItem,
"替换为文本:", "替换为", JOptionPane.PLAIN_MESSAGE);
textTemp = textTemp.substring(0, selectStart) +
substitudeStr + textTemp.substring(selectEnd);
jta.setText(textTemp);
int length = substitudeStr.length();
selectEnd = selectStart + length;
jta.select(selectStart, selectEnd);
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(subSingleItem,
ex.toString() + ":\n输入有误!", "出错啦!",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
//全部替换菜单项
subWholeItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String textTemp = jta.getText();
int selectStart = jta.getSelectionStart();
int selectEnd = jta.getSelectionEnd();
String sourceStr = "";
String substitudeStr = "";
try
{
if(textTemp.substring(selectStart, selectEnd).equals(""))
{
sourceStr = JOptionPane.showInputDialog(subSingleItem,
"要替换的文本:", "全部替换", JOptionPane.PLAIN_MESSAGE);
}
else
{
sourceStr = textTemp.substring(selectStart, selectEnd);
}
substitudeStr = JOptionPane.showInputDialog(subSingleItem,
"替换为文本:", "替换为", JOptionPane.PLAIN_MESSAGE);
textTemp = textTemp.replace(sourceStr, substitudeStr);
jta.setText(textTemp);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(subWholeItem,
ex.toString() + ":\n输入有误!", "出错啦!",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
//转换为大写菜单项
toUpItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String textTemp = jta.getText();
int selectStart = jta.getSelectionStart();
int selectEnd = jta.getSelectionEnd();
String sourceStr = textTemp.substring(selectStart, selectEnd);
try
{
if(sourceStr.equals(""))
{
jta.setText(textTemp.toUpperCase());
}
else
{
sourceStr = sourceStr.toUpperCase();
textTemp = textTemp.substring(0, selectStart) +
sourceStr + textTemp.substring(selectEnd);
jta.setText(textTemp);
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(toUpItem,
ex.toString() + ":\n输入有误!", "出错啦!",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
//转换为小写菜单项
toLowItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String textTemp = jta.getText();
int selectStart = jta.getSelectionStart();
int selectEnd = jta.getSelectionEnd();
String sourceStr = textTemp.substring(selectStart, selectEnd);
try
{
if(sourceStr.equals(""))
{
jta.setText(textTemp.toLowerCase());
}
else
{
sourceStr = sourceStr.toLowerCase();
textTemp = textTemp.substring(0, selectStart) +
sourceStr + textTemp.substring(selectEnd);
jta.setText(textTemp);
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(toLowItem,
ex.toString() + ":\n输入有误!", "出错啦!",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
//大小写转换菜单项
upLowItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String textTemp = jta.getText();
StringBuffer temp = new StringBuffer();
int selectStart = jta.getSelectionStart();
int selectEnd = jta.getSelectionEnd();
final int LEN = 100;
int loop = 0;
int last = 0;
int length = 0;
String sourceStr = textTemp.substring(selectStart, selectEnd);
try
{
if(sourceStr.equals(""))
{
length = textTemp.length();
loop = length / LEN;
last = length % LEN;
for(int i = 1; i <= loop; i++)
{
sourceStr = textTemp.substring((i-1)*LEN, i*LEN);
sourceStr = Notepad.this.upLowSwitch(sourceStr, LEN);
temp = temp.append(sourceStr);
}
sourceStr = textTemp.substring(loop * LEN);
sourceStr = Notepad.this.upLowSwitch(sourceStr, last);
temp = temp.append(sourceStr);
jta.setText(temp.toString());
}
else
{
String sourceTemp = new String(sourceStr);
length = sourceStr.length();
loop = length / LEN;
last = length % LEN;
for(int i = 1; i <= loop; i++)
{
sourceStr = sourceTemp.substring((i-1)*LEN, i*LEN);
sourceStr = Notepad.this.upLowSwitch(sourceStr, LEN);
temp = temp.append(sourceStr);
}
sourceStr = sourceTemp.substring(loop * LEN);
sourceStr = Notepad.this.upLowSwitch(sourceStr, last);
temp = temp.append(sourceStr);
temp = new StringBuffer(textTemp.substring(0, selectStart)).append(
temp).append(textTemp.substring(selectEnd));
jta.setText(temp.toString());
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(upLowItem,
ex.toString() + ":\n输入有误!", "出错啦!",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
editMenu.add(copyItem);
editMenu.add(pasteItem);
editMenu.add(cutItem);
editMenu.add(deleteItem);
changeMenu.add(toLowItem);
changeMenu.add(toUpItem);
changeMenu.add(upLowItem);
editMenu.add(changeMenu);
editMenu.add(findItem);
editMenu.add(gotoItem);
editMenu.add(selectItem);
subMenu.add(subSingleItem);
subMenu.add(subWholeItem);
editMenu.add(subMenu);
menuBar.add(editMenu);
//翻译菜单项
transItem.addActionListener(new ActionListener()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -