📄 menulogic.java
字号:
package notepad.logic;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import notepad.firstGUI.MyMenuBar;
import notepad.listener.SetJFrameListener;
import notepad.popGUI.About;
import notepad.popGUI.Finder;
import notepad.popGUI.FontDialog;
import notepad.popGUI.HelpText;
import notepad.popGUI.Replace;
public class MenuLogic implements ActionListener, CaretListener
{
/** 用于读、写文件的相关对象 */
File file = null;
BufferedReader in = null;
BufferedWriter out = null;
FileReader file_reader = null;
FileWriter file_writer = null;
JFileChooser fileChooser = null;
/** 表示文本区上的字符是否改变够,默认为是 */
boolean isTextStringChange = false;
/** 表示文件是否真的推出,默认为是 */
boolean isFileExit = true;
/** 用于设置字体的 */
private FontDialog fontDialog;
/** 主JFrame,逻辑控制的东西,都是在此反应出来 */
private JFrame parent;
/** 接口对象 */
private SetJFrameListener listener;
/**和JFrame上的是同一个引用,对这操作,JFrame的会同样变化*/
private JTextArea textArea;
/**和JFrame上的是同一个引用*/
private JTextField southField;
/**用来调用它的方法,判断菜单项选中状态的*/
private MyMenuBar menubar;
/**
* 构造方法
* @param parent
* @param textArea
* @param textField
* @param menubar
*/
public MenuLogic(JFrame parent, JTextArea textArea,
JTextField textField, MyMenuBar menubar)
{
this.parent = parent;
this.textArea = textArea;
this.southField = textField;
this.menubar = menubar;
file = new File("无标题.txt");
parent.setTitle(file.getName() + " - 记事本");
southField.setText("Java版记事本 作者:黎明你好");
fontDialog = new FontDialog(parent);// 设置字体的对话框
fileChooser = new JFileChooser();
fileChooser.addChoosableFileFilter(new MyFileFilter("txt"));// 添加文件筛选
}
/**
* 监听器方法,用来检测文件是否有改变过的方法
*/
public void caretUpdate(CaretEvent e)
{
if (file != null)
{
parent.setTitle("*" + file.getName() + " - 记事本");// 如果文本区内容已经改变,在标题上显示一个*号
isTextStringChange = true;
}
}
/**
* 菜单项监控方法
*/
public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand();
// 文件菜单中,菜单项的动作实现 ,包括新建,打开,保存,另存为,退出
if (action.equals("新建(N)"))// 新建文件
{
newFile();
}
else if (action.equals("打开(O)...")) // 打开文件
{
openFile();
}
else if (action.equals("保存(S)"))// 保存文件
{
saveFile();
}
else if (action.equals("另存为(A)...")) // 另存为文件
{
saveAsFile();
}
else if (action.equals("退出(X)")) // 退出菜单项
{
exit();
}
// 编辑菜单中,菜单项的动作实现 ,包括剪切,复制,粘贴,删除,查询,查询下一个,全选,插入时间
else if (action.equals("剪切(T)"))// 剪切
{
textArea.cut();
}
else if (action.equals("复制(C)"))// 复制
{
textArea.copy();
}
else if (action.equals("粘贴(P)"))// 粘贴
{
textArea.paste();
}
else if (action.equals("删除(L)"))// 删除
{
delete();
}
else if (action.equals("查找(F)") || action.equals("查找下一个(N)"))// 查询
{
find();
}
else if (action.equals("替换(R)"))// 替换
{
replace();
}
else if (action.equals("全选(A)"))// 全选
{
textArea.selectAll();
}
else if (action.equals("时间/日期(D)"))// 插入时间
{
// String str = (new Date()).toString();
String str = getNowTime();
int a = textArea.getCaretPosition();// 光标位置
textArea.insert(str, a);
}
// 格式菜单中,菜单项的动作实现 ,包括自动换行,单词为界,字体
else if (action.equals("自动换行(W)")) // 设置文本区是否自动换行
{
if (menubar.item_setLineWrap_isSelected())
textArea.setLineWrap(true);
else
textArea.setLineWrap(false);
}
else if (action.equals("单词为界(S)"))// 换行时候,是否让整个单词在一行
{
if (menubar.item_setWrapStyleWord_isSelected())
textArea.setWrapStyleWord(true);
else
textArea.setWrapStyleWord(false);
}
else if (action.equals("字体(F)..."))// 设置字体
{
fontDialog.setVisible(true);
if (fontDialog.isFontChange())// 如果改变了
{
textArea.setFont(fontDialog.getFont());
textArea.setForeground(fontDialog.getFontColor());
readFileToText(file);
}
}
// 查看菜单中,菜单项的动作实现 ,包括工具箱,窗口风格的设置
else if (action.equals("工具箱(T)"))
{
listener.setToolBarVisible(menubar.item_tool_isSelected());
}
else if (action.equals("Windows"))
{
listener.changeLookAndFeel("Windows");
}
else if (action.equals("Mac"))
{
listener.changeLookAndFeel("Mac");
}
else if (action.equals("Java"))
{
listener.changeLookAndFeel("Java");
}
// 帮助菜单中,菜单项的动作实现 ,包括帮助主题,关于记事本本
else if (action.equals("帮助主题(H)"))
{
About about = new About(parent);
about.setVisible(true);
}
else if (action.equals("关于记事本(A)"))
{
HelpText help = new HelpText(parent);
help.setVisible(true);
}
parent.setTitle(file.getName() + " - 记事本");
southField.setText(file.getAbsolutePath());
}
/**
* 新建文件的方法
*/
public void newFile()
{
if (isTextStringChange)
{
closeFile();
}
if (isFileExit)
{
File newFile = new File("无标题.txt");
file = newFile;
textArea.setText("");
isTextStringChange = false;
}
}
/**
* 用来打开文件的方法
*/
public void openFile()
{
if (isTextStringChange == true)// 打开文件之前,如果文本全有变动,就要进行关闭当前文件的操作
closeFile();
if (isFileExit == false)// 判断时候真的关闭当前文件,如果点了取消,则方法结束
return;
fileChooser.setDialogTitle("打开");
int result = fileChooser.showOpenDialog(parent);
if (result == JFileChooser.APPROVE_OPTION)// 如果点的确定按钮
{
file = fileChooser.getSelectedFile();
if (!file.exists())// 如果文件不存在
{
java.awt.Toolkit.getDefaultToolkit().beep();// 发出一个音频嘟嘟声。
JOptionPane.showMessageDialog(null, "" + file.getName()
+ "\n找不到文件" + "\n请检查所给的文件名是否正确", "打开",
JOptionPane.WARNING_MESSAGE);// 淡出提示对话框,找不到
}
else
// 如果文件存在
{
readFileToText(file);
isTextStringChange = false;
}
}
}
/**
* 另存问文件方法。当文件为空,或者点击另存为的时候,弹出文件对话框,进行保存文件处理。
*/
public void saveAsFile()
{
fileChooser.setDialogTitle("另存为");
int result = fileChooser.showSaveDialog(parent);
if (result == JFileChooser.APPROVE_OPTION)
{
file = fileChooser.getSelectedFile();
}
if (file != null)
{
saveTextToFile(file);
isTextStringChange = false;
}
}
/**
* 保存文件时的处理方法
*/
public void saveFile()
{
isTextStringChange = false;
if (file.getName().equals("无标题.txt"))// 判断文件是否是初始的名字,
saveAsFile();
else
{
saveTextToFile(file);// 否者直接保存文本区上的文字到文件里
}
}
/**
* 用来把制定文件读取到文本区的方法
*
* @param readFile
* 需要读取的文件对象
*/
public void readFileToText(File readFile)
{
textArea.setText(null);
parent.setTitle("" + readFile.getName());
try
{
file_reader = new FileReader(file);
in = new BufferedReader(file_reader);
String ss = new String();
while ((ss = in.readLine()) != null)
textArea.append(ss + '\n');
in.close();
textArea.setCaretPosition(0);// 把光标移动文件头
}
catch (Exception e)
{
southField.setText("文件没有找到或者发生IO错误");
System.out.println("" + e.toString());
}
}
/**
* 只用来把文本区上的文字保存到制定文件上
*
* @param saveFile
* 保存到的文件对象
*/
public void saveTextToFile(File saveFile)
{
try
{
file_writer = new FileWriter(saveFile);
out = new BufferedWriter(file_writer);
out.write(textArea.getText(), 0, (textArea.getText().length()));
out.flush();
}
catch (Exception e)
{
southField.setText("文件没有找到或者发生IO错误");
System.out.println("" + e.toString());
}
}
/**
* 需要关闭当前文件时,调用。
*/
public void closeFile()
{
if (isTextStringChange == true)// 如果文本区改变过
{
if (file.getName().equals("无标题.txt"))// 如果文件名是初始名,则需要询问,保存时用文件对话框
{
int n = JOptionPane.showConfirmDialog(parent, file.getName()
+ "的文字已经改变。\n 想保存么?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (n == JOptionPane.YES_OPTION)// 如果点的"是",弹出保存文件对话框,保存后关闭当前文件
{
isFileExit = true;
saveAsFile();
}
if (n == JOptionPane.NO_OPTION)// 如果点的"否",
{
isFileExit = true;
}
if (n == JOptionPane.CANCEL_OPTION)// 如果点取消,方法结束
{
isFileExit = false;
}
}
else
// 如果有文件名,则直接询问是否保存,保存时是直接保存到当前文件
{
int n = JOptionPane.showConfirmDialog(parent, file.getName()
+ "的文字已经改变。\n 想保存么?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (n == JOptionPane.YES_OPTION)// 如果点的"是",弹出保存文件对话框,保存后关闭当前文件
{
isFileExit = true;
saveFile();
}
if (n == JOptionPane.NO_OPTION)// 如果点的"否",
{
isFileExit = true;
}
if (n == JOptionPane.CANCEL_OPTION)// 如果点取消,方法结束
{
isFileExit = false;
}
}
}
}
/**
* 获得到系统当前时间的方法
*
* @return 当前时间字符串
*/
public String getNowTime()//
{
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
int year = calendar.get(Calendar.YEAR);// 年
int month = calendar.get(Calendar.MONTH) + 1;// 月
int day = calendar.get(Calendar.DAY_OF_MONTH);// 日
int hour = calendar.get(Calendar.HOUR_OF_DAY);// 时
int minute = calendar.get(Calendar.MINUTE);// 分
return (hour + ":" + minute + " " + year + "-" + month + "-" + day);
}
/**查找的方法*/
public void find()
{
Finder finder = new Finder(parent, textArea);
finder.setVisible(true);
}
/**替换的方法*/
public void replace()
{
Replace replace = new Replace(parent, textArea);
replace.setVisible(true);
}
/**删除当前选的字符串的方法*/
public void delete()
{
int start = textArea.getSelectionStart();
int end = textArea.getSelectionEnd();
textArea.replaceRange("", start, end);
}
/**
* 记事本需要退出时调用。
*/
public void exit()
{
if (isTextStringChange)// 如果文本区改变够,要进行关闭当前文件的操作
{
closeFile();
}
if (isFileExit)// 如果真的要关闭文件
{
System.exit(0);
}
}
/**添加监听器的方法*/
public void addSetJFrameListener(SetJFrameListener listener)
{
this.listener = listener;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -