From 00dee6d85578ac3c7864aec94a9b625758c4c61b Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Sun, 15 May 2022 23:30:39 +0800 Subject: [PATCH 01/25] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=90=84=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E5=8F=8A=E5=85=B6=E8=8F=9C=E5=8D=95=E9=A1=B9=EF=BC=8C?= =?UTF-8?q?=E4=B8=BA=E5=85=B6=E8=AE=BE=E7=BD=AE=E5=BF=AB=E6=8D=B7=E9=94=AE?= =?UTF-8?q?=E5=B9=B6=E6=B3=A8=E5=86=8C=E4=BA=8B=E4=BB=B6=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Test.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 24deb29..72f6647 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -1,7 +1,10 @@ package java2022spring; +import javax.swing.*; + public class Test { - public static void main(String[] args) { - System.out.println("Hello world!"); - } + public static void main(String args[]) + { Notepad notepad=new Notepad(); + notepad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//使用 System exit 方法退出应用程序 + } } -- Gitee From 58a8373af37898108358b7dbe6138cbb6428f2c1 Mon Sep 17 00:00:00 2001 From: cheng <1196922704@qq.com> Date: Mon, 16 May 2022 13:24:59 +0000 Subject: [PATCH 02/25] Notepad.java --- src/java2022spring/Notepad.java | 109 ++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/java2022spring/Notepad.java diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java new file mode 100644 index 0000000..8a0e74d --- /dev/null +++ b/src/java2022spring/Notepad.java @@ -0,0 +1,109 @@ +package java2022spring; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.io.*; + +public class Notepad extends JFrame implements ActionListener{ + //菜单条 + JMenuBar menuBar; + //菜单 + JMenu fileMenu,editMenu,formatMenu,viewMenu,helpMenu; + //“文件”的菜单项新建、打开、保存、另存为和退出功能 + JMenuItem fileM_New,fileM_Open,fileM_Save,fileM_SaveAs,fileM_Exit; + //“编辑”的菜单项撤消、剪切、复制、粘贴和删除功能 + JMenuItem editM_Cancel,editM_Cut,editM_Copy,editM_Paste,editM_Delete; + //“格式”的菜单项自动换行功能 + JMenuItem formatM_LineFeed; + //“查看”的菜单项状态栏功能 + JMenuItem viewM_Status; + //“帮助”的菜单项查看帮助和关于记事本功能 + JMenuItem helpM_CheckHelp,helpM_AboutNotepad; + //“文本”编辑区 + JTextArea text; + //状态栏标签 + JLabel statusLabel; + public Notepad() { + setTitle("记事本"); + //创建菜单条 + menuBar=new JMenuBar(); + //创建文件菜单及菜单项并注册事件监听 + fileMenu=new JMenu("文件(F)"); + fileMenu.setMnemonic('F');//设置快捷键ALT+F + + fileM_New=new JMenuItem("新建(N)"); + fileM_New.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));//设置快捷键CTRL+N + fileM_New.addActionListener(this); + + fileM_Open=new JMenuItem("打开(O)..."); + fileM_Open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));//设置快捷键CTRL+O + fileM_Open.addActionListener(this); + + fileM_Save=new JMenuItem("保存(S)"); + fileM_Save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));//设置快捷键CTRL+S + fileM_Save.addActionListener(this); + + fileM_SaveAs=new JMenuItem("另存为(A)"); + fileM_SaveAs.addActionListener(this); + + fileM_Exit=new JMenuItem("退出(X)"); + fileM_Exit.addActionListener(this); + + //创建编辑菜单及菜单项并注册事件监听 + editMenu=new JMenu("编辑(E)"); + editMenu.setMnemonic('E');//设置快捷键ALT+E + + editM_Cancel=new JMenuItem("撤销(U)"); + editM_Cancel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,ActionEvent.CTRL_MASK));//设置快捷键CTRL+U + editM_Cancel.addActionListener(this); + + editM_Cut=new JMenuItem("剪切(T)"); + editM_Cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));//设置快捷键CTRL+X + editM_Cut.addActionListener(this); + + editM_Copy=new JMenuItem("复制(C)"); + editM_Copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK));//设置快捷键CTRL+C + editM_Copy.addActionListener(this); + + editM_Paste=new JMenuItem("粘贴(P)"); + editM_Paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK));//设置快捷键CTRL+V + editM_Paste.addActionListener(this); + + editM_Delete=new JMenuItem("删除(D)"); + editM_Delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));//设置快捷键DELETE + editM_Delete.addActionListener(this); + + //创建格式菜单及菜单项并注册事件监听 + formatMenu=new JMenu("格式(O)"); + formatMenu.setMnemonic('O');//设置快捷键ALT+O + + formatM_LineFeed=new JMenuItem("自动换行(W)"); + formatM_LineFeed.setMnemonic('W');//设置快捷键ALT+W + formatM_LineFeed.addActionListener(this); + + //创建查看菜单及菜单项并注册事件监听 + viewMenu=new JMenu("查看(V)"); + viewMenu.setMnemonic('V');//设置快捷键ALT+V + + viewM_Status=new JMenuItem("状态栏(S)"); + viewM_Status.setMnemonic('S');//设置快捷键ALT+S + viewM_Status.addActionListener(this); + + //创建帮助菜单及菜单项并注册事件监听 + helpMenu=new JMenu("帮助(H)"); + helpMenu.setMnemonic('H');//设置快捷键ALT+H + + helpM_CheckHelp=new JMenuItem("查看帮助(H)"); + helpM_CheckHelp.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0)); //设置快捷键F1 + helpM_CheckHelp.addActionListener(this); + + helpM_AboutNotepad=new JMenuItem("关于记事本(A)"); + helpM_AboutNotepad.addActionListener(this); + } + @Override + public void actionPerformed(ActionEvent e) { + // TODO 自动生成的方法存根 + + } +} \ No newline at end of file -- Gitee From 34ac609921d7de7f586f71d434837850c65f2a8d Mon Sep 17 00:00:00 2001 From: cheng <1196922704@qq.com> Date: Mon, 16 May 2022 13:26:28 +0000 Subject: [PATCH 03/25] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20sr?= =?UTF-8?q?c/java2022spring/Notepad.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 109 -------------------------------- 1 file changed, 109 deletions(-) delete mode 100644 src/java2022spring/Notepad.java diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java deleted file mode 100644 index 8a0e74d..0000000 --- a/src/java2022spring/Notepad.java +++ /dev/null @@ -1,109 +0,0 @@ -package java2022spring; - -import javax.swing.*; -import java.awt.*; -import java.awt.event.*; -import java.io.*; - -public class Notepad extends JFrame implements ActionListener{ - //菜单条 - JMenuBar menuBar; - //菜单 - JMenu fileMenu,editMenu,formatMenu,viewMenu,helpMenu; - //“文件”的菜单项新建、打开、保存、另存为和退出功能 - JMenuItem fileM_New,fileM_Open,fileM_Save,fileM_SaveAs,fileM_Exit; - //“编辑”的菜单项撤消、剪切、复制、粘贴和删除功能 - JMenuItem editM_Cancel,editM_Cut,editM_Copy,editM_Paste,editM_Delete; - //“格式”的菜单项自动换行功能 - JMenuItem formatM_LineFeed; - //“查看”的菜单项状态栏功能 - JMenuItem viewM_Status; - //“帮助”的菜单项查看帮助和关于记事本功能 - JMenuItem helpM_CheckHelp,helpM_AboutNotepad; - //“文本”编辑区 - JTextArea text; - //状态栏标签 - JLabel statusLabel; - public Notepad() { - setTitle("记事本"); - //创建菜单条 - menuBar=new JMenuBar(); - //创建文件菜单及菜单项并注册事件监听 - fileMenu=new JMenu("文件(F)"); - fileMenu.setMnemonic('F');//设置快捷键ALT+F - - fileM_New=new JMenuItem("新建(N)"); - fileM_New.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));//设置快捷键CTRL+N - fileM_New.addActionListener(this); - - fileM_Open=new JMenuItem("打开(O)..."); - fileM_Open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));//设置快捷键CTRL+O - fileM_Open.addActionListener(this); - - fileM_Save=new JMenuItem("保存(S)"); - fileM_Save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));//设置快捷键CTRL+S - fileM_Save.addActionListener(this); - - fileM_SaveAs=new JMenuItem("另存为(A)"); - fileM_SaveAs.addActionListener(this); - - fileM_Exit=new JMenuItem("退出(X)"); - fileM_Exit.addActionListener(this); - - //创建编辑菜单及菜单项并注册事件监听 - editMenu=new JMenu("编辑(E)"); - editMenu.setMnemonic('E');//设置快捷键ALT+E - - editM_Cancel=new JMenuItem("撤销(U)"); - editM_Cancel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,ActionEvent.CTRL_MASK));//设置快捷键CTRL+U - editM_Cancel.addActionListener(this); - - editM_Cut=new JMenuItem("剪切(T)"); - editM_Cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));//设置快捷键CTRL+X - editM_Cut.addActionListener(this); - - editM_Copy=new JMenuItem("复制(C)"); - editM_Copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK));//设置快捷键CTRL+C - editM_Copy.addActionListener(this); - - editM_Paste=new JMenuItem("粘贴(P)"); - editM_Paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK));//设置快捷键CTRL+V - editM_Paste.addActionListener(this); - - editM_Delete=new JMenuItem("删除(D)"); - editM_Delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));//设置快捷键DELETE - editM_Delete.addActionListener(this); - - //创建格式菜单及菜单项并注册事件监听 - formatMenu=new JMenu("格式(O)"); - formatMenu.setMnemonic('O');//设置快捷键ALT+O - - formatM_LineFeed=new JMenuItem("自动换行(W)"); - formatM_LineFeed.setMnemonic('W');//设置快捷键ALT+W - formatM_LineFeed.addActionListener(this); - - //创建查看菜单及菜单项并注册事件监听 - viewMenu=new JMenu("查看(V)"); - viewMenu.setMnemonic('V');//设置快捷键ALT+V - - viewM_Status=new JMenuItem("状态栏(S)"); - viewM_Status.setMnemonic('S');//设置快捷键ALT+S - viewM_Status.addActionListener(this); - - //创建帮助菜单及菜单项并注册事件监听 - helpMenu=new JMenu("帮助(H)"); - helpMenu.setMnemonic('H');//设置快捷键ALT+H - - helpM_CheckHelp=new JMenuItem("查看帮助(H)"); - helpM_CheckHelp.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0)); //设置快捷键F1 - helpM_CheckHelp.addActionListener(this); - - helpM_AboutNotepad=new JMenuItem("关于记事本(A)"); - helpM_AboutNotepad.addActionListener(this); - } - @Override - public void actionPerformed(ActionEvent e) { - // TODO 自动生成的方法存根 - - } -} \ No newline at end of file -- Gitee From dd9c02913078aa72e6f5d679b9460f162525b69b Mon Sep 17 00:00:00 2001 From: cheng <1196922704@qq.com> Date: Mon, 16 May 2022 13:31:50 +0000 Subject: [PATCH 04/25] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=90=84=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E5=8F=8A=E5=85=B6=E8=8F=9C=E5=8D=95=E9=A1=B9=EF=BC=8C?= =?UTF-8?q?=E4=B8=BA=E5=85=B6=E8=AE=BE=E7=BD=AE=E5=BF=AB=E6=8D=B7=E9=94=AE?= =?UTF-8?q?=E5=B9=B6=E6=B3=A8=E5=86=8C=E4=BA=8B=E4=BB=B6=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 109 ++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/java2022spring/Notepad.java diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java new file mode 100644 index 0000000..8a0e74d --- /dev/null +++ b/src/java2022spring/Notepad.java @@ -0,0 +1,109 @@ +package java2022spring; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.io.*; + +public class Notepad extends JFrame implements ActionListener{ + //菜单条 + JMenuBar menuBar; + //菜单 + JMenu fileMenu,editMenu,formatMenu,viewMenu,helpMenu; + //“文件”的菜单项新建、打开、保存、另存为和退出功能 + JMenuItem fileM_New,fileM_Open,fileM_Save,fileM_SaveAs,fileM_Exit; + //“编辑”的菜单项撤消、剪切、复制、粘贴和删除功能 + JMenuItem editM_Cancel,editM_Cut,editM_Copy,editM_Paste,editM_Delete; + //“格式”的菜单项自动换行功能 + JMenuItem formatM_LineFeed; + //“查看”的菜单项状态栏功能 + JMenuItem viewM_Status; + //“帮助”的菜单项查看帮助和关于记事本功能 + JMenuItem helpM_CheckHelp,helpM_AboutNotepad; + //“文本”编辑区 + JTextArea text; + //状态栏标签 + JLabel statusLabel; + public Notepad() { + setTitle("记事本"); + //创建菜单条 + menuBar=new JMenuBar(); + //创建文件菜单及菜单项并注册事件监听 + fileMenu=new JMenu("文件(F)"); + fileMenu.setMnemonic('F');//设置快捷键ALT+F + + fileM_New=new JMenuItem("新建(N)"); + fileM_New.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));//设置快捷键CTRL+N + fileM_New.addActionListener(this); + + fileM_Open=new JMenuItem("打开(O)..."); + fileM_Open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));//设置快捷键CTRL+O + fileM_Open.addActionListener(this); + + fileM_Save=new JMenuItem("保存(S)"); + fileM_Save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));//设置快捷键CTRL+S + fileM_Save.addActionListener(this); + + fileM_SaveAs=new JMenuItem("另存为(A)"); + fileM_SaveAs.addActionListener(this); + + fileM_Exit=new JMenuItem("退出(X)"); + fileM_Exit.addActionListener(this); + + //创建编辑菜单及菜单项并注册事件监听 + editMenu=new JMenu("编辑(E)"); + editMenu.setMnemonic('E');//设置快捷键ALT+E + + editM_Cancel=new JMenuItem("撤销(U)"); + editM_Cancel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,ActionEvent.CTRL_MASK));//设置快捷键CTRL+U + editM_Cancel.addActionListener(this); + + editM_Cut=new JMenuItem("剪切(T)"); + editM_Cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));//设置快捷键CTRL+X + editM_Cut.addActionListener(this); + + editM_Copy=new JMenuItem("复制(C)"); + editM_Copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK));//设置快捷键CTRL+C + editM_Copy.addActionListener(this); + + editM_Paste=new JMenuItem("粘贴(P)"); + editM_Paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK));//设置快捷键CTRL+V + editM_Paste.addActionListener(this); + + editM_Delete=new JMenuItem("删除(D)"); + editM_Delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));//设置快捷键DELETE + editM_Delete.addActionListener(this); + + //创建格式菜单及菜单项并注册事件监听 + formatMenu=new JMenu("格式(O)"); + formatMenu.setMnemonic('O');//设置快捷键ALT+O + + formatM_LineFeed=new JMenuItem("自动换行(W)"); + formatM_LineFeed.setMnemonic('W');//设置快捷键ALT+W + formatM_LineFeed.addActionListener(this); + + //创建查看菜单及菜单项并注册事件监听 + viewMenu=new JMenu("查看(V)"); + viewMenu.setMnemonic('V');//设置快捷键ALT+V + + viewM_Status=new JMenuItem("状态栏(S)"); + viewM_Status.setMnemonic('S');//设置快捷键ALT+S + viewM_Status.addActionListener(this); + + //创建帮助菜单及菜单项并注册事件监听 + helpMenu=new JMenu("帮助(H)"); + helpMenu.setMnemonic('H');//设置快捷键ALT+H + + helpM_CheckHelp=new JMenuItem("查看帮助(H)"); + helpM_CheckHelp.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0)); //设置快捷键F1 + helpM_CheckHelp.addActionListener(this); + + helpM_AboutNotepad=new JMenuItem("关于记事本(A)"); + helpM_AboutNotepad.addActionListener(this); + } + @Override + public void actionPerformed(ActionEvent e) { + // TODO 自动生成的方法存根 + + } +} \ No newline at end of file -- Gitee From 5caf5be4992729e8fdff4cfd472c7581f1edcabd Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Mon, 16 May 2022 23:40:14 +0800 Subject: [PATCH 05/25] =?UTF-8?q?=E5=90=91=E8=8F=9C=E5=8D=95=E6=9D=A1?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8F=9C=E5=8D=95=E5=8F=8A=E5=85=B6=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E9=A1=B9=E5=B9=B6=E5=90=91=E7=AA=97=E5=8F=A3=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=8F=9C=E5=8D=95=E6=9D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 8a0e74d..8906567 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -100,6 +100,41 @@ public class Notepad extends JFrame implements ActionListener{ helpM_AboutNotepad=new JMenuItem("关于记事本(A)"); helpM_AboutNotepad.addActionListener(this); + + //向菜单条添加“文件”菜单及菜单项 + menuBar.add(fileMenu); + fileMenu.add(fileM_New); + fileMenu.add(fileM_Open); + fileMenu.add(fileM_Save); + fileMenu.add(fileM_SaveAs); + fileMenu.addSeparator(); + fileMenu.add(fileM_Exit); + + //向菜单条添加“编辑”菜单及菜单项 + menuBar.add(editMenu); + editMenu.add(editM_Cancel); + editMenu.addSeparator(); + editMenu.add(editM_Cut); + editMenu.add(editM_Copy); + editMenu.add(editM_Paste); + editMenu.add(editM_Delete); + + //向菜单条添加“格式”菜单及菜单项 + menuBar.add(formatMenu); + formatMenu.add(formatM_LineFeed); + + //向菜单条添加“查看”菜单及菜单项 + menuBar.add(viewMenu); + viewMenu.add(viewM_Status); + + //向菜单条添加“帮助”菜单及菜单项 + menuBar.add(helpMenu); + helpMenu.add(helpM_CheckHelp); + helpMenu.addSeparator(); + helpMenu.add(helpM_AboutNotepad); + + //向窗口添加菜单条 + this.setJMenuBar(menuBar); } @Override public void actionPerformed(ActionEvent e) { -- Gitee From 4420857ab094cb7410990fb35a50a35ae6637823 Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Tue, 17 May 2022 23:57:45 +0800 Subject: [PATCH 06/25] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E5=8C=BA=E5=9F=9F=E5=B9=B6=E6=B7=BB=E5=8A=A0=E6=BB=9A=E5=8A=A8?= =?UTF-8?q?=E6=9D=A1=EF=BC=8C=E8=AE=BE=E7=BD=AE=E7=AA=97=E5=8F=A3=E5=9C=A8?= =?UTF-8?q?=E5=B1=8F=E5=B9=95=E4=B8=8A=E7=9A=84=E4=BD=8D=E7=BD=AE=E3=80=81?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F=E5=92=8C=E5=8F=AF=E8=A7=81=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 16 ++++++++++++++-- src/java2022spring/Test.java | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 8906567..4b6ccb1 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -135,10 +135,22 @@ public class Notepad extends JFrame implements ActionListener{ //向窗口添加菜单条 this.setJMenuBar(menuBar); - } + + //创建文本区域并添加滚动条 + text=new JTextArea(20,50); + JScrollPane scroller=new JScrollPane(text,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); + this.add(scroller,BorderLayout.CENTER); + + + //设置窗口在屏幕上的位置、大小和可见性 + this.setLocation(200,200); + this.setSize(700,700); + this.setVisible(true); + } + @Override public void actionPerformed(ActionEvent e) { - // TODO 自动生成的方法存根 + } } \ No newline at end of file diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 72f6647..74336d9 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -5,6 +5,6 @@ import javax.swing.*; public class Test { public static void main(String args[]) { Notepad notepad=new Notepad(); - notepad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//使用 System exit 方法退出应用程序 + notepad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } -- Gitee From f5066550f1a0505928b291a0836c09a8b6761300 Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Wed, 18 May 2022 23:31:49 +0800 Subject: [PATCH 07/25] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=8F=B3=E9=94=AE?= =?UTF-8?q?=E5=BC=B9=E5=87=BA=E8=8F=9C=E5=8D=95=E5=8F=8A=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E9=A1=B9=EF=BC=8C=E6=B3=A8=E5=86=8C=E5=8F=B3=E9=94=AE=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E4=BA=8B=E4=BB=B6=E5=B9=B6=E5=90=91=E5=8F=B3=E9=94=AE?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=E6=B7=BB=E5=8A=A0=E8=8F=9C=E5=8D=95=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 34 ++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 4b6ccb1..76ad88a 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -24,6 +24,10 @@ public class Notepad extends JFrame implements ActionListener{ JTextArea text; //状态栏标签 JLabel statusLabel; + //右键弹出菜单项 + JPopupMenu popupMenu; + JMenuItem popupM_Cancel,popupM_Cut,popupM_Copy,popupM_Paste,popupM_Delete; + public Notepad() { setTitle("记事本"); //创建菜单条 @@ -133,6 +137,34 @@ public class Notepad extends JFrame implements ActionListener{ helpMenu.addSeparator(); helpMenu.add(helpM_AboutNotepad); + //创建右键弹出菜单并注册右键菜单事件 + popupMenu=new JPopupMenu(); + + popupM_Cancel=new JMenuItem("撤销(U)"); + popupM_Cancel.addActionListener(this); + + popupM_Cut=new JMenuItem("剪切(T)"); + popupM_Cut.addActionListener(this); + + popupM_Copy=new JMenuItem("复制(C)"); + popupM_Copy.addActionListener(this); + + popupM_Paste=new JMenuItem("粘帖(P)"); + popupM_Paste.addActionListener(this); + + popupM_Delete=new JMenuItem("删除(D)"); + popupM_Delete.addActionListener(this); + + //向右键菜单添加菜单项和分隔符 + popupMenu.add(popupM_Cancel); + popupMenu.addSeparator(); + popupMenu.add(popupM_Cut); + popupMenu.add(popupM_Copy); + popupMenu.add(popupM_Paste); + popupMenu.add(popupM_Delete); + + // + //向窗口添加菜单条 this.setJMenuBar(menuBar); @@ -140,7 +172,7 @@ public class Notepad extends JFrame implements ActionListener{ text=new JTextArea(20,50); JScrollPane scroller=new JScrollPane(text,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); this.add(scroller,BorderLayout.CENTER); - + text.setLineWrap(true);//设置自动换行 //设置窗口在屏幕上的位置、大小和可见性 this.setLocation(200,200); -- Gitee From 39bce4ba82bf95e909ae15b6aa5a72eac900a08c Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Thu, 19 May 2022 23:32:34 +0800 Subject: [PATCH 08/25] =?UTF-8?q?=E5=90=91=E6=96=87=E6=9C=AC=E5=8C=BA?= =?UTF-8?q?=E5=9F=9F=E6=B7=BB=E5=8A=A0=E5=8F=B3=E9=94=AE=E9=BC=A0=E6=A0=87?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 76ad88a..39dd2a6 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -1,6 +1,7 @@ package java2022spring; import javax.swing.*; +import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.io.*; @@ -105,7 +106,7 @@ public class Notepad extends JFrame implements ActionListener{ helpM_AboutNotepad=new JMenuItem("关于记事本(A)"); helpM_AboutNotepad.addActionListener(this); - //向菜单条添加“文件”菜单及菜单项 + //向菜单条添加“文件”菜单及菜单项 menuBar.add(fileMenu); fileMenu.add(fileM_New); fileMenu.add(fileM_Open); @@ -163,7 +164,19 @@ public class Notepad extends JFrame implements ActionListener{ popupMenu.add(popupM_Paste); popupMenu.add(popupM_Delete); - // + //向文本区域添加右键鼠标事件 + text.addMouseListener(new MouseAdapter() + { public void mousePressed(MouseEvent e) + { if(e.isPopupTrigger()) + popupMenu.show(e.getComponent(),e.getX(),e.getY()); + text.requestFocus(); + } + public void mouseReleased(MouseEvent e) { + if(e.isPopupTrigger()) + popupMenu.show(e.getComponent(),e.getX(),e.getY()); + text.requestFocus(); + } + }); //向窗口添加菜单条 this.setJMenuBar(menuBar); @@ -178,9 +191,8 @@ public class Notepad extends JFrame implements ActionListener{ this.setLocation(200,200); this.setSize(700,700); this.setVisible(true); - } - - @Override + } + @Override public void actionPerformed(ActionEvent e) { -- Gitee From 55db9d256c12a230ec823535fcf11d86aca992c4 Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Sun, 22 May 2022 22:15:28 +0800 Subject: [PATCH 09/25] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=89=93=E5=BC=80?= =?UTF-8?q?=E5=92=8C=E4=BF=9D=E5=AD=98=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 39 ++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 39dd2a6..3e6bcb5 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -192,9 +192,42 @@ public class Notepad extends JFrame implements ActionListener{ this.setSize(700,700); this.setVisible(true); } - @Override + @Override public void actionPerformed(ActionEvent e) { - - + //实现“新建”功能 + if(e.getSource()==fileM_New) { + + } + //实现“打开”功能 + else if(e.getSource()==fileM_Open) { + FileDialog fileDialog=new FileDialog(this,"打开文件",FileDialog.LOAD); + fileDialog.setVisible(true); + String path=fileDialog.getDirectory()+fileDialog.getFile(); + try { + BufferedReader readr=new BufferedReader(new FileReader(path)); + String s; + while((s=readr.readLine())!=null) + text.append(s+'\n'); + readr.close(); + } + catch(Exception ex) { + ex.printStackTrace(); + } + } + //实现“保存”功能 + else if(e.getSource()==fileM_Save) { + FileDialog fileDialog=new FileDialog(this,"保存文件",FileDialog.SAVE); + fileDialog.setVisible(true); + String path=fileDialog.getDirectory()+fileDialog.getFile(); + try { + BufferedWriter writer=new BufferedWriter(new FileWriter(path)); + String s=text.getText(); + writer.write(s); + writer.close(); + } + catch(Exception ex) { + ex.printStackTrace(); + } + } } } \ No newline at end of file -- Gitee From 413baf375a492a0d3b483b97ab3feda0dcbda423 Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Wed, 25 May 2022 23:41:43 +0800 Subject: [PATCH 10/25] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86?= =?UTF-8?q?=E9=94=99=E8=AF=AF=EF=BC=8C=E5=AE=9E=E7=8E=B0=E9=83=A8=E5=88=86?= =?UTF-8?q?=E2=80=9C=E6=96=B0=E5=BB=BA=E2=80=9D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 51 +++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 3e6bcb5..3492354 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -28,7 +28,8 @@ public class Notepad extends JFrame implements ActionListener{ //右键弹出菜单项 JPopupMenu popupMenu; JMenuItem popupM_Cancel,popupM_Cut,popupM_Copy,popupM_Paste,popupM_Delete; - + //其他变量 + String currentText;//存放当前编辑区文本 public Notepad() { setTitle("记事本"); //创建菜单条 @@ -164,6 +165,12 @@ public class Notepad extends JFrame implements ActionListener{ popupMenu.add(popupM_Paste); popupMenu.add(popupM_Delete); + //创建文本区域并添加滚动条 + text=new JTextArea(20,50); + JScrollPane scroller=new JScrollPane(text); + this.add(scroller,BorderLayout.CENTER); + text.setLineWrap(true);//设置自动换行 + //向文本区域添加右键鼠标事件 text.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) @@ -181,11 +188,6 @@ public class Notepad extends JFrame implements ActionListener{ //向窗口添加菜单条 this.setJMenuBar(menuBar); - //创建文本区域并添加滚动条 - text=new JTextArea(20,50); - JScrollPane scroller=new JScrollPane(text,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); - this.add(scroller,BorderLayout.CENTER); - text.setLineWrap(true);//设置自动换行 //设置窗口在屏幕上的位置、大小和可见性 this.setLocation(200,200); @@ -196,7 +198,38 @@ public class Notepad extends JFrame implements ActionListener{ public void actionPerformed(ActionEvent e) { //实现“新建”功能 if(e.getSource()==fileM_New) { - + String currentValue=text.getText(); + boolean isTextChange; + if(currentValue.equals(currentText)) + isTextChange = false; + else + isTextChange=true; + if(isTextChange) { + int whetherSave=JOptionPane.showConfirmDialog(this, "文件未保存,是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION); + if(whetherSave==JOptionPane.YES_OPTION) { + FileDialog fileDialog=new FileDialog(this,"另存为",FileDialog.SAVE); + fileDialog.setVisible(true); + String path=fileDialog.getDirectory()+fileDialog.getFile(); + try { + BufferedWriter writer=new BufferedWriter(new FileWriter(path)); + String s=text.getText(); + writer.write(s); + writer.close(); + } + catch(Exception ex) { + ex.printStackTrace(); + } + } + else if(whetherSave==JOptionPane.NO_OPTION) { + + } + else if(whetherSave==JOptionPane.CANCEL_OPTION) { + return; + } + } + else { + + } } //实现“打开”功能 else if(e.getSource()==fileM_Open) { @@ -214,9 +247,9 @@ public class Notepad extends JFrame implements ActionListener{ ex.printStackTrace(); } } - //实现“保存”功能 + //实现“另存为”功能 else if(e.getSource()==fileM_Save) { - FileDialog fileDialog=new FileDialog(this,"保存文件",FileDialog.SAVE); + FileDialog fileDialog=new FileDialog(this,"另存为",FileDialog.SAVE); fileDialog.setVisible(true); String path=fileDialog.getDirectory()+fileDialog.getFile(); try { -- Gitee From 0cfec0eaea6d3b126d5902ffa7c054f666a2374f Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Thu, 26 May 2022 23:18:57 +0800 Subject: [PATCH 11/25] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E2=80=9C=E6=96=B0?= =?UTF-8?q?=E5=BB=BA=E2=80=9D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 3492354..455ca1b 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -29,7 +29,7 @@ public class Notepad extends JFrame implements ActionListener{ JPopupMenu popupMenu; JMenuItem popupM_Cancel,popupM_Cut,popupM_Copy,popupM_Paste,popupM_Delete; //其他变量 - String currentText;//存放当前编辑区文本 + String oldValue;//存放当前编辑区文本 public Notepad() { setTitle("记事本"); //创建菜单条 @@ -170,6 +170,7 @@ public class Notepad extends JFrame implements ActionListener{ JScrollPane scroller=new JScrollPane(text); this.add(scroller,BorderLayout.CENTER); text.setLineWrap(true);//设置自动换行 + oldValue=text.getText(); //向文本区域添加右键鼠标事件 text.addMouseListener(new MouseAdapter() @@ -200,7 +201,7 @@ public class Notepad extends JFrame implements ActionListener{ if(e.getSource()==fileM_New) { String currentValue=text.getText(); boolean isTextChange; - if(currentValue.equals(currentText)) + if(currentValue.equals(oldValue)) isTextChange = false; else isTextChange=true; @@ -214,21 +215,29 @@ public class Notepad extends JFrame implements ActionListener{ BufferedWriter writer=new BufferedWriter(new FileWriter(path)); String s=text.getText(); writer.write(s); - writer.close(); + writer.flush(); + writer.close(); + this.setTitle("无标题——记事本"); + text.setText(null); + oldValue=text.getText(); } catch(Exception ex) { ex.printStackTrace(); } } else if(whetherSave==JOptionPane.NO_OPTION) { - + this.setTitle("无标题—记事本"); + text.setText(null); + oldValue=text.getText(); } else if(whetherSave==JOptionPane.CANCEL_OPTION) { return; } } else { - + this.setTitle("无标题—记事本"); + text.setText(null); + oldValue=text.getText(); } } //实现“打开”功能 -- Gitee From 067581c9572ddad467bcb6197f45cdee909ea18c Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Fri, 27 May 2022 23:09:26 +0800 Subject: [PATCH 12/25] =?UTF-8?q?=E5=AE=8C=E5=96=84=E2=80=9C=E6=89=93?= =?UTF-8?q?=E5=BC=80=E2=80=9D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 89 ++++++++++++++++++++++++++++----- 1 file changed, 76 insertions(+), 13 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 455ca1b..b4156d9 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -138,7 +138,7 @@ public class Notepad extends JFrame implements ActionListener{ helpMenu.add(helpM_CheckHelp); helpMenu.addSeparator(); helpMenu.add(helpM_AboutNotepad); - + //创建右键弹出菜单并注册右键菜单事件 popupMenu=new JPopupMenu(); @@ -189,12 +189,12 @@ public class Notepad extends JFrame implements ActionListener{ //向窗口添加菜单条 this.setJMenuBar(menuBar); - //设置窗口在屏幕上的位置、大小和可见性 this.setLocation(200,200); this.setSize(700,700); this.setVisible(true); } + @Override public void actionPerformed(ActionEvent e) { //实现“新建”功能 @@ -242,18 +242,81 @@ public class Notepad extends JFrame implements ActionListener{ } //实现“打开”功能 else if(e.getSource()==fileM_Open) { - FileDialog fileDialog=new FileDialog(this,"打开文件",FileDialog.LOAD); - fileDialog.setVisible(true); - String path=fileDialog.getDirectory()+fileDialog.getFile(); - try { - BufferedReader readr=new BufferedReader(new FileReader(path)); - String s; - while((s=readr.readLine())!=null) - text.append(s+'\n'); - readr.close(); + String currentValue=text.getText(); + boolean isTextChange; + if(currentValue.equals(oldValue)) + isTextChange = false; + else + isTextChange=true; + if(isTextChange) { + int whetherSave=JOptionPane.showConfirmDialog(this, "文件未保存,是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION); + if(whetherSave==JOptionPane.YES_OPTION) { + FileDialog fileDialog=new FileDialog(this,"另存为",FileDialog.SAVE); + fileDialog.setVisible(true); + String path=fileDialog.getDirectory()+fileDialog.getFile(); + try { + BufferedWriter writer=new BufferedWriter(new FileWriter(path)); + String s=text.getText(); + writer.write(s); + writer.flush(); + writer.close(); + FileDialog fileDialogs=new FileDialog(this,"打开文件",FileDialog.LOAD); + fileDialogs.setVisible(true); + String paths=fileDialogs.getDirectory()+fileDialogs.getFile(); + try { + BufferedReader reader=new BufferedReader(new FileReader(paths)); + String t; + text.setText(null); + while((t=reader.readLine())!=null) + text.append(t+'\n'); + reader.close(); + } + catch(Exception ex) { + ex.printStackTrace(); + } + oldValue=text.getText(); + } + catch(Exception ex) { + ex.printStackTrace(); + } + } + else if(whetherSave==JOptionPane.NO_OPTION) { + FileDialog fileDialog=new FileDialog(this,"打开文件",FileDialog.LOAD); + fileDialog.setVisible(true); + String path=fileDialog.getDirectory()+fileDialog.getFile(); + try { + BufferedReader reader=new BufferedReader(new FileReader(path)); + String s; + text.setText(null); + while((s=reader.readLine())!=null) + text.append(s+'\n'); + reader.close(); + } + catch(Exception ex) { + ex.printStackTrace(); + } + oldValue=text.getText(); + } + else if(whetherSave==JOptionPane.CANCEL_OPTION) { + return; + } } - catch(Exception ex) { - ex.printStackTrace(); + else { + FileDialog fileDialog=new FileDialog(this,"打开文件",FileDialog.LOAD); + fileDialog.setVisible(true); + String path=fileDialog.getDirectory()+fileDialog.getFile(); + try { + BufferedReader reader=new BufferedReader(new FileReader(path)); + String s; + text.setText(null); + while((s=reader.readLine())!=null) + text.append(s+'\n'); + reader.close(); + } + catch(Exception ex) { + ex.printStackTrace(); + } + oldValue=text.getText(); } } //实现“另存为”功能 -- Gitee From 588045fa0b3690d822cb3ec4dbdf6076536d485a Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Sat, 28 May 2022 21:20:47 +0800 Subject: [PATCH 13/25] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AA=97=E5=8F=A3?= =?UTF-8?q?=E7=9B=91=E5=90=AC=E5=99=A8=EF=BC=8C=E5=B9=B6=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E7=AA=97=E5=8F=A3=E6=97=B6=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index b4156d9..2e6a260 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -139,6 +139,10 @@ public class Notepad extends JFrame implements ActionListener{ helpMenu.addSeparator(); helpMenu.add(helpM_AboutNotepad); + //添加状态栏 + statusLabel=new JLabel(" "); + this.add(statusLabel,BorderLayout.SOUTH); + //创建右键弹出菜单并注册右键菜单事件 popupMenu=new JPopupMenu(); @@ -189,12 +193,54 @@ public class Notepad extends JFrame implements ActionListener{ //向窗口添加菜单条 this.setJMenuBar(menuBar); + //添加窗口监听器 + addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e){ + exitWindowChoose(); + } + }); + //设置窗口在屏幕上的位置、大小和可见性 this.setLocation(200,200); this.setSize(700,700); this.setVisible(true); } + //关闭窗口时调用方法 + public void exitWindowChoose() { + String currentValue=text.getText(); + boolean isTextChange; + if(currentValue.equals(oldValue)) + isTextChange = false; + else + isTextChange=true; + if(isTextChange) { + int whetherSave=JOptionPane.showConfirmDialog(this, "文件未保存,是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION); + if(whetherSave==JOptionPane.YES_OPTION) { + FileDialog fileDialog=new FileDialog(this,"另存为",FileDialog.SAVE); + fileDialog.setVisible(true); + String path=fileDialog.getDirectory()+fileDialog.getFile()+".txt"; + try { + BufferedWriter writer=new BufferedWriter(new FileWriter(path)); + String s=text.getText(); + writer.write(s); + writer.flush(); + writer.close(); + System.exit(0); + } + catch(Exception ex) { + ex.printStackTrace(); + } + } + else if(whetherSave==JOptionPane.NO_OPTION) { + System.exit(0); + } + else if(whetherSave==JOptionPane.CANCEL_OPTION) { + return; + } + } + } + @Override public void actionPerformed(ActionEvent e) { //实现“新建”功能 -- Gitee From 3345df38a98b94be9d5a2a60a294929f0510e0df Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Sun, 29 May 2022 20:28:15 +0800 Subject: [PATCH 14/25] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E2=80=9C=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E2=80=9D=E5=8A=9F=E8=83=BD=EF=BC=8C=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E2=80=9C=E6=96=87=E4=BB=B6=E2=80=9D=E8=8F=9C=E5=8D=95=E4=B8=8B?= =?UTF-8?q?=E5=90=84=E8=8F=9C=E5=8D=95=E9=A1=B9=E5=8F=8A=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E7=AA=97=E5=8F=A3=E6=97=B6=E8=B0=83=E7=94=A8=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=B8=AD=E4=B8=8E=E4=BF=9D=E5=AD=98=E7=9B=B8=E5=85=B3=E7=9A=84?= =?UTF-8?q?=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 261 +++++++++++++++++++++++--------- 1 file changed, 187 insertions(+), 74 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 2e6a260..cc193de 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -30,6 +30,9 @@ public class Notepad extends JFrame implements ActionListener{ JMenuItem popupM_Cancel,popupM_Cut,popupM_Copy,popupM_Paste,popupM_Delete; //其他变量 String oldValue;//存放当前编辑区文本 + boolean isNewFile=true; + String currentFile; + public Notepad() { setTitle("记事本"); //创建菜单条 @@ -217,20 +220,34 @@ public class Notepad extends JFrame implements ActionListener{ if(isTextChange) { int whetherSave=JOptionPane.showConfirmDialog(this, "文件未保存,是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION); if(whetherSave==JOptionPane.YES_OPTION) { - FileDialog fileDialog=new FileDialog(this,"另存为",FileDialog.SAVE); - fileDialog.setVisible(true); - String path=fileDialog.getDirectory()+fileDialog.getFile()+".txt"; - try { - BufferedWriter writer=new BufferedWriter(new FileWriter(path)); - String s=text.getText(); - writer.write(s); - writer.flush(); - writer.close(); - System.exit(0); - } - catch(Exception ex) { - ex.printStackTrace(); - } + if(isNewFile) { + FileDialog fileDialog=new FileDialog(this,"另存为",FileDialog.SAVE); + fileDialog.setVisible(true); + String path=fileDialog.getDirectory()+fileDialog.getFile()+".txt"; + try { + BufferedWriter writer=new BufferedWriter(new FileWriter(path)); + String s=text.getText(); + writer.write(s); + writer.flush(); + writer.close(); + System.exit(0); + } + catch(Exception ex) { + ex.printStackTrace(); + } + } + + else { + try { + BufferedWriter writer=new BufferedWriter(new FileWriter(currentFile)); + writer.write(text.getText(),0,text.getText().length()); + writer.flush(); + writer.close(); + } + catch(Exception ex) { + + } + } } else if(whetherSave==JOptionPane.NO_OPTION) { System.exit(0); @@ -239,6 +256,9 @@ public class Notepad extends JFrame implements ActionListener{ return; } } + else { + System.exit(0); + } } @Override @@ -254,26 +274,42 @@ public class Notepad extends JFrame implements ActionListener{ if(isTextChange) { int whetherSave=JOptionPane.showConfirmDialog(this, "文件未保存,是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION); if(whetherSave==JOptionPane.YES_OPTION) { - FileDialog fileDialog=new FileDialog(this,"另存为",FileDialog.SAVE); - fileDialog.setVisible(true); - String path=fileDialog.getDirectory()+fileDialog.getFile(); - try { - BufferedWriter writer=new BufferedWriter(new FileWriter(path)); - String s=text.getText(); - writer.write(s); - writer.flush(); - writer.close(); - this.setTitle("无标题——记事本"); - text.setText(null); - oldValue=text.getText(); - } - catch(Exception ex) { - ex.printStackTrace(); - } + if(isNewFile) { + FileDialog fileDialog=new FileDialog(this,"另存为",FileDialog.SAVE); + fileDialog.setVisible(true); + String path=fileDialog.getDirectory()+fileDialog.getFile()+".txt"; + try { + BufferedWriter writer=new BufferedWriter(new FileWriter(path)); + String s=text.getText(); + writer.write(s); + writer.flush(); + writer.close(); + this.setTitle("无标题—记事本"); + text.setText(null); + oldValue=text.getText(); + currentFile=path; + } + catch(Exception ex) { + ex.printStackTrace(); + } + } + else { + try { + BufferedWriter writer=new BufferedWriter(new FileWriter(currentFile)); + writer.write(text.getText(),0,text.getText().length()); + writer.flush(); + writer.close(); + isNewFile=true; + } + catch(Exception ex) { + + } + } } else if(whetherSave==JOptionPane.NO_OPTION) { this.setTitle("无标题—记事本"); text.setText(null); + isNewFile=true; oldValue=text.getText(); } else if(whetherSave==JOptionPane.CANCEL_OPTION) { @@ -282,7 +318,8 @@ public class Notepad extends JFrame implements ActionListener{ } else { this.setTitle("无标题—记事本"); - text.setText(null); + text.setText(null); + isNewFile=true; oldValue=text.getText(); } } @@ -295,37 +332,72 @@ public class Notepad extends JFrame implements ActionListener{ else isTextChange=true; if(isTextChange) { - int whetherSave=JOptionPane.showConfirmDialog(this, "文件未保存,是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION); - if(whetherSave==JOptionPane.YES_OPTION) { - FileDialog fileDialog=new FileDialog(this,"另存为",FileDialog.SAVE); - fileDialog.setVisible(true); - String path=fileDialog.getDirectory()+fileDialog.getFile(); - try { - BufferedWriter writer=new BufferedWriter(new FileWriter(path)); - String s=text.getText(); - writer.write(s); - writer.flush(); - writer.close(); - FileDialog fileDialogs=new FileDialog(this,"打开文件",FileDialog.LOAD); - fileDialogs.setVisible(true); - String paths=fileDialogs.getDirectory()+fileDialogs.getFile(); - try { - BufferedReader reader=new BufferedReader(new FileReader(paths)); - String t; - text.setText(null); - while((t=reader.readLine())!=null) - text.append(t+'\n'); - reader.close(); - } - catch(Exception ex) { - ex.printStackTrace(); - } - oldValue=text.getText(); - } - catch(Exception ex) { - ex.printStackTrace(); - } - } + int whetherSave=JOptionPane.showConfirmDialog(this, "文件未保存,是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION); + if(whetherSave==JOptionPane.YES_OPTION) { + if(isNewFile) { + FileDialog fileDialog=new FileDialog(this,"另存为",FileDialog.SAVE); + fileDialog.setVisible(true); + String path=fileDialog.getDirectory()+fileDialog.getFile()+".txt"; + try { + BufferedWriter writer=new BufferedWriter(new FileWriter(path)); + String s=text.getText(); + writer.write(s); + writer.flush(); + writer.close(); + FileDialog fileDialogs=new FileDialog(this,"打开文件",FileDialog.LOAD); + fileDialogs.setVisible(true); + String paths=fileDialogs.getDirectory()+fileDialogs.getFile(); + try { + BufferedReader reader=new BufferedReader(new FileReader(paths)); + String t; + text.setText(null); + while((t=reader.readLine())!=null) + text.append(t+'\n'); + reader.close(); + this.setTitle(fileDialogs.getFile()+"["+fileDialogs.getDirectory()+"]"+"—记事本"); + isNewFile=false; + oldValue=text.getText(); + currentFile=paths; + } + catch(Exception ex) { + ex.printStackTrace(); + } + } + catch(Exception ex) { + ex.printStackTrace(); + } + } + + else { + try { + BufferedWriter writer=new BufferedWriter(new FileWriter(currentFile)); + writer.write(text.getText(),0,text.getText().length()); + writer.flush(); + writer.close(); + FileDialog fileDialogs=new FileDialog(this,"打开文件",FileDialog.LOAD); + fileDialogs.setVisible(true); + String paths=fileDialogs.getDirectory()+fileDialogs.getFile(); + try { + BufferedReader reader=new BufferedReader(new FileReader(paths)); + String t; + text.setText(null); + while((t=reader.readLine())!=null) + text.append(t+'\n'); + reader.close(); + this.setTitle(fileDialogs.getFile()+"["+fileDialogs.getDirectory()+"]"+"—记事本"); + isNewFile=false; + oldValue=text.getText(); + currentFile=paths; + } + catch(Exception ex) { + ex.printStackTrace(); + } + } + catch(Exception ex) { + + } + } + } else if(whetherSave==JOptionPane.NO_OPTION) { FileDialog fileDialog=new FileDialog(this,"打开文件",FileDialog.LOAD); fileDialog.setVisible(true); @@ -337,11 +409,14 @@ public class Notepad extends JFrame implements ActionListener{ while((s=reader.readLine())!=null) text.append(s+'\n'); reader.close(); + isNewFile=false; + oldValue=text.getText(); + currentFile=path; + this.setTitle(fileDialog.getFile()+"["+fileDialog.getDirectory()+"]"+"—记事本"); } catch(Exception ex) { ex.printStackTrace(); } - oldValue=text.getText(); } else if(whetherSave==JOptionPane.CANCEL_OPTION) { return; @@ -349,32 +424,70 @@ public class Notepad extends JFrame implements ActionListener{ } else { FileDialog fileDialog=new FileDialog(this,"打开文件",FileDialog.LOAD); + fileDialog.setVisible(true); + String path=fileDialog.getDirectory()+fileDialog.getFile(); + try { + BufferedReader reader=new BufferedReader(new FileReader(path)); + String s; + text.setText(null); + while((s=reader.readLine())!=null) + text.append(s+'\n'); + reader.close(); + isNewFile=false; + oldValue=text.getText(); + currentFile=path; + this.setTitle(fileDialog.getFile()+"["+fileDialog.getDirectory()+"]"+"—记事本"); + } + catch(Exception ex) { + ex.printStackTrace(); + } + } + } + //实现“保存”功能 + else if(e.getSource()==fileM_Save) { + if(isNewFile) { + FileDialog fileDialog=new FileDialog(this,"保存为",FileDialog.SAVE); fileDialog.setVisible(true); - String path=fileDialog.getDirectory()+fileDialog.getFile(); + String path=fileDialog.getDirectory()+fileDialog.getFile()+".txt"; try { - BufferedReader reader=new BufferedReader(new FileReader(path)); - String s; - text.setText(null); - while((s=reader.readLine())!=null) - text.append(s+'\n'); - reader.close(); + BufferedWriter writer=new BufferedWriter(new FileWriter(path)); + String s=text.getText(); + writer.write(s); + writer.close(); + isNewFile=false; + oldValue=text.getText(); + currentFile=path; } catch(Exception ex) { ex.printStackTrace(); } - oldValue=text.getText(); + } + else { + try { + BufferedWriter writer=new BufferedWriter(new FileWriter(currentFile)); + writer.write(text.getText(),0,text.getText().length()); + writer.flush(); + writer.close(); + oldValue=text.getText(); + } + catch(Exception ex) { + + } } } //实现“另存为”功能 - else if(e.getSource()==fileM_Save) { + else if(e.getSource()==fileM_SaveAs) { FileDialog fileDialog=new FileDialog(this,"另存为",FileDialog.SAVE); fileDialog.setVisible(true); - String path=fileDialog.getDirectory()+fileDialog.getFile(); + String path=fileDialog.getDirectory()+fileDialog.getFile()+".txt"; try { BufferedWriter writer=new BufferedWriter(new FileWriter(path)); String s=text.getText(); writer.write(s); - writer.close(); + writer.close(); + isNewFile=false; + oldValue=text.getText(); + currentFile=path; } catch(Exception ex) { ex.printStackTrace(); -- Gitee From 23945af4d093a1c0b7bd5d565e46d3e944a1a4f7 Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Mon, 30 May 2022 23:14:15 +0800 Subject: [PATCH 15/25] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=B9=B6=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E2=80=9C=E6=96=B0=E5=BB=BA=E7=AA=97=E5=8F=A3=E2=80=9D?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=AE=9E=E7=8E=B0=E2=80=9C=E9=80=80?= =?UTF-8?q?=E5=87=BA=E2=80=9D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 25 ++++++++++++++++++++----- src/java2022spring/Test.java | 2 +- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index cc193de..e5e2ce7 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -11,8 +11,8 @@ public class Notepad extends JFrame implements ActionListener{ JMenuBar menuBar; //菜单 JMenu fileMenu,editMenu,formatMenu,viewMenu,helpMenu; - //“文件”的菜单项新建、打开、保存、另存为和退出功能 - JMenuItem fileM_New,fileM_Open,fileM_Save,fileM_SaveAs,fileM_Exit; + //“文件”的菜单项新建、新建窗口、打开、保存、另存为和退出功能 + JMenuItem fileM_New,fileM_NewWindow,fileM_Open,fileM_Save,fileM_SaveAs,fileM_Exit; //“编辑”的菜单项撤消、剪切、复制、粘贴和删除功能 JMenuItem editM_Cancel,editM_Cut,editM_Copy,editM_Paste,editM_Delete; //“格式”的菜单项自动换行功能 @@ -44,6 +44,10 @@ public class Notepad extends JFrame implements ActionListener{ fileM_New=new JMenuItem("新建(N)"); fileM_New.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));//设置快捷键CTRL+N fileM_New.addActionListener(this); + + fileM_NewWindow=new JMenuItem("新建窗口"); + fileM_NewWindow.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK+ActionEvent.SHIFT_MASK));//设置快捷键CTRL+Shift+N + fileM_NewWindow.addActionListener(this); fileM_Open=new JMenuItem("打开(O)..."); fileM_Open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));//设置快捷键CTRL+O @@ -113,6 +117,7 @@ public class Notepad extends JFrame implements ActionListener{ //向菜单条添加“文件”菜单及菜单项 menuBar.add(fileMenu); fileMenu.add(fileM_New); + fileMenu.add(fileM_NewWindow); fileMenu.add(fileM_Open); fileMenu.add(fileM_Save); fileMenu.add(fileM_SaveAs); @@ -230,7 +235,7 @@ public class Notepad extends JFrame implements ActionListener{ writer.write(s); writer.flush(); writer.close(); - System.exit(0); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } catch(Exception ex) { ex.printStackTrace(); @@ -243,6 +248,7 @@ public class Notepad extends JFrame implements ActionListener{ writer.write(text.getText(),0,text.getText().length()); writer.flush(); writer.close(); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } catch(Exception ex) { @@ -250,14 +256,14 @@ public class Notepad extends JFrame implements ActionListener{ } } else if(whetherSave==JOptionPane.NO_OPTION) { - System.exit(0); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } else if(whetherSave==JOptionPane.CANCEL_OPTION) { return; } } else { - System.exit(0); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } } @@ -323,6 +329,11 @@ public class Notepad extends JFrame implements ActionListener{ oldValue=text.getText(); } } + //实现“新建窗口”功能 + else if(e.getSource()==fileM_NewWindow) { + Notepad notepad=new Notepad(); + notepad.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + } //实现“打开”功能 else if(e.getSource()==fileM_Open) { String currentValue=text.getText(); @@ -493,5 +504,9 @@ public class Notepad extends JFrame implements ActionListener{ ex.printStackTrace(); } } + //实现“退出”功能 + else if(e.getSource()==fileM_Exit) { + exitWindowChoose(); + } } } \ No newline at end of file diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 74336d9..87983e7 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -5,6 +5,6 @@ import javax.swing.*; public class Test { public static void main(String args[]) { Notepad notepad=new Notepad(); - notepad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + notepad.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } } -- Gitee From ac4fa799f8e9049993e94d4f9a9f08fbe16d2d44 Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Tue, 31 May 2022 22:20:13 +0800 Subject: [PATCH 16/25] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E2=80=9C=E6=89=93?= =?UTF-8?q?=E5=BC=80=E2=80=9D=E5=8A=9F=E8=83=BD=E4=B8=AD=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 183 +++++++++++++++++++------------- 1 file changed, 108 insertions(+), 75 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index e5e2ce7..7d9a604 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -1,7 +1,10 @@ package java2022spring; import javax.swing.*; -import javax.swing.event.*; +import javax.swing.event.*; +import javax.swing.undo.CannotUndoException; +import javax.swing.undo.UndoManager; + import java.awt.*; import java.awt.event.*; import java.io.*; @@ -349,35 +352,37 @@ public class Notepad extends JFrame implements ActionListener{ FileDialog fileDialog=new FileDialog(this,"另存为",FileDialog.SAVE); fileDialog.setVisible(true); String path=fileDialog.getDirectory()+fileDialog.getFile()+".txt"; - try { - BufferedWriter writer=new BufferedWriter(new FileWriter(path)); - String s=text.getText(); - writer.write(s); - writer.flush(); - writer.close(); - FileDialog fileDialogs=new FileDialog(this,"打开文件",FileDialog.LOAD); - fileDialogs.setVisible(true); - String paths=fileDialogs.getDirectory()+fileDialogs.getFile(); - try { - BufferedReader reader=new BufferedReader(new FileReader(paths)); - String t; - text.setText(null); - while((t=reader.readLine())!=null) - text.append(t+'\n'); - reader.close(); - this.setTitle(fileDialogs.getFile()+"["+fileDialogs.getDirectory()+"]"+"—记事本"); - isNewFile=false; - oldValue=text.getText(); - currentFile=paths; - } - catch(Exception ex) { - ex.printStackTrace(); - } + try { + BufferedWriter writer=new BufferedWriter(new FileWriter(path)); + String s=text.getText(); + writer.write(s); + writer.flush(); + writer.close(); + FileDialog fileDialogs=new FileDialog(this,"打开文件",FileDialog.LOAD); + fileDialogs.setVisible(true); + String paths=fileDialogs.getDirectory()+fileDialogs.getFile(); + if(fileDialogs.getFile()!=null) { + try { + BufferedReader reader=new BufferedReader(new FileReader(path)); + String t; + text.setText(null); + while((t=reader.readLine())!=null) + text.append(t+'\n'); + this.setTitle(fileDialogs.getFile()+"["+fileDialogs.getDirectory()+"]"+"—记事本"); + reader.close(); + isNewFile=false; + currentFile=paths; + oldValue=text.getText(); + } + catch(Exception ex) { + ex.printStackTrace(); + } + } + } + catch(Exception ex) { + ex.printStackTrace(); + } } - catch(Exception ex) { - ex.printStackTrace(); - } - } else { try { @@ -388,21 +393,23 @@ public class Notepad extends JFrame implements ActionListener{ FileDialog fileDialogs=new FileDialog(this,"打开文件",FileDialog.LOAD); fileDialogs.setVisible(true); String paths=fileDialogs.getDirectory()+fileDialogs.getFile(); - try { - BufferedReader reader=new BufferedReader(new FileReader(paths)); - String t; - text.setText(null); - while((t=reader.readLine())!=null) - text.append(t+'\n'); - reader.close(); - this.setTitle(fileDialogs.getFile()+"["+fileDialogs.getDirectory()+"]"+"—记事本"); - isNewFile=false; - oldValue=text.getText(); - currentFile=paths; - } - catch(Exception ex) { - ex.printStackTrace(); - } + if(fileDialogs.getFile()!=null) { + try { + BufferedReader reader=new BufferedReader(new FileReader(paths)); + String s; + text.setText(null); + while((s=reader.readLine())!=null) + text.append(s+'\n'); + this.setTitle(fileDialogs.getFile()+"["+fileDialogs.getDirectory()+"]"+"—记事本"); + reader.close(); + isNewFile=false; + currentFile=paths; + oldValue=text.getText(); + } + catch(Exception ex) { + ex.printStackTrace(); + } + } } catch(Exception ex) { @@ -413,20 +420,22 @@ public class Notepad extends JFrame implements ActionListener{ FileDialog fileDialog=new FileDialog(this,"打开文件",FileDialog.LOAD); fileDialog.setVisible(true); String path=fileDialog.getDirectory()+fileDialog.getFile(); - try { - BufferedReader reader=new BufferedReader(new FileReader(path)); - String s; - text.setText(null); - while((s=reader.readLine())!=null) - text.append(s+'\n'); - reader.close(); - isNewFile=false; - oldValue=text.getText(); - currentFile=path; - this.setTitle(fileDialog.getFile()+"["+fileDialog.getDirectory()+"]"+"—记事本"); - } - catch(Exception ex) { - ex.printStackTrace(); + if(fileDialog.getFile()!=null) { + try { + BufferedReader reader=new BufferedReader(new FileReader(path)); + String s; + text.setText(null); + while((s=reader.readLine())!=null) + text.append(s+'\n'); + this.setTitle(fileDialog.getFile()+"["+fileDialog.getDirectory()+"]"+"—记事本"); + reader.close(); + isNewFile=false; + currentFile=path; + oldValue=text.getText(); + } + catch(Exception ex) { + ex.printStackTrace(); + } } } else if(whetherSave==JOptionPane.CANCEL_OPTION) { @@ -437,20 +446,22 @@ public class Notepad extends JFrame implements ActionListener{ FileDialog fileDialog=new FileDialog(this,"打开文件",FileDialog.LOAD); fileDialog.setVisible(true); String path=fileDialog.getDirectory()+fileDialog.getFile(); - try { - BufferedReader reader=new BufferedReader(new FileReader(path)); - String s; - text.setText(null); - while((s=reader.readLine())!=null) - text.append(s+'\n'); - reader.close(); - isNewFile=false; - oldValue=text.getText(); - currentFile=path; - this.setTitle(fileDialog.getFile()+"["+fileDialog.getDirectory()+"]"+"—记事本"); - } - catch(Exception ex) { - ex.printStackTrace(); + if(fileDialog.getFile()!=null) { + try { + BufferedReader reader=new BufferedReader(new FileReader(path)); + String s; + text.setText(null); + while((s=reader.readLine())!=null) + text.append(s+'\n'); + this.setTitle(fileDialog.getFile()+"["+fileDialog.getDirectory()+"]"+"—记事本"); + reader.close(); + isNewFile=false; + currentFile=path; + oldValue=text.getText(); + } + catch(Exception ex) { + ex.printStackTrace(); + } } } } @@ -466,8 +477,8 @@ public class Notepad extends JFrame implements ActionListener{ writer.write(s); writer.close(); isNewFile=false; + currentFile=path; oldValue=text.getText(); - currentFile=path; } catch(Exception ex) { ex.printStackTrace(); @@ -497,9 +508,9 @@ public class Notepad extends JFrame implements ActionListener{ writer.write(s); writer.close(); isNewFile=false; - oldValue=text.getText(); currentFile=path; - } + oldValue=text.getText(); + } catch(Exception ex) { ex.printStackTrace(); } @@ -508,5 +519,27 @@ public class Notepad extends JFrame implements ActionListener{ else if(e.getSource()==fileM_Exit) { exitWindowChoose(); } + //实现“撤销”功能 + else if(e.getSource()==editM_Cancel||e.getSource()==popupM_Cancel) { + + } + //实现“剪切”功能 + else if(e.getSource()==editM_Cut||e.getSource()==popupM_Cut) { + + } + //实现“复制”功能 + else if(e.getSource()==editM_Copy||e.getSource()==popupM_Copy) { + + } + //实现“粘贴”功能 + else if(e.getSource()==editM_Paste||e.getSource()==popupM_Paste) { + + } + //实现“删除”功能 + else if(e.getSource()==editM_Delete||e.getSource()==popupM_Delete) { + + } + + } } \ No newline at end of file -- Gitee From 2cb77b68a21d00450f60753886663d44258e21eb Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Wed, 1 Jun 2022 23:58:46 +0800 Subject: [PATCH 17/25] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E2=80=9C=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=8D=A2=E8=A1=8C=E2=80=9D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 7d9a604..58439c2 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -19,7 +19,7 @@ public class Notepad extends JFrame implements ActionListener{ //“编辑”的菜单项撤消、剪切、复制、粘贴和删除功能 JMenuItem editM_Cancel,editM_Cut,editM_Copy,editM_Paste,editM_Delete; //“格式”的菜单项自动换行功能 - JMenuItem formatM_LineFeed; + JCheckBoxMenuItem formatM_LineFeed; //“查看”的菜单项状态栏功能 JMenuItem viewM_Status; //“帮助”的菜单项查看帮助和关于记事本功能 @@ -94,9 +94,10 @@ public class Notepad extends JFrame implements ActionListener{ formatMenu=new JMenu("格式(O)"); formatMenu.setMnemonic('O');//设置快捷键ALT+O - formatM_LineFeed=new JMenuItem("自动换行(W)"); + formatM_LineFeed=new JCheckBoxMenuItem("自动换行(W)"); formatM_LineFeed.setMnemonic('W');//设置快捷键ALT+W formatM_LineFeed.addActionListener(this); + formatM_LineFeed.setState(true); //创建查看菜单及菜单项并注册事件监听 viewMenu=new JMenu("查看(V)"); @@ -539,7 +540,13 @@ public class Notepad extends JFrame implements ActionListener{ else if(e.getSource()==editM_Delete||e.getSource()==popupM_Delete) { } - + //实现“自动换行”功能 + else if(e.getSource()==formatM_LineFeed) { + if(formatM_LineFeed.getState()) + text.setLineWrap(true); + else + text.setLineWrap(false); + } } } \ No newline at end of file -- Gitee From 6fa4f756ab225197b0d1ca657d52d0cb3dd97597 Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Fri, 3 Jun 2022 16:34:21 +0800 Subject: [PATCH 18/25] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E2=80=9C=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=A0=8F=E2=80=9D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 54 +++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 58439c2..162dc24 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -2,12 +2,14 @@ package java2022spring; import javax.swing.*; import javax.swing.event.*; +import javax.swing.text.BadLocationException; import javax.swing.undo.CannotUndoException; import javax.swing.undo.UndoManager; import java.awt.*; import java.awt.event.*; import java.io.*; +import java.text.SimpleDateFormat; public class Notepad extends JFrame implements ActionListener{ //菜单条 @@ -21,7 +23,7 @@ public class Notepad extends JFrame implements ActionListener{ //“格式”的菜单项自动换行功能 JCheckBoxMenuItem formatM_LineFeed; //“查看”的菜单项状态栏功能 - JMenuItem viewM_Status; + JCheckBoxMenuItem viewM_Status; //“帮助”的菜单项查看帮助和关于记事本功能 JMenuItem helpM_CheckHelp,helpM_AboutNotepad; //“文本”编辑区 @@ -103,8 +105,9 @@ public class Notepad extends JFrame implements ActionListener{ viewMenu=new JMenu("查看(V)"); viewMenu.setMnemonic('V');//设置快捷键ALT+V - viewM_Status=new JMenuItem("状态栏(S)"); + viewM_Status=new JCheckBoxMenuItem("状态栏(S)"); viewM_Status.setMnemonic('S');//设置快捷键ALT+S + viewM_Status.setState(true); viewM_Status.addActionListener(this); //创建帮助菜单及菜单项并注册事件监听 @@ -150,10 +153,7 @@ public class Notepad extends JFrame implements ActionListener{ helpMenu.add(helpM_CheckHelp); helpMenu.addSeparator(); helpMenu.add(helpM_AboutNotepad); - - //添加状态栏 - statusLabel=new JLabel(" "); - this.add(statusLabel,BorderLayout.SOUTH); + //创建右键弹出菜单并注册右键菜单事件 popupMenu=new JPopupMenu(); @@ -201,6 +201,10 @@ public class Notepad extends JFrame implements ActionListener{ text.requestFocus(); } }); + + //添加状态栏 + statusLabel=new JLabel("按F1获取帮助"); + this.add(statusLabel,BorderLayout.SOUTH); //向窗口添加菜单条 this.setJMenuBar(menuBar); @@ -369,10 +373,13 @@ public class Notepad extends JFrame implements ActionListener{ text.setText(null); while((t=reader.readLine())!=null) text.append(t+'\n'); - this.setTitle(fileDialogs.getFile()+"["+fileDialogs.getDirectory()+"]"+"—记事本"); + this.setTitle(fileDialogs.getFile()+"—记事本"); reader.close(); isNewFile=false; currentFile=paths; + File file=new File(currentFile); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + statusLabel.setText(" 当前打开文件:"+currentFile+" | "+"上次修改时间:"+sdf.format(file.lastModified())); oldValue=text.getText(); } catch(Exception ex) { @@ -401,10 +408,13 @@ public class Notepad extends JFrame implements ActionListener{ text.setText(null); while((s=reader.readLine())!=null) text.append(s+'\n'); - this.setTitle(fileDialogs.getFile()+"["+fileDialogs.getDirectory()+"]"+"—记事本"); + this.setTitle(fileDialogs.getFile()+"—记事本"); reader.close(); isNewFile=false; currentFile=paths; + File file=new File(currentFile); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + statusLabel.setText(" 当前打开文件:"+currentFile+" | "+"上次修改时间:"+sdf.format(file.lastModified())); oldValue=text.getText(); } catch(Exception ex) { @@ -428,10 +438,13 @@ public class Notepad extends JFrame implements ActionListener{ text.setText(null); while((s=reader.readLine())!=null) text.append(s+'\n'); - this.setTitle(fileDialog.getFile()+"["+fileDialog.getDirectory()+"]"+"—记事本"); + this.setTitle(fileDialog.getFile()+"—记事本"); reader.close(); isNewFile=false; currentFile=path; + File file=new File(currentFile); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + statusLabel.setText(" 当前打开文件:"+currentFile+" | "+"上次修改时间:"+sdf.format(file.lastModified())); oldValue=text.getText(); } catch(Exception ex) { @@ -454,11 +467,14 @@ public class Notepad extends JFrame implements ActionListener{ text.setText(null); while((s=reader.readLine())!=null) text.append(s+'\n'); - this.setTitle(fileDialog.getFile()+"["+fileDialog.getDirectory()+"]"+"—记事本"); + this.setTitle(fileDialog.getFile()+"—记事本"); reader.close(); isNewFile=false; - currentFile=path; - oldValue=text.getText(); + currentFile=path; + File file=new File(currentFile); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + statusLabel.setText(" 当前打开文件:"+currentFile+" | "+"上次修改时间:"+sdf.format(file.lastModified())); + oldValue=text.getText(); } catch(Exception ex) { ex.printStackTrace(); @@ -479,6 +495,9 @@ public class Notepad extends JFrame implements ActionListener{ writer.close(); isNewFile=false; currentFile=path; + File file=new File(currentFile); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + statusLabel.setText(" 当前打开文件:"+currentFile+" | "+"上次修改时间:"+sdf.format(file.lastModified())); oldValue=text.getText(); } catch(Exception ex) { @@ -491,6 +510,9 @@ public class Notepad extends JFrame implements ActionListener{ writer.write(text.getText(),0,text.getText().length()); writer.flush(); writer.close(); + File file=new File(currentFile); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + statusLabel.setText(" 当前打开文件:"+currentFile+" | "+"上次修改时间:"+sdf.format(file.lastModified())); oldValue=text.getText(); } catch(Exception ex) { @@ -547,6 +569,12 @@ public class Notepad extends JFrame implements ActionListener{ else text.setLineWrap(false); } - + //实现“状态栏”功能 + else if(e.getSource()==viewM_Status) { + if(viewM_Status.getState()) + statusLabel.setVisible(true); + else + statusLabel.setVisible(false); + } } } \ No newline at end of file -- Gitee From 98cc7876e531709b165cefe16a87a20685fb6d21 Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Sat, 4 Jun 2022 00:35:26 +0800 Subject: [PATCH 19/25] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E2=80=9C=E6=9F=A5?= =?UTF-8?q?=E7=9C=8B=E5=B8=AE=E5=8A=A9=E2=80=9D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 162dc24..416faae 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -73,7 +73,7 @@ public class Notepad extends JFrame implements ActionListener{ editMenu.setMnemonic('E');//设置快捷键ALT+E editM_Cancel=new JMenuItem("撤销(U)"); - editM_Cancel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,ActionEvent.CTRL_MASK));//设置快捷键CTRL+U + editM_Cancel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,ActionEvent.CTRL_MASK));//设置快捷键CTRL+Z editM_Cancel.addActionListener(this); editM_Cut=new JMenuItem("剪切(T)"); @@ -217,7 +217,7 @@ public class Notepad extends JFrame implements ActionListener{ }); //设置窗口在屏幕上的位置、大小和可见性 - this.setLocation(200,200); + this.setLocation(100,100); this.setSize(700,700); this.setVisible(true); } @@ -575,6 +575,28 @@ public class Notepad extends JFrame implements ActionListener{ statusLabel.setVisible(true); else statusLabel.setVisible(false); + } + //实现“查看帮助”功能 + else if(e.getSource()==helpM_CheckHelp) { + JTable table; + JFrame frame=new JFrame("查看帮助"); + Object name[]= {"功能","快捷键"}; + Object a[][]= {{"文件","ALT+F"},{"新建","CTRL+N"},{"新建窗口","CTRL+Shift+N"},{"打开","CTRL+O"},{"保存","CTRL+S"}, + {"编辑","ALT+E"},{"撤消","CTRL+Z"},{"剪切","CTRL+X"},{"复制","CTRL+C"},{"粘贴","CTRL+V"},{"删除","DELETE"}, + {"格式","ALT+O"},{"自动换行","ALT+W"}, + {"查看","ALT+V"},{"状态栏","ALT+S"}, + {"帮助","ALT+H"},{"查看帮助","F1"}}; + table=new JTable(a,name); + table.setRowHeight(table.getRowHeight()+10); + frame.setSize(550,550); + frame.setLocation(100, 100); + frame.add(new JScrollPane(table),BorderLayout.CENTER); + frame.setVisible(true); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + } + //实现“关于记事本”功能 + else if(e.getSource()==helpM_AboutNotepad) { + } } } \ No newline at end of file -- Gitee From 2769fb0784f95355c42d900bf6c1c7e390dae183 Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Sat, 4 Jun 2022 22:11:34 +0800 Subject: [PATCH 20/25] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E2=80=9C=E5=85=B3?= =?UTF-8?q?=E4=BA=8E=E8=AE=B0=E4=BA=8B=E6=9C=AC=E2=80=9D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 416faae..18fd4f6 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -588,7 +588,7 @@ public class Notepad extends JFrame implements ActionListener{ {"帮助","ALT+H"},{"查看帮助","F1"}}; table=new JTable(a,name); table.setRowHeight(table.getRowHeight()+10); - frame.setSize(550,550); + frame.setSize(550,500); frame.setLocation(100, 100); frame.add(new JScrollPane(table),BorderLayout.CENTER); frame.setVisible(true); @@ -596,7 +596,11 @@ public class Notepad extends JFrame implements ActionListener{ } //实现“关于记事本”功能 else if(e.getSource()==helpM_AboutNotepad) { - + JOptionPane.showMessageDialog(this, + "编写者:程莹怡0403\n"+ + "编写时间:2022.05-2022.06\n"+ + "部分代码借鉴CSDN社区、《Java Swing图形开发界面与案例详解》以及《Java2实用教程》\n", + "关于记事本",JOptionPane.INFORMATION_MESSAGE); } } } \ No newline at end of file -- Gitee From c1a2df288b62ef8eeac296287b38dd9f0ac2858a Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Sun, 5 Jun 2022 23:41:00 +0800 Subject: [PATCH 21/25] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E2=80=9C=E6=92=A4?= =?UTF-8?q?=E9=94=80=E2=80=9D=E3=80=81=E2=80=9C=E5=89=AA=E5=88=87=E2=80=9D?= =?UTF-8?q?=E3=80=81=E2=80=9C=E5=A4=8D=E5=88=B6=E2=80=9D=E3=80=81=E2=80=9C?= =?UTF-8?q?=E7=B2=98=E8=B4=B4=E2=80=9D=E3=80=81=E2=80=9C=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E2=80=9D=E3=80=81=E2=80=9C=E5=85=A8=E9=80=89=E2=80=9D=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 140 ++++++++++++++++++++++++++++---- 1 file changed, 124 insertions(+), 16 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 18fd4f6..6f49481 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -2,24 +2,23 @@ package java2022spring; import javax.swing.*; import javax.swing.event.*; -import javax.swing.text.BadLocationException; -import javax.swing.undo.CannotUndoException; -import javax.swing.undo.UndoManager; - +import javax.swing.text.*; +import javax.swing.undo.*; import java.awt.*; +import java.awt.datatransfer.*; import java.awt.event.*; import java.io.*; -import java.text.SimpleDateFormat; +import java.text.*; -public class Notepad extends JFrame implements ActionListener{ +public class Notepad extends JFrame implements ActionListener,DocumentListener{ //菜单条 JMenuBar menuBar; //菜单 JMenu fileMenu,editMenu,formatMenu,viewMenu,helpMenu; //“文件”的菜单项新建、新建窗口、打开、保存、另存为和退出功能 JMenuItem fileM_New,fileM_NewWindow,fileM_Open,fileM_Save,fileM_SaveAs,fileM_Exit; - //“编辑”的菜单项撤消、剪切、复制、粘贴和删除功能 - JMenuItem editM_Cancel,editM_Cut,editM_Copy,editM_Paste,editM_Delete; + //“编辑”的菜单项撤消、剪切、复制、粘贴、删除和全选功能 + JMenuItem editM_Cancel,editM_Cut,editM_Copy,editM_Paste,editM_Delete,editM_SelectAll; //“格式”的菜单项自动换行功能 JCheckBoxMenuItem formatM_LineFeed; //“查看”的菜单项状态栏功能 @@ -32,7 +31,13 @@ public class Notepad extends JFrame implements ActionListener{ JLabel statusLabel; //右键弹出菜单项 JPopupMenu popupMenu; - JMenuItem popupM_Cancel,popupM_Cut,popupM_Copy,popupM_Paste,popupM_Delete; + JMenuItem popupM_Cancel,popupM_Cut,popupM_Copy,popupM_Paste,popupM_Delete,popupM_SelectAll; + //系统剪切板 + Toolkit toolkit=Toolkit.getDefaultToolkit(); + Clipboard clipBoard=toolkit.getSystemClipboard(); + //创建撤销操作管理器(与撤销操作有关) + protected UndoManager undo=new UndoManager(); + protected UndoableEditListener undoHandler=new UndoHandler(); //其他变量 String oldValue;//存放当前编辑区文本 boolean isNewFile=true; @@ -72,6 +77,19 @@ public class Notepad extends JFrame implements ActionListener{ editMenu=new JMenu("编辑(E)"); editMenu.setMnemonic('E');//设置快捷键ALT+E + //选择“编辑”菜单时,设置其菜单项功能的可用性 + editMenu.addMenuListener(new MenuListener() + { public void menuCanceled(MenuEvent e)//取消菜单时调用 + { menuItemEnabled(); + } + public void menuDeselected(MenuEvent e)//取消选择某个菜单时调用 + { menuItemEnabled(); + } + public void menuSelected(MenuEvent e)//选择某个菜单时调用 + { menuItemEnabled(); + } + }); + editM_Cancel=new JMenuItem("撤销(U)"); editM_Cancel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,ActionEvent.CTRL_MASK));//设置快捷键CTRL+Z editM_Cancel.addActionListener(this); @@ -92,6 +110,10 @@ public class Notepad extends JFrame implements ActionListener{ editM_Delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));//设置快捷键DELETE editM_Delete.addActionListener(this); + editM_SelectAll = new JMenuItem("全选",'A'); + editM_SelectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); // + editM_SelectAll.addActionListener(this); + //创建格式菜单及菜单项并注册事件监听 formatMenu=new JMenu("格式(O)"); formatMenu.setMnemonic('O');//设置快捷键ALT+O @@ -139,6 +161,8 @@ public class Notepad extends JFrame implements ActionListener{ editMenu.add(editM_Copy); editMenu.add(editM_Paste); editMenu.add(editM_Delete); + editMenu.addSeparator(); + editMenu.add(editM_SelectAll); //向菜单条添加“格式”菜单及菜单项 menuBar.add(formatMenu); @@ -173,6 +197,9 @@ public class Notepad extends JFrame implements ActionListener{ popupM_Delete=new JMenuItem("删除(D)"); popupM_Delete.addActionListener(this); + popupM_SelectAll=new JMenuItem("全选(A)"); + popupM_SelectAll.addActionListener(this); + //向右键菜单添加菜单项和分隔符 popupMenu.add(popupM_Cancel); popupMenu.addSeparator(); @@ -180,6 +207,8 @@ public class Notepad extends JFrame implements ActionListener{ popupMenu.add(popupM_Copy); popupMenu.add(popupM_Paste); popupMenu.add(popupM_Delete); + popupMenu.addSeparator(); + popupMenu.add(popupM_SelectAll); //创建文本区域并添加滚动条 text=new JTextArea(20,50); @@ -188,6 +217,10 @@ public class Notepad extends JFrame implements ActionListener{ text.setLineWrap(true);//设置自动换行 oldValue=text.getText(); + //编辑区注册事件监听 + text.getDocument().addUndoableEditListener(undoHandler); + text.getDocument().addDocumentListener(this); + //向文本区域添加右键鼠标事件 text.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) @@ -249,7 +282,6 @@ public class Notepad extends JFrame implements ActionListener{ ex.printStackTrace(); } } - else { try { BufferedWriter writer=new BufferedWriter(new FileWriter(currentFile)); @@ -275,6 +307,36 @@ public class Notepad extends JFrame implements ActionListener{ } } + //设置“编辑”菜单下的菜单项的可用性 + public void menuItemEnabled() { + String selectText=text.getSelectedText(); + if(selectText==null) { + editM_Cut.setEnabled(false); + popupM_Cut.setEnabled(false); + editM_Copy.setEnabled(false); + popupM_Copy.setEnabled(false); + editM_Delete.setEnabled(false); + popupM_Delete.setEnabled(false); + } + else { + editM_Cut.setEnabled(true); + popupM_Cut.setEnabled(true); + editM_Copy.setEnabled(true); + popupM_Copy.setEnabled(true); + editM_Delete.setEnabled(true); + popupM_Delete.setEnabled(true); + } + Transferable contents=clipBoard.getContents(this); + if(contents==null) + { editM_Paste.setEnabled(false); + popupM_Paste.setEnabled(false); + } + else + { editM_Paste.setEnabled(true); + popupM_Paste.setEnabled(true); + } + } + @Override public void actionPerformed(ActionEvent e) { //实现“新建”功能 @@ -544,24 +606,53 @@ public class Notepad extends JFrame implements ActionListener{ } //实现“撤销”功能 else if(e.getSource()==editM_Cancel||e.getSource()==popupM_Cancel) { - + if(undo.canUndo()) { + try { + undo.undo(); + } + catch(CannotUndoException ex) { + ex.printStackTrace(); + } + } + if(!undo.canUndo()) { + editM_Cancel.setEnabled(false); + } } //实现“剪切”功能 else if(e.getSource()==editM_Cut||e.getSource()==popupM_Cut) { - + String t=text.getSelectedText(); + StringSelection selection=new StringSelection(t); + clipBoard.setContents(selection, null); + text.replaceRange("", text.getSelectionStart(), text.getSelectionEnd()); } //实现“复制”功能 else if(e.getSource()==editM_Copy||e.getSource()==popupM_Copy) { - + String t=text.getSelectedText(); + StringSelection selection=new StringSelection(t); + clipBoard.setContents(selection, null); } //实现“粘贴”功能 else if(e.getSource()==editM_Paste||e.getSource()==popupM_Paste) { - + Transferable contents=clipBoard.getContents(this); + if(contents==null) { + return; + } + String t=""; + try { + t=(String)contents.getTransferData(DataFlavor.stringFlavor); + } + catch(Exception ex) { + } + text.replaceRange(t, text.getSelectionStart(),text.getSelectionEnd()); } //实现“删除”功能 else if(e.getSource()==editM_Delete||e.getSource()==popupM_Delete) { - + text.replaceRange("", text.getSelectionStart(), text.getSelectionEnd()); } + //实现“全选”功能 + else if(e.getSource()==editM_SelectAll || e.getSource()==popupM_SelectAll) { + text.selectAll(); + } //实现“自动换行”功能 else if(e.getSource()==formatM_LineFeed) { if(formatM_LineFeed.getState()) @@ -599,8 +690,25 @@ public class Notepad extends JFrame implements ActionListener{ JOptionPane.showMessageDialog(this, "编写者:程莹怡0403\n"+ "编写时间:2022.05-2022.06\n"+ - "部分代码借鉴CSDN社区、《Java Swing图形开发界面与案例详解》以及《Java2实用教程》\n", + "部分代码方法参考CSDN社区、《Java Swing图形开发界面与案例详解》以及《Java2实用教程》\n", "关于记事本",JOptionPane.INFORMATION_MESSAGE); } } + + //实现DocumentListener接口中的方法 + public void insertUpdate(DocumentEvent e) { + text.setEnabled(true); + } + public void removeUpdate(DocumentEvent e) { + text.setEnabled(true); + } + public void changedUpdate(DocumentEvent e) { + text.setEnabled(true); + } + + class UndoHandler implements UndoableEditListener + { public void undoableEditHappened(UndoableEditEvent ue) + { undo.addEdit(ue.getEdit()); + } + } } \ No newline at end of file -- Gitee From 38557b2cab68b0a03455e3ac5251a1b8c5fe80ec Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Mon, 6 Jun 2022 22:20:24 +0800 Subject: [PATCH 22/25] =?UTF-8?q?=E5=AE=8C=E5=96=84=E2=80=9C=E6=92=A4?= =?UTF-8?q?=E9=94=80=E2=80=9D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 43 ++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 6f49481..eefaa52 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -93,6 +93,7 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ editM_Cancel=new JMenuItem("撤销(U)"); editM_Cancel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,ActionEvent.CTRL_MASK));//设置快捷键CTRL+Z editM_Cancel.addActionListener(this); + editM_Cancel.setEnabled(false); editM_Cut=new JMenuItem("剪切(T)"); editM_Cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));//设置快捷键CTRL+X @@ -184,6 +185,7 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ popupM_Cancel=new JMenuItem("撤销(U)"); popupM_Cancel.addActionListener(this); + popupM_Cancel.setEnabled(false); popupM_Cut=new JMenuItem("剪切(T)"); popupM_Cut.addActionListener(this); @@ -210,6 +212,19 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ popupMenu.addSeparator(); popupMenu.add(popupM_SelectAll); + //单击右键弹出快捷菜单时,设置其菜单项功能的可用性 + popupMenu.addPopupMenuListener(new PopupMenuListener(){ + public void popupMenuWillBecomeVisible(PopupMenuEvent e) { + menuItemEnabled(); + } + public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { + menuItemEnabled(); + } + public void popupMenuCanceled(PopupMenuEvent e) { + menuItemEnabled(); + } + }); + //创建文本区域并添加滚动条 text=new JTextArea(20,50); JScrollPane scroller=new JScrollPane(text); @@ -362,6 +377,8 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ writer.close(); this.setTitle("无标题—记事本"); text.setText(null); + editM_Cancel.setEnabled(false); + popupM_Cancel.setEnabled(false); oldValue=text.getText(); currentFile=path; } @@ -376,6 +393,8 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ writer.flush(); writer.close(); isNewFile=true; + editM_Cancel.setEnabled(false); + popupM_Cancel.setEnabled(false); } catch(Exception ex) { @@ -387,6 +406,8 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ text.setText(null); isNewFile=true; oldValue=text.getText(); + editM_Cancel.setEnabled(false); + popupM_Cancel.setEnabled(false); } else if(whetherSave==JOptionPane.CANCEL_OPTION) { return; @@ -397,6 +418,8 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ text.setText(null); isNewFile=true; oldValue=text.getText(); + editM_Cancel.setEnabled(false); + popupM_Cancel.setEnabled(false); } } //实现“新建窗口”功能 @@ -443,6 +466,8 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); statusLabel.setText(" 当前打开文件:"+currentFile+" | "+"上次修改时间:"+sdf.format(file.lastModified())); oldValue=text.getText(); + editM_Cancel.setEnabled(false); + popupM_Cancel.setEnabled(false); } catch(Exception ex) { ex.printStackTrace(); @@ -478,6 +503,8 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); statusLabel.setText(" 当前打开文件:"+currentFile+" | "+"上次修改时间:"+sdf.format(file.lastModified())); oldValue=text.getText(); + editM_Cancel.setEnabled(false); + popupM_Cancel.setEnabled(false); } catch(Exception ex) { ex.printStackTrace(); @@ -508,6 +535,8 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); statusLabel.setText(" 当前打开文件:"+currentFile+" | "+"上次修改时间:"+sdf.format(file.lastModified())); oldValue=text.getText(); + editM_Cancel.setEnabled(false); + popupM_Cancel.setEnabled(false); } catch(Exception ex) { ex.printStackTrace(); @@ -536,7 +565,9 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ File file=new File(currentFile); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); statusLabel.setText(" 当前打开文件:"+currentFile+" | "+"上次修改时间:"+sdf.format(file.lastModified())); - oldValue=text.getText(); + oldValue=text.getText(); + editM_Cancel.setEnabled(false); + popupM_Cancel.setEnabled(false); } catch(Exception ex) { ex.printStackTrace(); @@ -616,6 +647,7 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ } if(!undo.canUndo()) { editM_Cancel.setEnabled(false); + popupM_Cancel.setEnabled(false); } } //实现“剪切”功能 @@ -697,13 +729,16 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ //实现DocumentListener接口中的方法 public void insertUpdate(DocumentEvent e) { - text.setEnabled(true); + editM_Cancel.setEnabled(true); + popupM_Cancel.setEnabled(true); } public void removeUpdate(DocumentEvent e) { - text.setEnabled(true); + editM_Cancel.setEnabled(true); + popupM_Cancel.setEnabled(true); } public void changedUpdate(DocumentEvent e) { - text.setEnabled(true); + editM_Cancel.setEnabled(true); + popupM_Cancel.setEnabled(true); } class UndoHandler implements UndoableEditListener -- Gitee From 3d220adce06524556907ba583a4e041d117966b4 Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Tue, 7 Jun 2022 13:54:12 +0800 Subject: [PATCH 23/25] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E2=80=9C=E7=BC=A9?= =?UTF-8?q?=E6=94=BE=E2=80=9D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 54 +++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index eefaa52..25abbeb 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -23,6 +23,8 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ JCheckBoxMenuItem formatM_LineFeed; //“查看”的菜单项状态栏功能 JCheckBoxMenuItem viewM_Status; + JMenu viewM_Zoom; + JMenuItem viewM_ZoomIn,viewM_ZoomOut; //“帮助”的菜单项查看帮助和关于记事本功能 JMenuItem helpM_CheckHelp,helpM_AboutNotepad; //“文本”编辑区 @@ -42,6 +44,8 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ String oldValue;//存放当前编辑区文本 boolean isNewFile=true; String currentFile; + JScrollPane scroller; + MouseWheelListener sysWheel; public Notepad() { setTitle("记事本"); @@ -133,6 +137,16 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ viewM_Status.setState(true); viewM_Status.addActionListener(this); + viewM_Zoom=new JMenu("缩放"); + + viewM_ZoomIn=new JMenuItem("放大"); + viewM_ZoomIn.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ADD,ActionEvent.CTRL_MASK));//设置快捷键CTRL++ + viewM_ZoomIn.addActionListener(this); + + viewM_ZoomOut=new JMenuItem("缩小"); + viewM_ZoomOut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS,ActionEvent.CTRL_MASK));//设置快捷键CTR+- + viewM_ZoomOut.addActionListener(this); + //创建帮助菜单及菜单项并注册事件监听 helpMenu=new JMenu("帮助(H)"); helpMenu.setMnemonic('H');//设置快捷键ALT+H @@ -171,7 +185,10 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ //向菜单条添加“查看”菜单及菜单项 menuBar.add(viewMenu); + viewMenu.add(viewM_Zoom); viewMenu.add(viewM_Status); + viewM_Zoom.add(viewM_ZoomIn); + viewM_Zoom.add(viewM_ZoomOut); //向菜单条添加“帮助”菜单及菜单项 menuBar.add(helpMenu); @@ -227,10 +244,13 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ //创建文本区域并添加滚动条 text=new JTextArea(20,50); - JScrollPane scroller=new JScrollPane(text); + scroller=new JScrollPane(text); this.add(scroller,BorderLayout.CENTER); text.setLineWrap(true);//设置自动换行 oldValue=text.getText(); + sysWheel = scroller.getMouseWheelListeners()[0];//得到系统滚动事件 + scroller.removeMouseWheelListener(sysWheel);//移除系统滚动,需要时添加 + scroller.addMouseWheelListener(new scrollers()); //编辑区注册事件监听 text.getDocument().addUndoableEditListener(undoHandler); @@ -692,6 +712,16 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ else text.setLineWrap(false); } + //实现“放大”功能 + else if(e.getSource()==viewM_ZoomIn) { + Font f=text.getFont(); + text.setFont(new Font(f.getFamily(),f.getStyle(),f.getSize()+1)); + } + //实现“缩小”功能 + else if(e.getSource()==viewM_ZoomOut) { + Font f=text.getFont(); + text.setFont(new Font(f.getFamily(),f.getStyle(),f.getSize()-1)); + } //实现“状态栏”功能 else if(e.getSource()==viewM_Status) { if(viewM_Status.getState()) @@ -727,6 +757,26 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ } } + //实现滚动缩放方法 + public class scrollers extends MouseAdapter{ + public void mouseWheelMoved(MouseWheelEvent e) { + if(e.isControlDown()) { + Font f = text.getFont(); + if(e.getWheelRotation()<0) { + text.setFont(new Font(f.getFamily(),f.getStyle(),f.getSize()+1)); + } + else if(e.getWheelRotation()>0) { + text.setFont(new Font(f.getFamily(),f.getStyle(),f.getSize()-1)); + } + } + else { + scroller.addMouseWheelListener(sysWheel); + sysWheel.mouseWheelMoved(e); + scroller.removeMouseWheelListener(sysWheel); + } + } + } + //实现DocumentListener接口中的方法 public void insertUpdate(DocumentEvent e) { editM_Cancel.setEnabled(true); @@ -745,5 +795,5 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener{ { public void undoableEditHappened(UndoableEditEvent ue) { undo.addEdit(ue.getEdit()); } - } + } } \ No newline at end of file -- Gitee From d3859f26d2fe8e8b115dc794b426a464088e2449 Mon Sep 17 00:00:00 2001 From: chengyingyi <1196922704@qq.com> Date: Tue, 7 Jun 2022 22:14:25 +0800 Subject: [PATCH 24/25] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E2=80=9C=E5=AD=97?= =?UTF-8?q?=E4=BD=93=E2=80=9D=E5=8A=9F=E8=83=BD=EF=BC=8C=E4=B8=BA=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=A0=8F=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96=E5=85=89?= =?UTF-8?q?=E6=A0=87=E6=89=80=E5=9C=A8=E8=A1=8C=E5=88=97=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/FontDialog.java | 101 ++++++++++++++++++++++++ src/java2022spring/FontFamilyNames.java | 10 +++ src/java2022spring/Notepad.java | 62 ++++++++++++--- 3 files changed, 163 insertions(+), 10 deletions(-) create mode 100644 src/java2022spring/FontDialog.java create mode 100644 src/java2022spring/FontFamilyNames.java diff --git a/src/java2022spring/FontDialog.java b/src/java2022spring/FontDialog.java new file mode 100644 index 0000000..65cf8e1 --- /dev/null +++ b/src/java2022spring/FontDialog.java @@ -0,0 +1,101 @@ +package java2022spring; +import java.awt.event.*; +import java.awt.*; +import javax.swing.*; +public class FontDialog extends JDialog implements ItemListener,ActionListener{ + FontFamilyNames fontFamilyNames; + int fontSize=12; + int fontStyle=Font.PLAIN; + String fontName; + JComboBox fontNameList,fontSizeList,fontStyleList; + JLabel label; + Font font; + JButton yes,cancel; + static int YES=1,NO=0; + int state=-1; + FontDialog(JFrame f){ + super(f); + setTitle("字体对话框"); + font=new Font("宋体",Font.PLAIN,12); + fontFamilyNames=new FontFamilyNames(); + setModal(true); + yes=new JButton("确定"); + cancel=new JButton("取消"); + yes.addActionListener(this); + cancel.addActionListener(this); + label=new JLabel("记事本-Notepad",JLabel.CENTER); + fontNameList=new JComboBox(); + fontSizeList=new JComboBox(); + fontStyleList=new JComboBox(); + String name[]=fontFamilyNames.getFontName(); + fontNameList.addItem("字体"); + for(int k=0;k Date: Wed, 8 Jun 2022 22:44:44 +0800 Subject: [PATCH 25/25] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=81=97=E6=BC=8F?= =?UTF-8?q?=E7=9A=84=E5=90=84=E4=B8=AA=E5=9C=B0=E6=96=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/FontDialog.java | 2 +- src/java2022spring/Notepad.java | 92 +++++++++++++++++++----------- 2 files changed, 61 insertions(+), 33 deletions(-) diff --git a/src/java2022spring/FontDialog.java b/src/java2022spring/FontDialog.java index 65cf8e1..aa50886 100644 --- a/src/java2022spring/FontDialog.java +++ b/src/java2022spring/FontDialog.java @@ -15,7 +15,7 @@ public class FontDialog extends JDialog implements ItemListener,ActionListener{ int state=-1; FontDialog(JFrame f){ super(f); - setTitle("字体对话框"); + setTitle("字体设置"); font=new Font("宋体",Font.PLAIN,12); fontFamilyNames=new FontFamilyNames(); setModal(true); diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 7dc069b..3299999 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -281,11 +281,11 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener, //添加状态栏 statusLabel=new JLabel("按F1获取帮助"); - lcLabel=new JLabel("第"+line+"行"+"第"+col+"列"); + lcLabel=new JLabel(" | 第"+line+"行"+"第"+col+"列"); panel=new JPanel(); + panel.add(statusLabel,BorderLayout.EAST); + panel.add(lcLabel,BorderLayout.WEST); this.add(panel,BorderLayout.SOUTH); - panel.add(statusLabel); - panel.add(lcLabel); //向窗口添加菜单条 this.setJMenuBar(menuBar); @@ -308,7 +308,7 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener, try { line=text.getLineOfOffset(c)+1; col=c-text.getLineStartOffset(line-1)+1; - lcLabel.setText("第"+line+"行"+"第"+col+"列"); + lcLabel.setText(" | 第"+line+"行"+"第"+col+"列"); } catch (BadLocationException e1) { e1.printStackTrace(); @@ -338,8 +338,12 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener, setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.dispose(); } - catch(Exception ex) { - ex.printStackTrace(); + catch(IOException ex) { + JOptionPane.showMessageDialog(this, + "保存失败", + "保存失败",JOptionPane.ERROR_MESSAGE); + Notepad notepad=new Notepad(); + text.setText(currentValue); } } else { @@ -351,7 +355,10 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener, setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.dispose(); } - catch(Exception ex) { + catch(IOException ex) { + JOptionPane.showMessageDialog(this, + "保存失败", + "保存失败",JOptionPane.ERROR_MESSAGE); } } @@ -430,8 +437,10 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener, oldValue=text.getText(); currentFile=path; } - catch(Exception ex) { - ex.printStackTrace(); + catch(IOException ex) { + JOptionPane.showMessageDialog(this, + "保存失败", + "保存失败",JOptionPane.ERROR_MESSAGE); } } else { @@ -444,8 +453,10 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener, editM_Cancel.setEnabled(false); popupM_Cancel.setEnabled(false); } - catch(Exception ex) { - + catch(IOException ex) { + JOptionPane.showMessageDialog(this, + "保存失败", + "保存失败",JOptionPane.ERROR_MESSAGE); } } } @@ -517,13 +528,17 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener, editM_Cancel.setEnabled(false); popupM_Cancel.setEnabled(false); } - catch(Exception ex) { - ex.printStackTrace(); + catch(IOException ex) { + JOptionPane.showMessageDialog(this, + "打开失败", + "打开失败",JOptionPane.ERROR_MESSAGE); } } } - catch(Exception ex) { - ex.printStackTrace(); + catch(IOException ex) { + JOptionPane.showMessageDialog(this, + "保存失败", + "保存失败",JOptionPane.ERROR_MESSAGE); } } @@ -554,13 +569,17 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener, editM_Cancel.setEnabled(false); popupM_Cancel.setEnabled(false); } - catch(Exception ex) { - ex.printStackTrace(); + catch(IOException ex) { + JOptionPane.showMessageDialog(this, + "打开失败", + "打开失败",JOptionPane.ERROR_MESSAGE); } } } - catch(Exception ex) { - + catch(IOException ex) { + JOptionPane.showMessageDialog(this, + "保存失败", + "保存失败",JOptionPane.ERROR_MESSAGE); } } } @@ -586,8 +605,10 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener, editM_Cancel.setEnabled(false); popupM_Cancel.setEnabled(false); } - catch(Exception ex) { - ex.printStackTrace(); + catch(IOException ex) { + JOptionPane.showMessageDialog(this, + "打开失败", + "打开失败",JOptionPane.ERROR_MESSAGE); } } } @@ -617,8 +638,10 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener, editM_Cancel.setEnabled(false); popupM_Cancel.setEnabled(false); } - catch(Exception ex) { - ex.printStackTrace(); + catch(IOException ex) { + JOptionPane.showMessageDialog(this, + "打开失败", + "打开失败",JOptionPane.ERROR_MESSAGE); } } } @@ -641,8 +664,10 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener, statusLabel.setText(" 当前打开文件:"+currentFile+" | "+"上次修改时间:"+sdf.format(file.lastModified())); oldValue=text.getText(); } - catch(Exception ex) { - ex.printStackTrace(); + catch(IOException ex) { + JOptionPane.showMessageDialog(this, + "保存失败", + "保存失败",JOptionPane.ERROR_MESSAGE); } } else { @@ -656,8 +681,10 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener, statusLabel.setText(" 当前打开文件:"+currentFile+" | "+"上次修改时间:"+sdf.format(file.lastModified())); oldValue=text.getText(); } - catch(Exception ex) { - + catch(IOException ex) { + JOptionPane.showMessageDialog(this, + "保存失败", + "保存失败",JOptionPane.ERROR_MESSAGE); } } } @@ -675,22 +702,23 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener, currentFile=path; oldValue=text.getText(); } - catch(Exception ex) { - ex.printStackTrace(); + catch(IOException ex) { + JOptionPane.showMessageDialog(this, + "保存失败", + "保存失败",JOptionPane.ERROR_MESSAGE); } } //实现“退出”功能 else if(e.getSource()==fileM_Exit) { exitWindowChoose(); } - //实现“撤销”功能(部分参考CSDN社区) + //实现“撤消”功能(部分参考CSDN社区) else if(e.getSource()==editM_Cancel||e.getSource()==popupM_Cancel) { if(undo.canUndo()) { try { undo.undo(); } catch(CannotUndoException ex) { - ex.printStackTrace(); } } if(!undo.canUndo()) { @@ -721,7 +749,7 @@ public class Notepad extends JFrame implements ActionListener,DocumentListener, try { t=(String)contents.getTransferData(DataFlavor.stringFlavor); } - catch(Exception ex) { + catch(IOException | UnsupportedFlavorException ex) { } text.replaceRange(t, text.getSelectionStart(),text.getSelectionEnd()); } -- Gitee