📄 java记事本_增加关闭响应.java
字号:
if (result == JFileChooser.CANCEL_OPTION) {
statusLabel1.setText("您没有选择任何文件");
return;
}
saveFileName = fileChooser.getSelectedFile();
if (saveFileName == null || saveFileName.getName().equals(""))
JOptionPane.showMessageDialog(this, "不合法的文件名", "不合法的文件名", JOptionPane.ERROR_MESSAGE);
else {
saveFile();
Text.setText("");
this.setTitle("新建文本");
statusLabel1.setText(" 新建文本");
}
} else if (saveChoose == JOptionPane.NO_OPTION) {
Text.replaceRange("", 0, Text.getText().length());
statusLabel1.setText(" 新建文件");
this.setTitle("无标题 - 记事本");
isNewFile = true;
undo.discardAllEdits(); // 撤消所有的"撤消"操作
mEdit_Undo.setEnabled(false);
popupMenu_Undo.setEnabled(false);
undoButton.setEnabled(false);
redoButton.setEnabled(false);
oldValue = Text.getText();
} else if (saveChoose == JOptionPane.CANCEL_OPTION) {
return;
}
} else {
// Text.replaceRange("", 0, Text.getText().length());
Text.setText("");
statusLabel1.setText(" 新建文件");
this.setTitle("无标题 - 记事本");
isNewFile = true;
undo.discardAllEdits();
mEdit_Undo.setEnabled(false);
popupMenu_Undo.setEnabled(false);
undoButton.setEnabled(false);
oldValue = Text.getText();
}
}// 新建处理结束
// 打开
else if (e.getActionCommand().equals("打开(O)") || e.getSource() == openButton) {
Text.requestFocus();
String currentValue = Text.getText();
boolean isTextChange = (currentValue.equals(oldValue)) ? false : true;
if (isTextChange) {
int saveChoose = JOptionPane.showConfirmDialog(this, "您的文件尚未保存。是否保存?", "提示", JOptionPane.YES_NO_CANCEL_OPTION);
if (saveChoose == JOptionPane.YES_OPTION) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("另存为");
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.CANCEL_OPTION) {
statusLabel1.setText("您没有选择任何文件");
return;
}
saveFileName = fileChooser.getSelectedFile();
if (saveFileName == null || saveFileName.getName().equals(""))
JOptionPane.showMessageDialog(this, "不合法的文件名", "不合法的文件名", JOptionPane.ERROR_MESSAGE);
else {
saveFile();
isNewFile = false;
currentFile = saveFileName;
oldValue = Text.getText();
this.setTitle(saveFileName.getName() + " - 记事本");
statusLabel1.setText(" 当前打开文件:" + saveFileName.getAbsoluteFile());
}
} else if (saveChoose == JOptionPane.NO_OPTION) {
String str = null;
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("打开文件");
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.CANCEL_OPTION) {
statusLabel1.setText("您没有选择任何文件");
return;
}
fileName = fileChooser.getSelectedFile();
if (fileName == null || fileName.getName().equals(""))
JOptionPane.showMessageDialog(this, "不合法的文件名", "不合法的文件名", JOptionPane.ERROR_MESSAGE);
else {
try {
FileReader fr = new FileReader(fileName);
BufferedReader bfr = new BufferedReader(fr);
Text.setText("");
while ((str = bfr.readLine()) != null) {// 每次读取一行,直到文件结束
Text.append(str + "\15\12");
}// endwhile
this.setTitle(fileName.getName() + " - 记事本");
statusLabel1.setText(" 当前打开文件:" + fileName.getAbsoluteFile());
fr.close();
isNewFile = false;
currentFile = fileName;
oldValue = Text.getText();
} catch (IOException ioException) {
}
}
} else {
return;
}
}
else {
String str = null;
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("打开文件");
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.CANCEL_OPTION) {
statusLabel1.setText(" 您没有选择任何文件");
return;
}
fileName = fileChooser.getSelectedFile();
if (fileName == null || fileName.getName().equals(""))
JOptionPane.showMessageDialog(this, "不合法的文件名", "不合法的文件名", JOptionPane.ERROR_MESSAGE);
else {
try {
FileReader fr = new FileReader(fileName);
BufferedReader bfr = new BufferedReader(fr);
Text.setText("");
while ((str = bfr.readLine()) != null) {// 每次读取一行,直到文件结束
Text.append(str + "\15\12");
}// endwhile
this.setTitle(fileName.getName() + " - 记事本");
statusLabel1.setText(" 当前打开文件:" + fileName.getAbsoluteFile());
fr.close();
isNewFile = false;
currentFile = fileName;
oldValue = Text.getText();
} catch (IOException ioException) {
}
}
}
}// "打开"处理结束
// 保存
else if (e.getSource() == mFile_Save || e.getSource() == saveButton) {
Text.requestFocus();
if (isNewFile) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("另存为");
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.CANCEL_OPTION) {
statusLabel1.setText(" 您没有选择任何文件");
return;
}
saveFileName = fileChooser.getSelectedFile();
if (saveFileName == null || saveFileName.getName().equals(""))
JOptionPane.showMessageDialog(this, "不合法的文件名", "不合法的文件名", JOptionPane.ERROR_MESSAGE);
else {
saveFile();
isNewFile = false;
currentFile = saveFileName;
oldValue = Text.getText();
this.setTitle(saveFileName.getName() + " - 记事本");
statusLabel1.setText(" 当前打开文件:" + saveFileName.getAbsoluteFile());
}
} else {
try {
FileWriter fw = new FileWriter(currentFile);
BufferedWriter bfw = new BufferedWriter(fw);
bfw.write(Text.getText(), 0, Text.getText().length());
bfw.flush();
fw.close();
} catch (IOException ioException) {
}
}
}// "保存"处理结束
// 另存为
else if (e.getSource() == mFile_ASave || e.getSource() == saveAsButton) {
Text.requestFocus();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("另存为");
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.CANCEL_OPTION) {
statusLabel1.setText(" 您没有选择任何文件");
return;
}
saveFileName = fileChooser.getSelectedFile();
if (saveFileName == null || saveFileName.getName().equals(""))
JOptionPane.showMessageDialog(this, "不合法的文件名", "不合法的文件名", JOptionPane.ERROR_MESSAGE);
else {
saveFile();
isNewFile = false;
currentFile = saveFileName;
oldValue = Text.getText();
this.setTitle(saveFileName.getName() + " - 记事本");
statusLabel1.setText(" 当前打开文件:" + saveFileName.getAbsoluteFile());
}
}// "另存为"处理结束
// 打印
else if (e.getSource() == mFile_Print || e.getSource() == printButton) {
Text.requestFocus();
JOptionPane.showMessageDialog(this, "此功能尚未添加!", "提示", JOptionPane.WARNING_MESSAGE);
}
// 退出
else if (e.getSource() == mFile_Exit) {
int exitChoose = JOptionPane.showConfirmDialog(this, "确定要退出么?", "退出提示", JOptionPane.OK_CANCEL_OPTION);
if (exitChoose == JOptionPane.OK_OPTION) {
checkText();
} else {
return;
}
}
// 撤消
else if (e.getSource() == mEdit_Undo || e.getSource() == popupMenu_Undo || e.getSource() == undoButton) {
Text.requestFocus();
if (undo.canUndo()) {
try {
undo.undo();
} catch (CannotUndoException ex) {
System.out.println("Unable to undo: " + ex);
ex.printStackTrace();
}
if (!undo.canUndo()) {
mEdit_Undo.setEnabled(false);
popupMenu_Undo.setEnabled(false);
undoButton.setEnabled(false);
}
}
}
// 剪切
else if (e.getSource() == mEdit_Cut || e.getSource() == popupMenu_Cut || e.getSource() == cutButton) {
Text.requestFocus();
String text = Text.getSelectedText();
StringSelection selection = new StringSelection(text);
clipBoard.setContents(selection, null);
Text.replaceRange("", Text.getSelectionStart(), Text.getSelectionEnd());
checkMenuItemEnabled(); // 设置剪切、复制、粘贴、删除等功能的可用性
}
// 复制
else if (e.getSource() == mEdit_Copy || e.getSource() == popupMenu_Copy || e.getSource() == copyButton) {
Text.requestFocus();
String text = Text.getSelectedText();
StringSelection selection = new StringSelection(text);
clipBoard.setContents(selection, null);
checkMenuItemEnabled(); // 设置剪切、复制、粘贴、删除等功能的可用性
}
// 粘贴
else if (e.getSource() == mEdit_Paste || e.getSource() == popupMenu_Paste || e.getSource() == pasteButton) {
Text.requestFocus();
Transferable contents = clipBoard.getContents(this);
if (contents == null)
return;
String text;
text = "";
try {
text = (String) contents.getTransferData(DataFlavor.stringFlavor);
} catch (Exception exception) {
}
Text.replaceRange(text, Text.getSelectionStart(), Text.getSelectionEnd());
checkMenuItemEnabled(); // 设置剪切、复制、粘贴、删除等功能的可用性
}
// 删除
else if (e.getSource() == mEdit_Del || e.getSource() == popupMenu_Delete || e.getSource() == deleteButton) {
Text.requestFocus();
Text.replaceRange("", Text.getSelectionStart(), Text.getSelectionEnd());
checkMenuItemEnabled(); // 设置剪切、复制、粘贴、删除等功能的可用性
}
// 查找
else if (e.getSource() == mEdit_Search || e.getSource() == searchButton) {
Text.requestFocus();
if (e.getSource() == searchButton) {
Text.requestFocus();
Text.setCaretPosition(0);
}
mySearch();
}
// 查找下一个(此功能尚未能很好实现,所以就先用查找功能来代替)
else if (e.getSource() == mEdit_SearchNext) {
mySearch();
}
// 替换(与查找功能集成在一起了)
else if (e.getSource() == mEdit_Replace) {
mySearch();
}
// 转到
else if (e.getSource() == mEdit_Turnto) {
final JDialog gotoDialog = new JDialog(this, "转到下列行");
JLabel gotoLabel = new JLabel("行数(L):");
final JTextField linenum = new JTextField(5);
linenum.setText("1");
linenum.selectAll();
JButton okButton = new JButton("确定");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int totalLine = Text.getLineCount();
int[] lineNumber = new int[totalLine + 1];
String s = Text.getText();
int pos = 0, t = 0;
while (true) {
pos = s.indexOf('\12', pos);
// System.out.println("引索pos:"+pos);
if (pos == -1)
break;
lineNumber[t++] = pos++;
}
int gt = 1;
try {
gt = Integer.parseInt(linenum.getText());
} catch (NumberFormatException efe) {
JOptionPane.showMessageDialog(null, "请输入行数!", "提示", JOptionPane.WARNING_MESSAGE);
linenum.requestFocus(true);
return;
}
if (gt < 2 || gt >= totalLine) {
if (gt < 2)
Text.setCaretPosition(0);
else
Text.setCaretPosition(s.length());
} else
Text.setCaretPosition(lineNumber[gt - 2] + 1);
gotoDialog.dispose();
}
});
JButton cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
gotoDialog.dispose();
}
});
Container con = gotoDialog.getContentPane();
con.setLayout(new FlowLayout());
con.add(gotoLabel);
con.add(linenum);
con.add(okButton);
con.add(cancelButton);
gotoDialog.setSize(200, 100);
gotoDialog.setResizable(false);
gotoDialog.setLocation(300, 280);
gotoDialog.setVisible(true);
}// "转到"处理结束
// 插入时间日期
else if (e.getSource() == mEdit_TimeDate || e.getSource() == timeButton) {
Text.requestFocus();
SimpleDateFormat currentDateTime = new SimpleDateFormat("HH:mm yyyy-MM-dd");
Text.insert(currentDateTime.format(new Date()), Text.getCaretPosition());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -