📄 java记事本_增加关闭响应.java
字号:
// --------------------------------------向容器添加工具栏
container.add(toolBar, BorderLayout.NORTH);
// -----------------------------------创建和添加状态栏
statusBar = new JToolBar();
statusBar.setLayout(new FlowLayout(FlowLayout.LEFT));
statusLabel1 = new JLabel("按F1获取帮助 ");
statusLabel2 = new JLabel(" 当前时间:" + hour + ":" + min + ":" + second);
statusLabel3 = new JLabel(" 当前光标所在行数" + getlineNumber());
statusBar.add(statusLabel1);
statusBar.addSeparator();
statusBar.add(statusLabel2);
statusBar.addSeparator();
statusBar.add(statusLabel3);
container.add(statusBar, BorderLayout.SOUTH);
statusBar.setVisible(true);
// ------------------------------------改变标题栏窗口左侧默认图标
Toolkit tk = Toolkit.getDefaultToolkit();
Image image = tk.createImage("Icons/notepad.gif");
this.setIconImage(image);
this.setJMenuBar(MenuBar); // 向窗口添加菜单条
container.add(scroll, BorderLayout.CENTER); // 向容器添加文本编辑区
this.pack();
this.setSize(800, 800);
this.setVisible(true);
checkMenuItemEnabled();
Text.requestFocus();
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
checkText();
}
});
Clock clock = new Clock();
clock.start();
}
public void checkText() {
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();
Text.setText("");
this.setTitle("新建文本");
statusLabel1.setText(" 新建文本");
}
}
else if (saveChoose == JOptionPane.NO_OPTION) {
System.exit(0);
}
else if (saveChoose == JOptionPane.CANCEL_OPTION) {
Text.requestFocus();
}
}
else if (!isTextChange) {
System.exit(0);
}
}
public void checkMenuItemEnabled() {
String selectText = Text.getSelectedText();
if (selectText == null) {
mEdit_Cut.setEnabled(false);
popupMenu_Cut.setEnabled(false);
cutButton.setEnabled(false);
mEdit_Copy.setEnabled(false);
popupMenu_Copy.setEnabled(false);
copyButton.setEnabled(false);
mEdit_Del.setEnabled(false);
popupMenu_Delete.setEnabled(false);
deleteButton.setEnabled(false);
} else {
mEdit_Cut.setEnabled(true);
popupMenu_Cut.setEnabled(true);
cutButton.setEnabled(true);
mEdit_Copy.setEnabled(true);
popupMenu_Copy.setEnabled(true);
copyButton.setEnabled(true);
mEdit_Del.setEnabled(true);
popupMenu_Delete.setEnabled(true);
deleteButton.setEnabled(true);
}
// 粘贴功能可用性判断
Transferable contents = clipBoard.getContents(this);
if (contents == null) {
mEdit_Paste.setEnabled(false);
popupMenu_Paste.setEnabled(false);
pasteButton.setEnabled(false);
} else {
mEdit_Paste.setEnabled(true);
popupMenu_Paste.setEnabled(true);
pasteButton.setEnabled(true);
}
}
public int getlineNumber() {
int totalLine = Text.getLineCount();
int[] lineNumber = new int[totalLine + 1];
int pos = 0, t = 0, num = 0, i = 0;
String s = Text.getText();
while (true) {
pos = s.indexOf('\12', pos); // 返回 \n 所在的位置
if (pos == -1)
break;
lineNumber[t++] = pos++;
}
if (Text.getCaretPosition() <= lineNumber[0])
num = 1;
else {
if (Text.getCaretPosition() > lineNumber[Text.getLineCount() - 1])
num = Text.getLineCount();
for (i = 0; i < totalLine + 1; i++) {
if (Text.getCaretPosition() <= lineNumber[i]) {
num = i + 1;
break;
} else
continue;
}
}
return num;
}
public void saveFile() {
try {
FileWriter fw = new FileWriter(saveFileName);
fw.write(Text.getText());
fw.close();
} catch (Exception e) {
}
}
public void mySearch() {
final JDialog findDialog = new JDialog(this, "查找与替换", true);
Container con = findDialog.getContentPane();
con.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel searchContentLabel = new JLabel("查找内容(N) :");
JLabel replaceContentLabel = new JLabel("替换为(P) :");
final JTextField findText = new JTextField(22);
final JTextField replaceText = new JTextField(22);
final JCheckBox matchcase = new JCheckBox("区分大小写");
ButtonGroup bGroup = new ButtonGroup();
final JRadioButton up = new JRadioButton("向上(U)");
final JRadioButton down = new JRadioButton("向下(D)");
down.setSelected(true);
bGroup.add(up);
bGroup.add(down);
JButton searchNext = new JButton("查找下一个(F)");
JButton replace = new JButton("替换(R)");
final JButton replaceAll = new JButton("全部替换(A)");
searchNext.setPreferredSize(new Dimension(110, 22));
replace.setPreferredSize(new Dimension(110, 22));
replaceAll.setPreferredSize(new Dimension(110, 22));
// "替换"按钮的事件处理
replace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (replaceText.getText().length() == 0 && Text.getSelectedText() != null)
Text.replaceSelection("");
if (replaceText.getText().length() > 0 && Text.getSelectedText() != null)
Text.replaceSelection(replaceText.getText());
}
});
// "替换全部"按钮的事件处理
replaceAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Text.setCaretPosition(0); // 将光标放到编辑区开头
int a = 0, b = 0, replaceCount = 0;
if (findText.getText().length() == 0) {
JOptionPane.showMessageDialog(findDialog, "请填写查找内容!", "提示", JOptionPane.WARNING_MESSAGE);
findText.requestFocus(true);
return;
}
while (a > -1) {
int FindStartPos = Text.getCaretPosition();
String str1, str2, str3, str4, strA, strB;
str1 = Text.getText();
str2 = str1.toLowerCase();
str3 = findText.getText();
str4 = str3.toLowerCase();
if (matchcase.isSelected()) {
strA = str1;
strB = str3;
} else {
strA = str2;
strB = str4;
}
if (up.isSelected()) {
if (Text.getSelectedText() == null) {
a = strA.lastIndexOf(strB, FindStartPos - 1);
} else {
a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1);
}
} else if (down.isSelected()) {
if (Text.getSelectedText() == null) {
a = strA.indexOf(strB, FindStartPos);
} else {
a = strA.indexOf(strB, FindStartPos - findText.getText().length() + 1);
}
}
if (a > -1) {
if (up.isSelected()) {
Text.setCaretPosition(a);
b = findText.getText().length();
Text.select(a, a + b);
} else if (down.isSelected()) {
Text.setCaretPosition(a);
b = findText.getText().length();
Text.select(a, a + b);
}
} else {
if (replaceCount == 0) {
JOptionPane.showMessageDialog(findDialog, "找不到您查找的内容!", "记事本", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(findDialog, "成功替换" + replaceCount + "个匹配内容!", "替换成功", JOptionPane.INFORMATION_MESSAGE);
}
}
if (replaceText.getText().length() == 0 && Text.getSelectedText() != null) {
Text.replaceSelection("");
replaceCount++;
}
if (replaceText.getText().length() > 0 && Text.getSelectedText() != null) {
Text.replaceSelection(replaceText.getText());
replaceCount++;
}
}// end while
}
}); /* "替换全部"按钮的事件处理结束 */
// "查找下一个"按钮事件处理
searchNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int a = 0, b = 0;
int FindStartPos = Text.getCaretPosition();
String str1, str2, str3, str4, strA, strB;
str1 = Text.getText();
str2 = str1.toLowerCase();
str3 = findText.getText();
str4 = str3.toLowerCase();
// "区分大小写"的CheckBox被选中
if (matchcase.isSelected()) {
strA = str1;
strB = str3;
} else {
strA = str2;
strB = str4;
}
if (up.isSelected()) {
if (Text.getSelectedText() == null) {
a = strA.lastIndexOf(strB, FindStartPos - 1);
} else {
a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1);
}
} else if (down.isSelected()) {
if (Text.getSelectedText() == null) {
a = strA.indexOf(strB, FindStartPos);
} else {
a = strA.indexOf(strB, FindStartPos - findText.getText().length() + 1);
}
}
if (a > -1) {
if (up.isSelected()) {
Text.setCaretPosition(a);
b = findText.getText().length();
Text.select(a, a + b);
} else if (down.isSelected()) {
Text.setCaretPosition(a);
b = findText.getText().length();
Text.select(a, a + b);
}
} else {
JOptionPane.showMessageDialog(null, "找不到您查找的内容!", "记事本", JOptionPane.INFORMATION_MESSAGE);
}
}
});/* "查找下一个"按钮事件处理结束 */
// "取消"按钮及事件处理
JButton cancel = new JButton("取消");
cancel.setPreferredSize(new Dimension(110, 22));
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
findDialog.dispose();
}
});
// 创建"查找与替换"对话框的界面
JPanel bottomPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel topPanel = new JPanel();
JPanel direction = new JPanel();
direction.setBorder(BorderFactory.createTitledBorder("方向 "));
direction.add(up);
direction.add(down);
direction.setPreferredSize(new Dimension(170, 60));
JPanel replacePanel = new JPanel();
replacePanel.setLayout(new GridLayout(2, 1));
replacePanel.add(replace);
replacePanel.add(replaceAll);
topPanel.add(searchContentLabel);
topPanel.add(findText);
topPanel.add(searchNext);
centerPanel.add(replaceContentLabel);
centerPanel.add(replaceText);
centerPanel.add(replacePanel);
bottomPanel.add(matchcase);
bottomPanel.add(direction);
bottomPanel.add(cancel);
con.add(topPanel);
con.add(centerPanel);
con.add(bottomPanel);
// 设置"查找与替换"对话框的大小、可更改大小(否)、位置和可见性
findDialog.setSize(410, 210);
findDialog.setResizable(false);
findDialog.setLocation(230, 280);
findDialog.setVisible(true);
}
/* 方法mySearch()结束 */
// 实现ActionListener的事件处理方法public void actionPerformed(ActionEvent e)
public void actionPerformed(ActionEvent e) {
// 新建
if (e.getActionCommand().equals("新建(N)") || e.getSource() == newButton)
// 最初发生 Event 的对象
// if(e.getSource()==mFile_New||e.getSource()==newButton)
{
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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -