From b5dbee5c29e51cb4fc1a6a40f0be1f4c75272ba2 Mon Sep 17 00:00:00 2001 From: yuxiaoshuo Date: Mon, 16 May 2022 21:46:50 +0800 Subject: [PATCH 01/16] =?UTF-8?q?=E6=90=AD=E5=BB=BA=E4=BA=86=E8=AE=B0?= =?UTF-8?q?=E4=BA=8B=E6=9C=AC=E7=9A=84=E5=9F=BA=E6=9C=AC=E7=AA=97=E4=BD=93?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E5=86=99=E4=BA=86=E6=89=93=E5=BC=80=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E7=9B=91=E5=90=AC=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 126 ++++++++++++++++++++++++++++++++ src/java2022spring/Test.java | 7 -- 2 files changed, 126 insertions(+), 7 deletions(-) create mode 100644 src/java2022spring/Notepad.java delete mode 100644 src/java2022spring/Test.java diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java new file mode 100644 index 0000000..d2878bb --- /dev/null +++ b/src/java2022spring/Notepad.java @@ -0,0 +1,126 @@ +package java2022spring; +import java.awt.*; +import java.io.*; +import javax.swing.*; +import java.awt.event.*; +public class Notepad implements ActionListener { + JFrame frame=new JFrame("文本编辑器"); + JTextArea textarea=new JTextArea(40,40); + JScrollPane pane=new JScrollPane(textarea); + JMenuBar menubar; + JMenu menu1,menu2,menu3,menu4,menu5; + JMenuItem item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,item11,item12,item13,item14; + JFileChooser filechooser; + public Notepad() { + //实例化 + menubar=new JMenuBar(); + menu1=new JMenu("文件(F)"); + menu2=new JMenu("编辑(E)"); + menu3=new JMenu("格式(O)"); + menu4=new JMenu("查看(V)"); + menu5=new JMenu("帮助(H)"); + item1=new JMenuItem("新建(N)"); + item2=new JMenuItem("打开(O)"); + item3=new JMenuItem("保存(S)"); + item4=new JMenuItem("另存为(A)"); + item5=new JMenuItem("退出(X)"); + item6=new JMenuItem("撤消(U)"); + item7=new JMenuItem("剪切(T)"); + item8=new JMenuItem("复制(C)"); + item9=new JMenuItem("粘贴(V)"); + item10=new JMenuItem("删除(L)"); + item11=new JMenuItem("自动换行(W)"); + item12=new JMenuItem("状态栏(S)"); + item13=new JMenuItem("查看帮助(H)"); + item14=new JMenuItem("关于记事本(A)"); + + //组建 + menu1.add(item1); + menu1.add(item2); + menu1.add(item3); + menu1.add(item4); + menu1.add(item5); + menu2.add(item6); + menu2.add(item7); + menu2.add(item8); + menu2.add(item9); + menu2.add(item10); + menu3.add(item11); + menu4.add(item12); + menu5.add(item13); + menu5.add(item14); + menubar.add(menu1); + menubar.add(menu2); + menubar.add(menu3); + menubar.add(menu4); + menubar.add(menu5); + frame.setJMenuBar(menubar); + frame.add(pane); + + //窗口基本参数 + frame.setBounds(20,30,900,550); + frame.setLocationRelativeTo(null);//设置窗口居中显示 + frame.setVisible(true); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + textarea.setFont(new Font("宋体",Font.PLAIN,20)); + textarea.setLineWrap(true);//自动换行 + menubar.setBackground(Color.white); + + //设置快捷键 + item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK)); + item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK)); + item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK)); + item4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,ActionEvent.CTRL_MASK)); + item6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,ActionEvent.CTRL_MASK)); + item7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK)); + item8.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK)); + item9.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK)); + item10.setMnemonic(KeyEvent.VK_DELETE); + + //添加监视器 + add(); + } + + public void add() { + item2.addActionListener(this); + item2.setActionCommand("打开(O)"); + } + + public void actionPerformed(ActionEvent e) { + String content=e.getActionCommand(); + if(content.equals("打开(O)")) { + filechooser=new JFileChooser(); + filechooser.setDialogTitle("请选择文件"); + filechooser.showOpenDialog(null); + filechooser.setVisible(true); + File file=filechooser.getSelectedFile(); + BufferedReader bu=null; + try { + FileReader reader=new FileReader(file); + bu=new BufferedReader(reader); + String readcontent=""; + String allcode=""; + while((readcontent=bu.readLine())!=null) { + allcode+= readcontent+"\r\n"; + } + textarea.setText(allcode); + } + catch(Exception e2){ + e2.printStackTrace(); + } + finally { + try { + bu.close(); + } + catch(IOException e1) { + e1.printStackTrace(); + } + } + } + } + + + public static void main(String[] args) { + Notepad window=new Notepad(); + } +} diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java deleted file mode 100644 index 24deb29..0000000 --- a/src/java2022spring/Test.java +++ /dev/null @@ -1,7 +0,0 @@ -package java2022spring; - -public class Test { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} -- Gitee From a889f10ca0ee3c75c551960aff1fc59bc0285ae0 Mon Sep 17 00:00:00 2001 From: yuxiaoshuo Date: Tue, 17 May 2022 22:12:38 +0800 Subject: [PATCH 02/16] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E4=BA=86"=E6=89=93=E5=BC=80""=E6=96=B0=E5=BB=BA""=E4=BF=9D?= =?UTF-8?q?=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 | 193 +++++++++++++++++++++++++++----- 1 file changed, 167 insertions(+), 26 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index d2878bb..dbfa840 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -2,15 +2,21 @@ package java2022spring; import java.awt.*; import java.io.*; import javax.swing.*; +import javax.swing.filechooser.FileFilter; + import java.awt.event.*; public class Notepad implements ActionListener { - JFrame frame=new JFrame("文本编辑器"); - JTextArea textarea=new JTextArea(40,40); - JScrollPane pane=new JScrollPane(textarea); - JMenuBar menubar; - JMenu menu1,menu2,menu3,menu4,menu5; - JMenuItem item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,item11,item12,item13,item14; - JFileChooser filechooser; + private JFrame frame=new JFrame("文本编辑器"); + private JTextArea textarea=new JTextArea(40,40); + private JScrollPane pane=new JScrollPane(textarea); + private JMenuBar menubar; + private JMenu menu1,menu2,menu3,menu4,menu5; + private JMenuItem item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,item11,item12,item13,item14; + private JFileChooser filechooser; + private boolean flag = true;// 需要一个flag变量,存储当前文件是否存在源文件:true,有;false,没有 + private String txt = ""; + private File FILE; + private JLabel labelTips; public Notepad() { //实例化 menubar=new JMenuBar(); @@ -39,8 +45,10 @@ public class Notepad implements ActionListener { menu1.add(item2); menu1.add(item3); menu1.add(item4); + menu1.addSeparator();//添加分割线 menu1.add(item5); menu2.add(item6); + menu2.addSeparator(); menu2.add(item7); menu2.add(item8); menu2.add(item9); @@ -48,6 +56,7 @@ public class Notepad implements ActionListener { menu3.add(item11); menu4.add(item12); menu5.add(item13); + menu5.addSeparator(); menu5.add(item14); menubar.add(menu1); menubar.add(menu2); @@ -66,29 +75,90 @@ public class Notepad implements ActionListener { textarea.setLineWrap(true);//自动换行 menubar.setBackground(Color.white); - //设置快捷键 - item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK)); - item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK)); - item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK)); - item4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,ActionEvent.CTRL_MASK)); - item6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,ActionEvent.CTRL_MASK)); - item7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK)); - item8.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK)); - item9.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK)); - item10.setMnemonic(KeyEvent.VK_DELETE); - - //添加监视器 add(); } + public void add() { + //添加监视器 + item1.addActionListener(this); item2.addActionListener(this); - item2.setActionCommand("打开(O)"); + item3.addActionListener(this); + item4.addActionListener(this); + item5.addActionListener(this); + item6.addActionListener(this); + item7.addActionListener(this); + item8.addActionListener(this); + item9.addActionListener(this); + item10.addActionListener(this); + item11.addActionListener(this); + item12.addActionListener(this); + item13.addActionListener(this); + item14.addActionListener(this); + + //设置快捷键 + menu1.setMnemonic('F'); //设置快捷键ALT+F + menu2.setMnemonic('E'); + menu3.setMnemonic('O'); + menu4.setMnemonic('V'); + menu5.setMnemonic('H'); + item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,2)); //设置快捷键CTRL+N + item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,2)); + item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,2)); + item4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,3)); //设置快捷键CTRL+SHIFT+N + item6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,2)); + item7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,2)); + item8.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,2)); + item9.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,2)); + item10.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); //设置快捷键DELETE } public void actionPerformed(ActionEvent e) { - String content=e.getActionCommand(); - if(content.equals("打开(O)")) { + String command=e.getActionCommand(); + //"新建"功能 + if(command.equals("新建(N)")) { + String content = new String(textarea.getText()); + if (flag) { // 如果有源文件 + if (txt.equals(content)) { // 判断文本是否有改动(此为无改动) + flag=false; + NewFile(); + } + else { // 有改动 + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", + JOptionPane.YES_NO_CANCEL_OPTION);//添加确认提示框 + if (i == JOptionPane.OK_OPTION) { + flag=false; + save(); + NewFile(); + } + else if (i == JOptionPane.NO_OPTION) { + flag=false; + NewFile(); + } + } + } + else { // 无源文件 + if (txt.equals(content)) { // 判断文本是否有改动 + flag=false; + NewFile(); + } + else { // 有改动 + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", + JOptionPane.YES_NO_CANCEL_OPTION); + if (i == JOptionPane.OK_OPTION) { + flag=false; + save(); + NewFile(); + } + else if (i == JOptionPane.NO_OPTION) { + flag=false; + NewFile(); + } + } + } + } + //"打开"功能 + else if(command.equals("打开(O)")) { filechooser=new JFileChooser(); filechooser.setDialogTitle("请选择文件"); filechooser.showOpenDialog(null); @@ -105,22 +175,93 @@ public class Notepad implements ActionListener { } textarea.setText(allcode); } - catch(Exception e2){ - e2.printStackTrace(); + catch(Exception e1){ + e1.printStackTrace(); } finally { try { bu.close(); } - catch(IOException e1) { - e1.printStackTrace(); + catch(IOException e2) { + e2.printStackTrace(); } } } + + //"保存"功能 + else if(command.equals("保存(S)")) { + save(); + } + + } + + // 新建窗口 + public void NewFile() { + frame.dispose(); + Notepad note = new Notepad(); } + //关闭窗口时 + public void Exit() { + String content = new String(textarea.getText()); + if (flag) { // 如果有源文件 + if (txt.equals(content)) { // 判断文本是否有改动(此为无改动) + System.exit(0); + } + else { // 有改动 + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", + JOptionPane.YES_NO_CANCEL_OPTION);//添加确认提示框 + if (i == JOptionPane.OK_OPTION) { + save(); + } + else if (i == JOptionPane.NO_OPTION) { + System.exit(0); + } + } + } + else { // 无源文件 + if (txt.equals(content)) { // 判断文本是否有改动 + System.exit(0); + } + else { // 有改动 + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", + JOptionPane.YES_NO_CANCEL_OPTION); + if (i == JOptionPane.OK_OPTION) { + save(); + } + else if (i == JOptionPane.NO_OPTION) { + System.exit(0); + } + } + } + } + + // 保存文件 + private void save() { + FileDialog fileDialog = new FileDialog(frame, "保存文件至", FileDialog.SAVE); + fileDialog.setVisible(true); //保存文件的对话框设置为可见 + String absPath = fileDialog.getDirectory() + fileDialog.getFile(); //获取保存的路径和设置的文件名 + try {//用来检测是否设置了保存路径 + BufferedWriter wr = new BufferedWriter(new FileWriter(absPath)); //设置输出文件名为保存的路径下面的文件名 + String s = textarea.getText(); //获取文本域字符串 + wr.write(s); //开始写入 + wr.close(); //写完关闭 + } + catch (IOException e) { + e.printStackTrace(); + } + } public static void main(String[] args) { Notepad window=new Notepad(); + //生成windows风格界面 (此代码为借鉴) + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException + | UnsupportedLookAndFeelException e) { + e.printStackTrace(); + } } } + + -- Gitee From e312b8919228bb0c0d7cffd51b9a11220c57e472 Mon Sep 17 00:00:00 2001 From: yuxiaoshuo Date: Wed, 18 May 2022 21:38:17 +0800 Subject: [PATCH 03/16] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86"=E5=8F=A6?= =?UTF-8?q?=E5=AD=98=E4=B8=BA"=E3=80=81"=E9=80=80=E5=87=BA"=E3=80=81"?= =?UTF-8?q?=E5=A4=8D=E5=88=B6"=E3=80=81"=E7=B2=98=E8=B4=B4"=E3=80=81"?= =?UTF-8?q?=E5=89=AA=E5=88=87"=E3=80=81"=E5=88=A0=E9=99=A4"=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E8=BF=9B=E4=B8=80=E6=AD=A5=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E4=BA=86"=E6=89=93=E5=BC=80"=E3=80=81"=E4=BF=9D=E5=AD=98"?= =?UTF-8?q?=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 | 242 +++++++++++++++++++++++++++----- 1 file changed, 205 insertions(+), 37 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index dbfa840..548a4f0 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -1,5 +1,9 @@ package java2022spring; import java.awt.*; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; import java.io.*; import javax.swing.*; import javax.swing.filechooser.FileFilter; @@ -9,14 +13,15 @@ public class Notepad implements ActionListener { private JFrame frame=new JFrame("文本编辑器"); private JTextArea textarea=new JTextArea(40,40); private JScrollPane pane=new JScrollPane(textarea); + private Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //调用系统剪贴板方法 private JMenuBar menubar; private JMenu menu1,menu2,menu3,menu4,menu5; - private JMenuItem item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,item11,item12,item13,item14; + private JMenuItem item,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,item11,item12,item13,item14; private JFileChooser filechooser; private boolean flag = true;// 需要一个flag变量,存储当前文件是否存在源文件:true,有;false,没有 private String txt = ""; private File FILE; - private JLabel labelTips; + private BufferedReader bu; public Notepad() { //实例化 menubar=new JMenuBar(); @@ -25,7 +30,8 @@ public class Notepad implements ActionListener { menu3=new JMenu("格式(O)"); menu4=new JMenu("查看(V)"); menu5=new JMenu("帮助(H)"); - item1=new JMenuItem("新建(N)"); + item=new JMenuItem("新建(N)"); + item1=new JMenuItem("新窗口(W)"); item2=new JMenuItem("打开(O)"); item3=new JMenuItem("保存(S)"); item4=new JMenuItem("另存为(A)"); @@ -33,7 +39,7 @@ public class Notepad implements ActionListener { item6=new JMenuItem("撤消(U)"); item7=new JMenuItem("剪切(T)"); item8=new JMenuItem("复制(C)"); - item9=new JMenuItem("粘贴(V)"); + item9=new JMenuItem("粘贴(P)"); item10=new JMenuItem("删除(L)"); item11=new JMenuItem("自动换行(W)"); item12=new JMenuItem("状态栏(S)"); @@ -41,6 +47,7 @@ public class Notepad implements ActionListener { item14=new JMenuItem("关于记事本(A)"); //组建 + menu1.add(item); menu1.add(item1); menu1.add(item2); menu1.add(item3); @@ -81,6 +88,7 @@ public class Notepad implements ActionListener { public void add() { //添加监视器 + item.addActionListener(this); item1.addActionListener(this); item2.addActionListener(this); item3.addActionListener(this); @@ -102,10 +110,11 @@ public class Notepad implements ActionListener { menu3.setMnemonic('O'); menu4.setMnemonic('V'); menu5.setMnemonic('H'); - item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,2)); //设置快捷键CTRL+N + item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,2)); //设置快捷键CTRL+N + item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,3)); //设置快捷键CTRL+SHIFT+N item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,2)); item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,2)); - item4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,3)); //设置快捷键CTRL+SHIFT+N + item4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,3)); item6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,2)); item7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,2)); item8.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,2)); @@ -115,6 +124,8 @@ public class Notepad implements ActionListener { public void actionPerformed(ActionEvent e) { String command=e.getActionCommand(); + + //"文件"菜单 //"新建"功能 if(command.equals("新建(N)")) { String content = new String(textarea.getText()); @@ -157,33 +168,51 @@ public class Notepad implements ActionListener { } } } + + //"新窗口"功能 + else if(command.equals("新窗口(W)")) { + Notepad note=new Notepad(); + } + //"打开"功能 else if(command.equals("打开(O)")) { - filechooser=new JFileChooser(); - filechooser.setDialogTitle("请选择文件"); - filechooser.showOpenDialog(null); - filechooser.setVisible(true); - File file=filechooser.getSelectedFile(); - BufferedReader bu=null; - try { - FileReader reader=new FileReader(file); - bu=new BufferedReader(reader); - String readcontent=""; - String allcode=""; - while((readcontent=bu.readLine())!=null) { - allcode+= readcontent+"\r\n"; - } - textarea.setText(allcode); - } - catch(Exception e1){ - e1.printStackTrace(); - } - finally { - try { - bu.close(); + String content = new String(textarea.getText()); + if (flag) { // 如果有源文件 + if (txt.equals(content)) { // 判断文本是否有改动(此为无改动) + flag=false; + open(); + } + else { // 有改动 + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", + JOptionPane.YES_NO_CANCEL_OPTION);//添加确认提示框 + if (i == JOptionPane.OK_OPTION) { + flag=false; + save(); + open(); + } + else if (i == JOptionPane.NO_OPTION) { + flag=false; + open(); + } } - catch(IOException e2) { - e2.printStackTrace(); + } + else { // 无源文件 + if (txt.equals(content)) { // 判断文本是否有改动 + flag=false; + open(); + } + else { // 有改动 + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", + JOptionPane.YES_NO_CANCEL_OPTION); + if (i == JOptionPane.OK_OPTION) { + flag=false; + save(); + open(); + } + else if (i == JOptionPane.NO_OPTION) { + flag=false; + open(); + } } } } @@ -193,6 +222,119 @@ public class Notepad implements ActionListener { save(); } + //"另存为"功能 + else if(command.equals("另存为(A)")) { + save(); + flag=true; + } + + //"退出"功能 + else if(command.equals("退出(X)")) { + String content = new String(textarea.getText()); + if (flag) { // 如果有源文件 + if (txt.equals(content)) { // 判断文本是否有改动(此为无改动) + flag=false; + System.exit(0); + } + else { // 有改动 + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", + JOptionPane.YES_NO_CANCEL_OPTION);//添加确认提示框 + if (i == JOptionPane.OK_OPTION) { + flag=false; + save(); + System.exit(0); + } + else if (i == JOptionPane.NO_OPTION) { + flag=false; + System.exit(0); + } + } + } + else { // 无源文件 + if (txt.equals(content)) { // 判断文本是否有改动 + flag=false; + System.exit(0); + } + else { // 有改动 + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", + JOptionPane.YES_NO_CANCEL_OPTION); + if (i == JOptionPane.OK_OPTION) { + flag=false; + save(); + System.exit(0); + } + else if (i == JOptionPane.NO_OPTION) { + flag=false; + System.exit(0); + } + } + } + } + + //"编辑"菜单 + //"撤销"功能 + else if(command.equals("撤销(U)")) { + + } + + //"剪切"功能 + else if(command.equals("剪切(T)")) { + String temp = textarea.getSelectedText(); + StringSelection selection = new StringSelection(temp); //传送到字符串里存着 + clipboard.setContents(selection, null); + textarea.setText(""); + } + + //"复制"功能 + else if(command.equals("复制(C)")) { + String temp = textarea.getSelectedText(); + StringSelection selection = new StringSelection(temp); //传送到字符串里存着 + clipboard.setContents(selection, null); + } + + //"粘贴"功能(该部分代码有借鉴) + else if(command.equals("粘贴(P)")) { + Transferable temp = clipboard.getContents(null); //获取系统剪贴板中的内容 + if (temp.isDataFlavorSupported(DataFlavor.stringFlavor)) { //判断剪贴板中的内容是否支持文本 + try { + String text = (String) temp.getTransferData(DataFlavor.stringFlavor); //强制转换剪贴板中的内容 + int n=textarea.getCaretPosition(); //获取当前光标的位置 + textarea.insert(text,n); //插入复制的内容到文本框的光标后面 + } + catch (Exception e1) { + e1.printStackTrace(); + } + } + } + + //"删除"功能 + else if(command.equals("删除(L)")) { + + } + + //"格式"菜单 + //"撤销"功能 + else if(command.equals("自动换行(W)")) { + + } + + //"查看"菜单 + //"撤销"功能 + else if(command.equals("状态栏(S)")) { + + } + + //"帮助"菜单 + //"撤销"功能 + else if(command.equals("查看帮助(H)")) { + + } + + //"关于记事本"功能 + else if(command.equals("关于记事本(A)")) { + + } + } // 新建窗口 @@ -235,12 +377,40 @@ public class Notepad implements ActionListener { } } } + + //打开文件 + private void open() { + FileDialog filedialog = new FileDialog(frame, "请选择文件", FileDialog.LOAD); + filedialog.setVisible(true); //保存文件的对话框设置为可见 + String absPath = filedialog.getDirectory() + filedialog.getFile(); + frame.setTitle(filedialog.getFile()); + try { + bu = new BufferedReader(new FileReader(absPath)); + String str=""; + String allcontent=""; + while((str=bu.readLine())!=null) + allcontent+= str+"\r\n"; + textarea.setText(allcontent); + } + catch (Exception e2) { + e2.printStackTrace(); + } + finally { + try { + bu.close(); + } + catch(IOException e3) { + e3.printStackTrace(); + } + } + } - // 保存文件 + //保存文件 private void save() { - FileDialog fileDialog = new FileDialog(frame, "保存文件至", FileDialog.SAVE); - fileDialog.setVisible(true); //保存文件的对话框设置为可见 - String absPath = fileDialog.getDirectory() + fileDialog.getFile(); //获取保存的路径和设置的文件名 + FileDialog filedialog = new FileDialog(frame, "保存文件至", FileDialog.SAVE); + filedialog.setVisible(true); //保存文件的对话框设置为可见 + String absPath = filedialog.getDirectory() + filedialog.getFile(); //获取保存的路径和设置的文件名 + frame.setTitle(filedialog.getFile());//将窗口设为已保存文件的名字 try {//用来检测是否设置了保存路径 BufferedWriter wr = new BufferedWriter(new FileWriter(absPath)); //设置输出文件名为保存的路径下面的文件名 String s = textarea.getText(); //获取文本域字符串 @@ -254,7 +424,7 @@ public class Notepad implements ActionListener { public static void main(String[] args) { Notepad window=new Notepad(); - //生成windows风格界面 (此代码为借鉴) + //生成Windows风格的UI界面 (此代码为借鉴) try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException @@ -263,5 +433,3 @@ public class Notepad implements ActionListener { } } } - - -- Gitee From ccdba2903899b38d6c742da2dd449a71bad5074b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E6=99=93=E7=83=81?= <10945491+yu-xiaoshuo@user.noreply.gitee.com> Date: Thu, 19 May 2022 14:46:54 +0000 Subject: [PATCH 04/16] add LICENSE. --- LICENSE | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ee58399 --- /dev/null +++ b/LICENSE @@ -0,0 +1,127 @@ + 鏈ㄥ叞瀹芥澗璁稿彲璇, 绗2鐗 + + 鏈ㄥ叞瀹芥澗璁稿彲璇侊紝 绗2鐗 + 2020骞1鏈 http://license.coscl.org.cn/MulanPSL2 + + + 鎮ㄥ鈥滆蒋浠垛濈殑澶嶅埗銆佷娇鐢ㄣ佷慨鏀瑰強鍒嗗彂鍙楁湪鍏板鏉捐鍙瘉锛岀2鐗堬紙鈥滄湰璁稿彲璇佲濓級鐨勫涓嬫潯娆剧殑绾︽潫锛 + + 0. 瀹氫箟 + + 鈥滆蒋浠垛濇槸鎸囩敱鈥滆础鐚濇瀯鎴愮殑璁稿彲鍦ㄢ滄湰璁稿彲璇佲濅笅鐨勭▼搴忓拰鐩稿叧鏂囨。鐨勯泦鍚堛 + + 鈥滆础鐚濇槸鎸囩敱浠讳竴鈥滆础鐚呪濊鍙湪鈥滄湰璁稿彲璇佲濅笅鐨勫彈鐗堟潈娉曚繚鎶ょ殑浣滃搧銆 + + 鈥滆础鐚呪濇槸鎸囧皢鍙楃増鏉冩硶淇濇姢鐨勪綔鍝佽鍙湪鈥滄湰璁稿彲璇佲濅笅鐨勮嚜鐒朵汉鎴栤滄硶浜哄疄浣撯濄 + + 鈥滄硶浜哄疄浣撯濇槸鎸囨彁浜よ础鐚殑鏈烘瀯鍙婂叾鈥滃叧鑱斿疄浣撯濄 + + 鈥滃叧鑱斿疄浣撯濇槸鎸囷紝瀵光滄湰璁稿彲璇佲濅笅鐨勮涓烘柟鑰岃█锛屾帶鍒躲佸彈鎺у埗鎴栦笌鍏跺叡鍚屽彈鎺у埗鐨勬満鏋勶紝姝ゅ鐨勬帶鍒舵槸鎸囨湁鍙楁帶鏂规垨鍏卞悓鍙楁帶鏂硅嚦灏50%鐩存帴鎴栭棿鎺ョ殑鎶曠エ鏉冦佽祫閲戞垨鍏朵粬鏈変环璇佸埜銆 + + 1. 鎺堜簣鐗堟潈璁稿彲 + + 姣忎釜鈥滆础鐚呪濇牴鎹滄湰璁稿彲璇佲濇巿浜堟偍姘镐箙鎬х殑銆佸叏鐞冩х殑銆佸厤璐圭殑銆侀潪鐙崰鐨勩佷笉鍙挙閿鐨勭増鏉冭鍙紝鎮ㄥ彲浠ュ鍒躲佷娇鐢ㄣ佷慨鏀广佸垎鍙戝叾鈥滆础鐚濓紝涓嶈淇敼涓庡惁銆 + + 2. 鎺堜簣涓撳埄璁稿彲 + + 姣忎釜鈥滆础鐚呪濇牴鎹滄湰璁稿彲璇佲濇巿浜堟偍姘镐箙鎬х殑銆佸叏鐞冩х殑銆佸厤璐圭殑銆侀潪鐙崰鐨勩佷笉鍙挙閿鐨勶紙鏍规嵁鏈潯瑙勫畾鎾ら攢闄ゅ锛変笓鍒╄鍙紝渚涙偍鍒堕犮佸鎵樺埗閫犮佷娇鐢ㄣ佽璇洪攢鍞侀攢鍞佽繘鍙e叾鈥滆础鐚濇垨浠ュ叾浠栨柟寮忚浆绉诲叾鈥滆础鐚濄傚墠杩颁笓鍒╄鍙粎闄愪簬鈥滆础鐚呪濈幇鍦ㄦ垨灏嗘潵鎷ユ湁鎴栨帶鍒剁殑鍏垛滆础鐚濇湰韬垨鍏垛滆础鐚濅笌璁稿彲鈥滆础鐚濇椂鐨勨滆蒋浠垛濈粨鍚堣屽皢蹇呯劧浼氫镜鐘殑涓撳埄鏉冨埄瑕佹眰锛屼笉鍖呮嫭瀵光滆础鐚濈殑淇敼鎴栧寘鍚滆础鐚濈殑鍏朵粬缁撳悎銆傚鏋滄偍鎴栨偍鐨勨滃叧鑱斿疄浣撯濈洿鎺ユ垨闂存帴鍦帮紝灏扁滆蒋浠垛濇垨鍏朵腑鐨勨滆础鐚濆浠讳綍浜哄彂璧蜂笓鍒╀镜鏉冭瘔璁硷紙鍖呮嫭鍙嶈瘔鎴栦氦鍙夎瘔璁硷級鎴栧叾浠栦笓鍒╃淮鏉冭鍔紝鎸囨帶鍏朵镜鐘笓鍒╂潈锛屽垯鈥滄湰璁稿彲璇佲濇巿浜堟偍瀵光滆蒋浠垛濈殑涓撳埄璁稿彲鑷偍鎻愯捣璇夎鎴栧彂璧风淮鏉冭鍔ㄤ箣鏃ョ粓姝€ + + 3. 鏃犲晢鏍囪鍙 + + 鈥滄湰璁稿彲璇佲濅笉鎻愪緵瀵光滆础鐚呪濈殑鍟嗗搧鍚嶇О銆佸晢鏍囥佹湇鍔℃爣蹇楁垨浜у搧鍚嶇О鐨勫晢鏍囪鍙紝浣嗘偍涓烘弧瓒崇4鏉¤瀹氱殑澹版槑涔夊姟鑰屽繀椤讳娇鐢ㄩ櫎澶栥 + + 4. 鍒嗗彂闄愬埗 + + 鎮ㄥ彲浠ュ湪浠讳綍濯掍粙涓皢鈥滆蒋浠垛濅互婧愮▼搴忓舰寮忔垨鍙墽琛屽舰寮忛噸鏂板垎鍙戯紝涓嶈淇敼涓庡惁锛屼絾鎮ㄥ繀椤诲悜鎺ユ敹鑰呮彁渚涒滄湰璁稿彲璇佲濈殑鍓湰锛屽苟淇濈暀鈥滆蒋浠垛濅腑鐨勭増鏉冦佸晢鏍囥佷笓鍒╁強鍏嶈矗澹版槑銆 + + 5. 鍏嶈矗澹版槑涓庤矗浠婚檺鍒 + + 鈥滆蒋浠垛濆強鍏朵腑鐨勨滆础鐚濆湪鎻愪緵鏃朵笉甯︿换浣曟槑绀烘垨榛樼ず鐨勬媴淇濄傚湪浠讳綍鎯呭喌涓嬶紝鈥滆础鐚呪濇垨鐗堟潈鎵鏈夎呬笉瀵逛换浣曚汉鍥犱娇鐢ㄢ滆蒋浠垛濇垨鍏朵腑鐨勨滆础鐚濊屽紩鍙戠殑浠讳綍鐩存帴鎴栭棿鎺ユ崯澶辨壙鎷呰矗浠伙紝涓嶈鍥犱綍绉嶅師鍥犲鑷存垨鑰呭熀浜庝綍绉嶆硶寰嬬悊璁猴紝鍗充娇鍏舵浘琚缓璁湁姝ょ鎹熷け鐨勫彲鑳芥с + + 6. 璇█ + 鈥滄湰璁稿彲璇佲濅互涓嫳鏂囧弻璇〃杩帮紝涓嫳鏂囩増鏈叿鏈夊悓绛夋硶寰嬫晥鍔涖傚鏋滀腑鑻辨枃鐗堟湰瀛樺湪浠讳綍鍐茬獊涓嶄竴鑷达紝浠ヤ腑鏂囩増涓哄噯銆 + + 鏉℃缁撴潫 + + 濡備綍灏嗘湪鍏板鏉捐鍙瘉锛岀2鐗堬紝搴旂敤鍒版偍鐨勮蒋浠 + + 濡傛灉鎮ㄥ笇鏈涘皢鏈ㄥ叞瀹芥澗璁稿彲璇侊紝绗2鐗堬紝搴旂敤鍒版偍鐨勬柊杞欢锛屼负浜嗘柟渚挎帴鏀惰呮煡闃咃紝寤鸿鎮ㄥ畬鎴愬涓嬩笁姝ワ細 + + 1锛 璇锋偍琛ュ厖濡備笅澹版槑涓殑绌虹櫧锛屽寘鎷蒋浠跺悕銆佽蒋浠剁殑棣栨鍙戣〃骞翠唤浠ュ強鎮ㄤ綔涓虹増鏉冧汉鐨勫悕瀛楋紱 + + 2锛 璇锋偍鍦ㄨ蒋浠跺寘鐨勪竴绾х洰褰曚笅鍒涘缓浠モ淟ICENSE鈥濅负鍚嶇殑鏂囦欢锛屽皢鏁翠釜璁稿彲璇佹枃鏈斁鍏ヨ鏂囦欢涓紱 + + 3锛 璇峰皢濡備笅澹版槑鏂囨湰鏀惧叆姣忎釜婧愭枃浠剁殑澶撮儴娉ㄩ噴涓 + + Copyright (c) [Year] [name of copyright holder] + [Software Name] is licensed under Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. + + + Mulan Permissive Software License锛孷ersion 2 + + Mulan Permissive Software License锛孷ersion 2 (Mulan PSL v2) + January 2020 http://license.coscl.org.cn/MulanPSL2 + + Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v2 (this License) with the following terms and conditions: + + 0. Definition + + Software means the program and related documents which are licensed under this License and comprise all Contribution(s). + + Contribution means the copyrightable work licensed by a particular Contributor under this License. + + Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License. + + Legal Entity means the entity making a Contribution and all its Affiliates. + + Affiliates means entities that control, are controlled by, or are under common control with the acting entity under this License, 鈥榗ontrol鈥 means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity. + + 1. Grant of Copyright License + + Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not. + + 2. Grant of Patent License + + Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution, where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed. The patent license shall not apply to any modification of the Contribution, and any other combination which includes the Contribution. If you or your Affiliates directly or indirectly institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken. + + 3. No Trademark License + + No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in Section 4. + + 4. Distribution Restriction + + You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software. + + 5. Disclaimer of Warranty and Limitation of Liability + + THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO MATTER HOW IT鈥橲 CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + + 6. Language + + THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION SHALL PREVAIL. + + END OF THE TERMS AND CONDITIONS + + How to Apply the Mulan Permissive Software License锛孷ersion 2 (Mulan PSL v2) to Your Software + + To apply the Mulan PSL v2 to your work, for easy identification by recipients, you are suggested to complete following three steps: + + i Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner; + + ii Create a file named 鈥淟ICENSE鈥 which contains the whole context of this License in the first directory of your software package; + + iii Attach the statement to the appropriate annotated syntax at the beginning of each source file. + + + Copyright (c) [Year] [name of copyright holder] + [Software Name] is licensed under Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. -- Gitee From 78a20772cc984b401fcfc583903d2e8cd53feda7 Mon Sep 17 00:00:00 2001 From: yuxiaoshuo Date: Thu, 19 May 2022 23:58:40 +0800 Subject: [PATCH 05/16] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E9=87=8C=E7=9A=84=E6=89=80=E6=9C=89=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E4=BD=86=E4=BB=8D=E9=9C=80=E6=94=B9=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 222 +++++++++++++++++++++----------- 1 file changed, 144 insertions(+), 78 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 548a4f0..a93162e 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -6,22 +6,36 @@ import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.io.*; import javax.swing.*; +import javax.swing.event.CaretEvent; +import javax.swing.event.CaretListener; +import javax.swing.event.UndoableEditEvent; +import javax.swing.event.UndoableEditListener; import javax.swing.filechooser.FileFilter; +import javax.swing.undo.CannotRedoException; +import javax.swing.undo.UndoManager; import java.awt.event.*; public class Notepad implements ActionListener { - private JFrame frame=new JFrame("文本编辑器"); + private JFrame frame=new JFrame("无标题-记事本"); private JTextArea textarea=new JTextArea(40,40); private JScrollPane pane=new JScrollPane(textarea); private Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //调用系统剪贴板方法 + private UndoManager undomanager = new UndoManager(); private JMenuBar menubar; private JMenu menu1,menu2,menu3,menu4,menu5; - private JMenuItem item,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,item11,item12,item13,item14; + private JMenuItem itemnew,itemwindow,itemopen,itemsave,itemsaveas,itemexit,itemrevoke,itemshear,itemcopy,itempaste,itemdelete,itemselectall,itemhelp,itemabout; + private JCheckBoxMenuItem itemautowrap,itemstatusbar; + private JToolBar toolstate; + private JLabel label1,label2; private JFileChooser filechooser; private boolean flag = true;// 需要一个flag变量,存储当前文件是否存在源文件:true,有;false,没有 private String txt = ""; private File FILE; private BufferedReader bu; + + int line=1;//文本行数 + int col=1;//文本列数 + int sum=0; public Notepad() { //实例化 menubar=new JMenuBar(); @@ -30,41 +44,44 @@ public class Notepad implements ActionListener { menu3=new JMenu("格式(O)"); menu4=new JMenu("查看(V)"); menu5=new JMenu("帮助(H)"); - item=new JMenuItem("新建(N)"); - item1=new JMenuItem("新窗口(W)"); - item2=new JMenuItem("打开(O)"); - item3=new JMenuItem("保存(S)"); - item4=new JMenuItem("另存为(A)"); - item5=new JMenuItem("退出(X)"); - item6=new JMenuItem("撤消(U)"); - item7=new JMenuItem("剪切(T)"); - item8=new JMenuItem("复制(C)"); - item9=new JMenuItem("粘贴(P)"); - item10=new JMenuItem("删除(L)"); - item11=new JMenuItem("自动换行(W)"); - item12=new JMenuItem("状态栏(S)"); - item13=new JMenuItem("查看帮助(H)"); - item14=new JMenuItem("关于记事本(A)"); + itemnew=new JMenuItem("新建(N)"); + itemwindow=new JMenuItem("新窗口(W)"); + itemopen=new JMenuItem("打开(O)"); + itemsave=new JMenuItem("保存(S)"); + itemsaveas=new JMenuItem("另存为(A)"); + itemexit=new JMenuItem("退出(X)"); + itemrevoke=new JMenuItem("撤消(U)"); + itemshear=new JMenuItem("剪切(T)"); + itemcopy=new JMenuItem("复制(C)"); + itempaste=new JMenuItem("粘贴(P)"); + itemdelete=new JMenuItem("删除(L)"); + itemselectall=new JMenuItem("全选(A)"); + itemautowrap=new JCheckBoxMenuItem("自动换行(W)", false); + itemstatusbar=new JCheckBoxMenuItem("状态栏(S)", true); + itemhelp=new JMenuItem("查看帮助(H)"); + itemabout=new JMenuItem("关于记事本(A)"); //组建 - menu1.add(item); - menu1.add(item1); - menu1.add(item2); - menu1.add(item3); - menu1.add(item4); + menu1.add(itemnew); + menu1.add(itemwindow); + menu1.add(itemopen); + menu1.add(itemsave); + menu1.add(itemsaveas); menu1.addSeparator();//添加分割线 - menu1.add(item5); - menu2.add(item6); + menu1.add(itemexit); + menu2.add(itemrevoke); + menu2.addSeparator(); + menu2.add(itemshear); + menu2.add(itemcopy); + menu2.add(itempaste); + menu2.add(itemdelete); menu2.addSeparator(); - menu2.add(item7); - menu2.add(item8); - menu2.add(item9); - menu2.add(item10); - menu3.add(item11); - menu4.add(item12); - menu5.add(item13); + menu2.add(itemselectall); + menu3.add(itemautowrap); + menu4.add(itemstatusbar); + menu5.add(itemhelp); menu5.addSeparator(); - menu5.add(item14); + menu5.add(itemabout); menubar.add(menu1); menubar.add(menu2); menubar.add(menu3); @@ -79,30 +96,58 @@ public class Notepad implements ActionListener { frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); textarea.setFont(new Font("宋体",Font.PLAIN,20)); - textarea.setLineWrap(true);//自动换行 menubar.setBackground(Color.white); - add(); + + //状态栏 + toolstate=new JToolBar(); + toolstate.setSize(textarea.getSize().width,10); + toolstate.setBackground(Color.LIGHT_GRAY); + toolstate.add(label1 = new JLabel("第"+line+"行,第"+col+"列")); + toolstate.add(label2 = new JLabel(" 一共"+sum+"字")); + frame.add(toolstate,BorderLayout.SOUTH); + toolstate.setVisible(true); + toolstate.setFloatable(false); + } public void add() { - //添加监视器 - item.addActionListener(this); - item1.addActionListener(this); - item2.addActionListener(this); - item3.addActionListener(this); - item4.addActionListener(this); - item5.addActionListener(this); - item6.addActionListener(this); - item7.addActionListener(this); - item8.addActionListener(this); - item9.addActionListener(this); - item10.addActionListener(this); - item11.addActionListener(this); - item12.addActionListener(this); - item13.addActionListener(this); - item14.addActionListener(this); + //添加监听器 + itemnew.addActionListener(this); + itemwindow.addActionListener(this); + itemopen.addActionListener(this); + itemsave.addActionListener(this); + itemsaveas.addActionListener(this); + itemexit.addActionListener(this); + itemrevoke.addActionListener(this); + itemshear.addActionListener(this); + itemcopy.addActionListener(this); + itempaste.addActionListener(this); + itemdelete.addActionListener(this); + itemselectall.addActionListener(this); + itemautowrap.addActionListener(this); + itemstatusbar.addActionListener(this); + itemhelp.addActionListener(this); + itemabout.addActionListener(this); + textarea.getDocument().addUndoableEditListener(undomanager); + + //添加状态栏监听器 + textarea.addCaretListener(new CaretListener() { + @Override + public void caretUpdate(CaretEvent e) { + try { + int position = textarea.getCaretPosition(); + int line = textarea.getLineOfOffset(position) + 1;// 获取行数 + int col = position - textarea.getLineStartOffset(line - 1) + 1;// 获取列数 + label1.setText("第"+line+"行,第"+col+"列"); + label2.setText("一共"+textarea.getText().length()+"字"); + } + catch (Exception ex) {} + } + }); + + //设置快捷键 menu1.setMnemonic('F'); //设置快捷键ALT+F @@ -110,22 +155,24 @@ public class Notepad implements ActionListener { menu3.setMnemonic('O'); menu4.setMnemonic('V'); menu5.setMnemonic('H'); - item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,2)); //设置快捷键CTRL+N - item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,3)); //设置快捷键CTRL+SHIFT+N - item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,2)); - item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,2)); - item4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,3)); - item6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,2)); - item7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,2)); - item8.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,2)); - item9.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,2)); - item10.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); //设置快捷键DELETE + itemnew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,2)); //设置快捷键CTRL+N + itemwindow.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,3)); //设置快捷键CTRL+SHIFT+N + itemopen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,2)); + itemsave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,2)); + itemsaveas.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,3)); + itemrevoke.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,2)); + itemshear.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,2)); + itemcopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,2)); + itempaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,2)); + itemdelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); //设置快捷键DELETE + itemselectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,2)); + } public void actionPerformed(ActionEvent e) { String command=e.getActionCommand(); - //"文件"菜单 + //——"文件"菜单 //"新建"功能 if(command.equals("新建(N)")) { String content = new String(textarea.getText()); @@ -271,25 +318,27 @@ public class Notepad implements ActionListener { } } - //"编辑"菜单 + //——"编辑"菜单 //"撤销"功能 else if(command.equals("撤销(U)")) { - + if (undomanager.canUndo()) { + undomanager.undo(); + } } //"剪切"功能 else if(command.equals("剪切(T)")) { String temp = textarea.getSelectedText(); - StringSelection selection = new StringSelection(temp); //传送到字符串里存着 - clipboard.setContents(selection, null); - textarea.setText(""); + StringSelection selectedtxt = new StringSelection(temp); //传送到字符串里存着 + clipboard.setContents(selectedtxt, null); + textarea.replaceRange("", textarea.getSelectionStart(), textarea.getSelectionEnd()); //将选中文本清空 } //"复制"功能 else if(command.equals("复制(C)")) { String temp = textarea.getSelectedText(); - StringSelection selection = new StringSelection(temp); //传送到字符串里存着 - clipboard.setContents(selection, null); + StringSelection selectedtxt = new StringSelection(temp); //传送到字符串里存着 + clipboard.setContents(selectedtxt, null); } //"粘贴"功能(该部分代码有借鉴) @@ -309,30 +358,45 @@ public class Notepad implements ActionListener { //"删除"功能 else if(command.equals("删除(L)")) { - + textarea.replaceRange("", textarea.getSelectionStart(), textarea.getSelectionEnd()); //将选中文本清空 + } + + //"全选"功能 + else if(command.equals("全选(A)")) { + textarea.selectAll(); } //"格式"菜单 - //"撤销"功能 + //"自动换行"功能 else if(command.equals("自动换行(W)")) { - + if(itemautowrap.isSelected()) { + textarea.setLineWrap(true); + } + else { + textarea.setLineWrap(false); + } } - //"查看"菜单 - //"撤销"功能 + //——"查看"菜单 + //"状态栏"功能 else if(command.equals("状态栏(S)")) { - + if (itemstatusbar.isSelected()) { + toolstate.setVisible(true); + } + else { + toolstate.setVisible(false); + } } - //"帮助"菜单 - //"撤销"功能 + //——"帮助"菜单 + //"查看帮助"功能 else if(command.equals("查看帮助(H)")) { - + JOptionPane.showMessageDialog(frame,"请查看百度!","查看帮助",1); } //"关于记事本"功能 else if(command.equals("关于记事本(A)")) { - + JOptionPane.showMessageDialog(frame,"记事本\n作者:yuxiaoshuo\n我的邮箱:yxsthebest@qq.com","关于记事本",1); } } @@ -378,6 +442,8 @@ public class Notepad implements ActionListener { } } + + //打开文件 private void open() { FileDialog filedialog = new FileDialog(frame, "请选择文件", FileDialog.LOAD); -- Gitee From 4c133288f8a35aa1db51dd12dac8a46a3d574d5f Mon Sep 17 00:00:00 2001 From: yuxiaoshuo Date: Sun, 22 May 2022 22:00:50 +0800 Subject: [PATCH 06/16] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=8F=B3?= =?UTF-8?q?=E9=94=AE=E8=8F=9C=E5=8D=95=EF=BC=8C=E5=AE=8C=E5=96=84=E4=BA=86?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 192 +++++++++++++++++++++++--------- 1 file changed, 142 insertions(+), 50 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index a93162e..c51f0ad 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -14,9 +14,10 @@ import javax.swing.filechooser.FileFilter; import javax.swing.undo.CannotRedoException; import javax.swing.undo.UndoManager; + import java.awt.event.*; public class Notepad implements ActionListener { - private JFrame frame=new JFrame("无标题-记事本"); + private JFrame frame=new JFrame("无标题 - 记事本"); private JTextArea textarea=new JTextArea(40,40); private JScrollPane pane=new JScrollPane(textarea); private Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //调用系统剪贴板方法 @@ -28,16 +29,22 @@ public class Notepad implements ActionListener { private JToolBar toolstate; private JLabel label1,label2; private JFileChooser filechooser; - private boolean flag = true;// 需要一个flag变量,存储当前文件是否存在源文件:true,有;false,没有 - private String txt = ""; + private FileDialog filedialog; + private boolean flag = true;// 需要一个flag变量,判断当前文件是否存在源文件:true,有;false,没有 + private String txt = "",tst; + private String currentpath = null; private File FILE; private BufferedReader bu; + //右键菜单 + JPopupMenu popupmenu; + JMenuItem poprevoke,popshear,popcopy,poppaste,popdelete,popselectall; + int line=1;//文本行数 int col=1;//文本列数 int sum=0; public Notepad() { - //实例化 + //创建菜单 menubar=new JMenuBar(); menu1=new JMenu("文件(F)"); menu2=new JMenu("编辑(E)"); @@ -50,7 +57,7 @@ public class Notepad implements ActionListener { itemsave=new JMenuItem("保存(S)"); itemsaveas=new JMenuItem("另存为(A)"); itemexit=new JMenuItem("退出(X)"); - itemrevoke=new JMenuItem("撤消(U)"); + itemrevoke=new JMenuItem("撤销(U)"); itemshear=new JMenuItem("剪切(T)"); itemcopy=new JMenuItem("复制(C)"); itempaste=new JMenuItem("粘贴(P)"); @@ -61,7 +68,16 @@ public class Notepad implements ActionListener { itemhelp=new JMenuItem("查看帮助(H)"); itemabout=new JMenuItem("关于记事本(A)"); - //组建 + //创建右键弹出菜单 + popupmenu=new JPopupMenu(); + poprevoke=new JMenuItem("撤销(U)"); + popshear=new JMenuItem("剪切(T)"); + popcopy=new JMenuItem("复制(C)"); + poppaste=new JMenuItem("粘贴(P)"); + popdelete=new JMenuItem("删除(L)"); + popselectall=new JMenuItem("全选(A)"); + + //添加到菜单 menu1.add(itemnew); menu1.add(itemwindow); menu1.add(itemopen); @@ -90,6 +106,16 @@ public class Notepad implements ActionListener { frame.setJMenuBar(menubar); frame.add(pane); + //添加到右键菜单 + popupmenu.add(poprevoke); + popupmenu.addSeparator(); + popupmenu.add(popshear); + popupmenu.add(popcopy); + popupmenu.add(poppaste); + popupmenu.add(popdelete); + popupmenu.addSeparator(); + popupmenu.add(popselectall); + //窗口基本参数 frame.setBounds(20,30,900,550); frame.setLocationRelativeTo(null);//设置窗口居中显示 @@ -99,11 +125,11 @@ public class Notepad implements ActionListener { menubar.setBackground(Color.white); add(); - //状态栏 + //状态栏基本参数 toolstate=new JToolBar(); toolstate.setSize(textarea.getSize().width,10); toolstate.setBackground(Color.LIGHT_GRAY); - toolstate.add(label1 = new JLabel("第"+line+"行,第"+col+"列")); + toolstate.add(label1 = new JLabel(" 第"+line+"行,第"+col+"列 ")); toolstate.add(label2 = new JLabel(" 一共"+sum+"字")); frame.add(toolstate,BorderLayout.SOUTH); toolstate.setVisible(true); @@ -113,7 +139,7 @@ public class Notepad implements ActionListener { public void add() { - //添加监听器 + //添加菜单项监听器 itemnew.addActionListener(this); itemwindow.addActionListener(this); itemopen.addActionListener(this); @@ -130,6 +156,16 @@ public class Notepad implements ActionListener { itemstatusbar.addActionListener(this); itemhelp.addActionListener(this); itemabout.addActionListener(this); + + //添加右键菜单监听器 + poprevoke.addActionListener(this); + popshear.addActionListener(this); + popcopy.addActionListener(this); + poppaste.addActionListener(this); + popdelete.addActionListener(this); + popselectall.addActionListener(this); + + //添加撤销监听器 textarea.getDocument().addUndoableEditListener(undomanager); //添加状态栏监听器 @@ -138,16 +174,35 @@ public class Notepad implements ActionListener { public void caretUpdate(CaretEvent e) { try { int position = textarea.getCaretPosition(); - int line = textarea.getLineOfOffset(position) + 1;// 获取行数 - int col = position - textarea.getLineStartOffset(line - 1) + 1;// 获取列数 + line = textarea.getLineOfOffset(position) + 1;// 获取行数 + col = position - textarea.getLineStartOffset(line - 1) + 1;// 获取列数 + sum=textarea.getText().length();// 获取总字数 label1.setText("第"+line+"行,第"+col+"列"); - label2.setText("一共"+textarea.getText().length()+"字"); + label2.setText("一共"+sum+"字"); } catch (Exception ex) {} } }); - + //添加右键菜单监听器(此部分代码有参考) + textarea.addMouseListener(new MouseAdapter() { + public void mousePressed(MouseEvent e) { + if (e.isPopupTrigger())// 返回此鼠标事件是否为该平台的弹出菜单触发事件 + { + popupmenu.show(e.getComponent(), e.getX(), e.getY());// 在组件调用者的坐标空间中的位置 X、Y 显示弹出菜单 + } + textarea.requestFocus();// 编辑区获取焦点 + } + + public void mouseReleased(MouseEvent e) { + if (e.isPopupTrigger())// 返回此鼠标事件是否为该平台的弹出菜单触发事件 + { + popupmenu.show(e.getComponent(), e.getX(), e.getY());// 在组件调用者的坐标空间中的位置 X、Y 显示弹出菜单 + } + textarea.requestFocus();// 编辑区获取焦点 + } + }); + //设置快捷键 menu1.setMnemonic('F'); //设置快捷键ALT+F @@ -176,41 +231,41 @@ public class Notepad implements ActionListener { //"新建"功能 if(command.equals("新建(N)")) { String content = new String(textarea.getText()); - if (flag) { // 如果有源文件 - if (txt.equals(content)) { // 判断文本是否有改动(此为无改动) + if (flag&&(this.currentpath!=null)) { // 如果有源文件 + if (tst.equals(content)) { // 判断源文件文本是否有改动(此为无改动) + textarea.setText(""); flag=false; - NewFile(); } else { // 有改动 - int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将不保存", "保存", JOptionPane.YES_NO_CANCEL_OPTION);//添加确认提示框 if (i == JOptionPane.OK_OPTION) { - flag=false; save(); - NewFile(); + textarea.setText(""); + flag=false; } else if (i == JOptionPane.NO_OPTION) { + textarea.setText(""); flag=false; - NewFile(); } } } else { // 无源文件 if (txt.equals(content)) { // 判断文本是否有改动 + textarea.setText(""); flag=false; - NewFile(); } else { // 有改动 - int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将不保存", "保存", JOptionPane.YES_NO_CANCEL_OPTION); if (i == JOptionPane.OK_OPTION) { + saveAs(); + textarea.setText(""); flag=false; - save(); - NewFile(); } else if (i == JOptionPane.NO_OPTION) { + textarea.setText(""); flag=false; - NewFile(); } } } @@ -224,40 +279,40 @@ public class Notepad implements ActionListener { //"打开"功能 else if(command.equals("打开(O)")) { String content = new String(textarea.getText()); - if (flag) { // 如果有源文件 - if (txt.equals(content)) { // 判断文本是否有改动(此为无改动) - flag=false; + if (flag&&(currentpath!=null)) { // 如果有源文件 + if (tst.equals(content)) { // 判断文本是否有改动(此为无改动) open(); + flag=true; } else { // 有改动 - int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将不保存", "保存", JOptionPane.YES_NO_CANCEL_OPTION);//添加确认提示框 if (i == JOptionPane.OK_OPTION) { - flag=false; + flag=true; save(); open(); } else if (i == JOptionPane.NO_OPTION) { - flag=false; + flag=true; open(); } } } else { // 无源文件 if (txt.equals(content)) { // 判断文本是否有改动 - flag=false; + flag=true; open(); } else { // 有改动 - int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将不保存", "保存", JOptionPane.YES_NO_CANCEL_OPTION); if (i == JOptionPane.OK_OPTION) { - flag=false; + flag=true; save(); open(); } else if (i == JOptionPane.NO_OPTION) { - flag=false; + flag=true; open(); } } @@ -267,11 +322,12 @@ public class Notepad implements ActionListener { //"保存"功能 else if(command.equals("保存(S)")) { save(); + flag=true; } //"另存为"功能 else if(command.equals("另存为(A)")) { - save(); + saveAs(); flag=true; } @@ -401,11 +457,20 @@ public class Notepad implements ActionListener { } - // 新建窗口 - public void NewFile() { - frame.dispose(); - Notepad note = new Notepad(); - } + // 设置文件过滤器(还没好) + private void setFileFilter() { + FilenameFilter filter = new FilenameFilter(){ + public boolean accept(File dir, String name) { + if (name.endsWith("txt")){ + return true; + } + return false; + } + }; + filedialog.setFilenameFilter(filter); + filedialog.setVisible(true); + } + //关闭窗口时 public void Exit() { @@ -448,14 +513,19 @@ public class Notepad implements ActionListener { private void open() { FileDialog filedialog = new FileDialog(frame, "请选择文件", FileDialog.LOAD); filedialog.setVisible(true); //保存文件的对话框设置为可见 - String absPath = filedialog.getDirectory() + filedialog.getFile(); - frame.setTitle(filedialog.getFile()); + String abspath = filedialog.getDirectory() + filedialog.getFile();//获取选中文件的路径 + currentpath = filedialog.getDirectory() + filedialog.getFile(); + if(filedialog.getFile()==null) + frame.setTitle("无标题 - 记事本"); + else + frame.setTitle(filedialog.getFile()+" - 记事本"); + try { - bu = new BufferedReader(new FileReader(absPath)); + bu = new BufferedReader(new FileReader(abspath)); String str=""; String allcontent=""; while((str=bu.readLine())!=null) - allcontent+= str+"\r\n"; + allcontent+= str; textarea.setText(allcontent); } catch (Exception e2) { @@ -471,22 +541,44 @@ public class Notepad implements ActionListener { } } - //保存文件 - private void save() { + //文件保存 + private void save() { + if(this.currentpath==null) { + this.saveAs(); + } + else { + try { + BufferedWriter wr = new BufferedWriter(new FileWriter(currentpath)); + String s = textarea.getText(); + wr.write(s); + wr.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + tst=textarea.getText(); + } + + } + + //文件另存为 + private void saveAs() { FileDialog filedialog = new FileDialog(frame, "保存文件至", FileDialog.SAVE); filedialog.setVisible(true); //保存文件的对话框设置为可见 - String absPath = filedialog.getDirectory() + filedialog.getFile(); //获取保存的路径和设置的文件名 + String abspath = filedialog.getDirectory() + filedialog.getFile(); //获取保存的路径和设置的文件名 frame.setTitle(filedialog.getFile());//将窗口设为已保存文件的名字 + currentpath = filedialog.getDirectory() + filedialog.getFile(); + tst=textarea.getText(); try {//用来检测是否设置了保存路径 - BufferedWriter wr = new BufferedWriter(new FileWriter(absPath)); //设置输出文件名为保存的路径下面的文件名 + BufferedWriter wr = new BufferedWriter(new FileWriter(abspath)); //设置输出文件名为保存的路径下面的文件名 String s = textarea.getText(); //获取文本域字符串 wr.write(s); //开始写入 wr.close(); //写完关闭 } catch (IOException e) { e.printStackTrace(); - } } + } public static void main(String[] args) { Notepad window=new Notepad(); -- Gitee From 8b4a705fbf2c2fa65d6b799419b184e4a80aa7bf Mon Sep 17 00:00:00 2001 From: yuxiaoshuo Date: Tue, 24 May 2022 00:04:42 +0800 Subject: [PATCH 07/16] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86=E6=96=87?= =?UTF-8?q?=E4=BB=B6=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 | 76 ++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index c51f0ad..0c34b61 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -31,7 +31,7 @@ public class Notepad implements ActionListener { private JFileChooser filechooser; private FileDialog filedialog; private boolean flag = true;// 需要一个flag变量,判断当前文件是否存在源文件:true,有;false,没有 - private String txt = "",tst; + private String txt = "",tst,name; private String currentpath = null; private File FILE; private BufferedReader bu; @@ -231,7 +231,7 @@ public class Notepad implements ActionListener { //"新建"功能 if(command.equals("新建(N)")) { String content = new String(textarea.getText()); - if (flag&&(this.currentpath!=null)) { // 如果有源文件 + if (flag&&(name!=null)) { // 如果有源文件 if (tst.equals(content)) { // 判断源文件文本是否有改动(此为无改动) textarea.setText(""); flag=false; @@ -259,7 +259,7 @@ public class Notepad implements ActionListener { int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将不保存", "保存", JOptionPane.YES_NO_CANCEL_OPTION); if (i == JOptionPane.OK_OPTION) { - saveAs(); + save(); textarea.setText(""); flag=false; } @@ -279,7 +279,7 @@ public class Notepad implements ActionListener { //"打开"功能 else if(command.equals("打开(O)")) { String content = new String(textarea.getText()); - if (flag&&(currentpath!=null)) { // 如果有源文件 + if (flag&&(name!=null)) { // 如果有源文件 if (tst.equals(content)) { // 判断文本是否有改动(此为无改动) open(); flag=true; @@ -288,32 +288,32 @@ public class Notepad implements ActionListener { int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将不保存", "保存", JOptionPane.YES_NO_CANCEL_OPTION);//添加确认提示框 if (i == JOptionPane.OK_OPTION) { - flag=true; save(); open(); + flag=true; } else if (i == JOptionPane.NO_OPTION) { - flag=true; open(); + flag=true; } } } else { // 无源文件 if (txt.equals(content)) { // 判断文本是否有改动 - flag=true; open(); + flag=true; } else { // 有改动 int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将不保存", "保存", JOptionPane.YES_NO_CANCEL_OPTION); if (i == JOptionPane.OK_OPTION) { - flag=true; save(); open(); + flag=true; } else if (i == JOptionPane.NO_OPTION) { - flag=true; open(); + flag=true; } } } @@ -515,11 +515,10 @@ public class Notepad implements ActionListener { filedialog.setVisible(true); //保存文件的对话框设置为可见 String abspath = filedialog.getDirectory() + filedialog.getFile();//获取选中文件的路径 currentpath = filedialog.getDirectory() + filedialog.getFile(); - if(filedialog.getFile()==null) - frame.setTitle("无标题 - 记事本"); - else - frame.setTitle(filedialog.getFile()+" - 记事本"); - + name=filedialog.getFile(); + frame.setTitle(name+" - 记事本"); + if(name==null) + return; try { bu = new BufferedReader(new FileReader(abspath)); String str=""; @@ -527,6 +526,7 @@ public class Notepad implements ActionListener { while((str=bu.readLine())!=null) allcontent+= str; textarea.setText(allcontent); + tst=allcontent; } catch (Exception e2) { e2.printStackTrace(); @@ -541,32 +541,17 @@ public class Notepad implements ActionListener { } } - //文件保存 - private void save() { - if(this.currentpath==null) { - this.saveAs(); - } - else { - try { - BufferedWriter wr = new BufferedWriter(new FileWriter(currentpath)); - String s = textarea.getText(); - wr.write(s); - wr.close(); - } - catch (IOException e) { - e.printStackTrace(); - } - tst=textarea.getText(); - } - - } + //文件另存为 private void saveAs() { FileDialog filedialog = new FileDialog(frame, "保存文件至", FileDialog.SAVE); filedialog.setVisible(true); //保存文件的对话框设置为可见 String abspath = filedialog.getDirectory() + filedialog.getFile(); //获取保存的路径和设置的文件名 - frame.setTitle(filedialog.getFile());//将窗口设为已保存文件的名字 + name=filedialog.getFile(); + frame.setTitle(name+" - 记事本"); + if(name==null)//若无选择文件,则返回原先窗口 + return; currentpath = filedialog.getDirectory() + filedialog.getFile(); tst=textarea.getText(); try {//用来检测是否设置了保存路径 @@ -579,6 +564,29 @@ public class Notepad implements ActionListener { e.printStackTrace(); } } + + //文件保存 + private void save() { + if(name==null) { + this.saveAs(); + if(name==null){ + return; + } + } + else { + try { + BufferedWriter wr = new BufferedWriter(new FileWriter(currentpath)); + String s = textarea.getText(); + wr.write(s); + wr.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + tst=textarea.getText(); + } + + } public static void main(String[] args) { Notepad window=new Notepad(); -- Gitee From e2730de07c8ad30827cacf9bd436e2e937309c64 Mon Sep 17 00:00:00 2001 From: yuxiaoshuo Date: Tue, 24 May 2022 00:08:55 +0800 Subject: [PATCH 08/16] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nullnull | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 nullnull diff --git a/nullnull b/nullnull new file mode 100644 index 0000000..e69de29 -- Gitee From 6eb62d2fa8e1b0afe07ebcfcbab763c064d6971b Mon Sep 17 00:00:00 2001 From: yuxiaoshuo Date: Tue, 24 May 2022 00:25:07 +0800 Subject: [PATCH 09/16] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86=E6=96=87?= =?UTF-8?q?=E4=BB=B6=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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 0c34b61..bcd62b8 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -33,7 +33,7 @@ public class Notepad implements ActionListener { private boolean flag = true;// 需要一个flag变量,判断当前文件是否存在源文件:true,有;false,没有 private String txt = "",tst,name; private String currentpath = null; - private File FILE; + private BufferedReader bu; //右键菜单 -- Gitee From 3015ec1b7828c67720e59ed60a58a6ccda04ad4c Mon Sep 17 00:00:00 2001 From: yuxiaoshuo Date: Tue, 24 May 2022 21:16:44 +0800 Subject: [PATCH 10/16] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86=E6=96=B0?= =?UTF-8?q?=E5=BB=BA=E3=80=81=E6=89=93=E5=BC=80=E3=80=81=E7=B2=98=E8=B4=B4?= =?UTF-8?q?=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 | 270 ++++++++++++++++---------------- 1 file changed, 135 insertions(+), 135 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index bcd62b8..41d4aaa 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -31,9 +31,9 @@ public class Notepad implements ActionListener { private JFileChooser filechooser; private FileDialog filedialog; private boolean flag = true;// 需要一个flag变量,判断当前文件是否存在源文件:true,有;false,没有 - private String txt = "",tst,name; + private String txt = "",tst,name,path; private String currentpath = null; - + private File file; private BufferedReader bu; //右键菜单 @@ -205,13 +205,13 @@ public class Notepad implements ActionListener { //设置快捷键 - menu1.setMnemonic('F'); //设置快捷键ALT+F + menu1.setMnemonic('F'); // 设置快捷键ALT+F menu2.setMnemonic('E'); menu3.setMnemonic('O'); menu4.setMnemonic('V'); menu5.setMnemonic('H'); - itemnew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,2)); //设置快捷键CTRL+N - itemwindow.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,3)); //设置快捷键CTRL+SHIFT+N + itemnew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,2)); // 设置快捷键CTRL+N + itemwindow.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,3)); // 设置快捷键CTRL+SHIFT+N itemopen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,2)); itemsave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,2)); itemsaveas.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,3)); @@ -219,7 +219,7 @@ public class Notepad implements ActionListener { itemshear.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,2)); itemcopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,2)); itempaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,2)); - itemdelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); //设置快捷键DELETE + itemdelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); // 设置快捷键DELETE itemselectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,2)); } @@ -227,203 +227,210 @@ public class Notepad implements ActionListener { public void actionPerformed(ActionEvent e) { String command=e.getActionCommand(); - //——"文件"菜单 - //"新建"功能 + // ——"文件"菜单 + // "新建"功能 if(command.equals("新建(N)")) { String content = new String(textarea.getText()); - if (flag&&(name!=null)) { // 如果有源文件 + if (currentpath!=null) { // 如果有源文件 if (tst.equals(content)) { // 判断源文件文本是否有改动(此为无改动) textarea.setText(""); - flag=false; + currentpath=null; + frame.setTitle("无标题 - 记事本"); } else { // 有改动 int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将不保存", "保存", - JOptionPane.YES_NO_CANCEL_OPTION);//添加确认提示框 + JOptionPane.YES_NO_CANCEL_OPTION);// 添加确认提示框 if (i == JOptionPane.OK_OPTION) { save(); textarea.setText(""); - flag=false; + currentpath=null; + frame.setTitle("无标题 - 记事本"); } else if (i == JOptionPane.NO_OPTION) { textarea.setText(""); - flag=false; + currentpath=null; + frame.setTitle("无标题 - 记事本"); } } } else { // 无源文件 if (txt.equals(content)) { // 判断文本是否有改动 textarea.setText(""); - flag=false; + currentpath=null; + frame.setTitle("无标题 - 记事本"); } else { // 有改动 int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将不保存", "保存", JOptionPane.YES_NO_CANCEL_OPTION); if (i == JOptionPane.OK_OPTION) { - save(); + saveAs(); textarea.setText(""); - flag=false; + currentpath=null; + frame.setTitle("无标题 - 记事本"); } else if (i == JOptionPane.NO_OPTION) { textarea.setText(""); - flag=false; + currentpath=null; + frame.setTitle("无标题 - 记事本"); } } } } - //"新窗口"功能 + // "新窗口"功能 else if(command.equals("新窗口(W)")) { Notepad note=new Notepad(); } - //"打开"功能 + // "打开"功能 else if(command.equals("打开(O)")) { String content = new String(textarea.getText()); - if (flag&&(name!=null)) { // 如果有源文件 + if (currentpath!=null) { // 如果有源文件 if (tst.equals(content)) { // 判断文本是否有改动(此为无改动) open(); - flag=true; } else { // 有改动 int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将不保存", "保存", - JOptionPane.YES_NO_CANCEL_OPTION);//添加确认提示框 + JOptionPane.YES_NO_CANCEL_OPTION);// 添加确认提示框 if (i == JOptionPane.OK_OPTION) { save(); open(); - flag=true; } else if (i == JOptionPane.NO_OPTION) { open(); - flag=true; } } } else { // 无源文件 - if (txt.equals(content)) { // 判断文本是否有改动 + if (txt.equals(content)) { // 判断文本是否有改动(此为无改动) open(); - flag=true; } else { // 有改动 int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将不保存", "保存", JOptionPane.YES_NO_CANCEL_OPTION); if (i == JOptionPane.OK_OPTION) { - save(); + saveAs(); open(); - flag=true; } else if (i == JOptionPane.NO_OPTION) { - open(); - flag=true; + open(); } } } } - //"保存"功能 + // "保存"功能 else if(command.equals("保存(S)")) { save(); - flag=true; + } - //"另存为"功能 + // "另存为"功能 else if(command.equals("另存为(A)")) { saveAs(); - flag=true; + } - //"退出"功能 + // "退出"功能 else if(command.equals("退出(X)")) { String content = new String(textarea.getText()); - if (flag) { // 如果有源文件 - if (txt.equals(content)) { // 判断文本是否有改动(此为无改动) - flag=false; + if (currentpath!=null) { // 如果有源文件 + if (tst.equals(content)) { // 判断文本是否有改动(此为无改动) System.exit(0); } else { // 有改动 int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", - JOptionPane.YES_NO_CANCEL_OPTION);//添加确认提示框 - if (i == JOptionPane.OK_OPTION) { - flag=false; + JOptionPane.YES_NO_CANCEL_OPTION);// 添加确认提示框 + if (i == JOptionPane.OK_OPTION) { save(); System.exit(0); } - else if (i == JOptionPane.NO_OPTION) { - flag=false; + else if (i == JOptionPane.NO_OPTION) { System.exit(0); } } } else { // 无源文件 - if (txt.equals(content)) { // 判断文本是否有改动 - flag=false; + if (txt.equals(content)) { // 判断文本是否有改动 System.exit(0); } else { // 有改动 int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", JOptionPane.YES_NO_CANCEL_OPTION); - if (i == JOptionPane.OK_OPTION) { - flag=false; - save(); + if (i == JOptionPane.OK_OPTION) { + saveAs(); System.exit(0); } - else if (i == JOptionPane.NO_OPTION) { - flag=false; + else if (i == JOptionPane.NO_OPTION) { System.exit(0); } } } } - //——"编辑"菜单 - //"撤销"功能 + // ——"编辑"菜单 + // "撤销"功能 else if(command.equals("撤销(U)")) { if (undomanager.canUndo()) { undomanager.undo(); } } - //"剪切"功能 + // "剪切"功能 else if(command.equals("剪切(T)")) { String temp = textarea.getSelectedText(); - StringSelection selectedtxt = new StringSelection(temp); //传送到字符串里存着 + StringSelection selectedtxt = new StringSelection(temp); // 传送到字符串里存着 clipboard.setContents(selectedtxt, null); - textarea.replaceRange("", textarea.getSelectionStart(), textarea.getSelectionEnd()); //将选中文本清空 + textarea.replaceRange("", textarea.getSelectionStart(), textarea.getSelectionEnd()); // 将选中文本清空 } - //"复制"功能 + // "复制"功能 else if(command.equals("复制(C)")) { String temp = textarea.getSelectedText(); - StringSelection selectedtxt = new StringSelection(temp); //传送到字符串里存着 + StringSelection selectedtxt = new StringSelection(temp); // 传送到字符串里存着 clipboard.setContents(selectedtxt, null); } - //"粘贴"功能(该部分代码有借鉴) + // "粘贴"功能(该部分代码有借鉴) else if(command.equals("粘贴(P)")) { - Transferable temp = clipboard.getContents(null); //获取系统剪贴板中的内容 - if (temp.isDataFlavorSupported(DataFlavor.stringFlavor)) { //判断剪贴板中的内容是否支持文本 - try { - String text = (String) temp.getTransferData(DataFlavor.stringFlavor); //强制转换剪贴板中的内容 - int n=textarea.getCaretPosition(); //获取当前光标的位置 - textarea.insert(text,n); //插入复制的内容到文本框的光标后面 - } - catch (Exception e1) { - e1.printStackTrace(); - } + Transferable temp = clipboard.getContents(null); // 获取系统剪贴板中的内容 + if (temp.isDataFlavorSupported(DataFlavor.stringFlavor)) { // 判断剪贴板中的内容是否支持文本 + if(textarea.getSelectedText()==null) {// 如果没有选中文本 + try { + String text = (String) temp.getTransferData(DataFlavor.stringFlavor); // 强制转换剪贴板中的内容 + int n=textarea.getCaretPosition(); // 获取当前光标的位置 + textarea.insert(text,n); // 插入复制的内容到文本框的光标后面 + } + catch (Exception e1) { + e1.printStackTrace(); + } + } + else {// 有选中文本 + textarea.replaceRange("", textarea.getSelectionStart(), textarea.getSelectionEnd()); // 将选中文本清空 + try { + String text = (String) temp.getTransferData(DataFlavor.stringFlavor); // 强制转换剪贴板中的内容 + int n=textarea.getCaretPosition(); // 获取当前光标的位置 + textarea.insert(text,n); // 插入复制的内容到文本框的光标后面 + } + catch (Exception e1) { + e1.printStackTrace(); + } + } } } - //"删除"功能 + // "删除"功能 else if(command.equals("删除(L)")) { textarea.replaceRange("", textarea.getSelectionStart(), textarea.getSelectionEnd()); //将选中文本清空 } - //"全选"功能 + // "全选"功能 else if(command.equals("全选(A)")) { textarea.selectAll(); } - //"格式"菜单 - //"自动换行"功能 + // "格式"菜单 + // "自动换行"功能 else if(command.equals("自动换行(W)")) { if(itemautowrap.isSelected()) { textarea.setLineWrap(true); @@ -433,8 +440,8 @@ public class Notepad implements ActionListener { } } - //——"查看"菜单 - //"状态栏"功能 + // ——"查看"菜单 + // "状态栏"功能 else if(command.equals("状态栏(S)")) { if (itemstatusbar.isSelected()) { toolstate.setVisible(true); @@ -444,35 +451,21 @@ public class Notepad implements ActionListener { } } - //——"帮助"菜单 - //"查看帮助"功能 + // ——"帮助"菜单 + // "查看帮助"功能 else if(command.equals("查看帮助(H)")) { JOptionPane.showMessageDialog(frame,"请查看百度!","查看帮助",1); } - //"关于记事本"功能 + // "关于记事本"功能 else if(command.equals("关于记事本(A)")) { JOptionPane.showMessageDialog(frame,"记事本\n作者:yuxiaoshuo\n我的邮箱:yxsthebest@qq.com","关于记事本",1); } } - - // 设置文件过滤器(还没好) - private void setFileFilter() { - FilenameFilter filter = new FilenameFilter(){ - public boolean accept(File dir, String name) { - if (name.endsWith("txt")){ - return true; - } - return false; - } - }; - filedialog.setFilenameFilter(filter); - filedialog.setVisible(true); - } - //关闭窗口时 + // 关闭窗口时 public void Exit() { String content = new String(textarea.getText()); if (flag) { // 如果有源文件 @@ -511,32 +504,34 @@ public class Notepad implements ActionListener { //打开文件 private void open() { - FileDialog filedialog = new FileDialog(frame, "请选择文件", FileDialog.LOAD); + FileDialog filedialog = new FileDialog(frame, "请选择文件", FileDialog.LOAD);//设置文件打开框 filedialog.setVisible(true); //保存文件的对话框设置为可见 - String abspath = filedialog.getDirectory() + filedialog.getFile();//获取选中文件的路径 - currentpath = filedialog.getDirectory() + filedialog.getFile(); name=filedialog.getFile(); - frame.setTitle(name+" - 记事本"); - if(name==null) + path=filedialog.getDirectory(); + if(name==null||path==null) return; - try { - bu = new BufferedReader(new FileReader(abspath)); - String str=""; - String allcontent=""; - while((str=bu.readLine())!=null) - allcontent+= str; - textarea.setText(allcontent); - tst=allcontent; - } - catch (Exception e2) { - e2.printStackTrace(); - } - finally { + if(name!=null&&path!=null) { + file=new File(path,name); + currentpath = file.getName() + file.getAbsolutePath();//获取选中文件的路径 + try { - bu.close(); + bu = new BufferedReader(new FileReader(file)); + String str=""; + while((str=bu.readLine())!=null) + textarea.append(str); + tst=textarea.getText(); + frame.setTitle(name+" - 记事本"); + } + catch (Exception e2) { + e2.printStackTrace(); } - catch(IOException e3) { - e3.printStackTrace(); + finally { + try { + bu.close(); + } + catch(IOException e3) { + e3.printStackTrace(); + } } } } @@ -545,57 +540,62 @@ public class Notepad implements ActionListener { //文件另存为 private void saveAs() { - FileDialog filedialog = new FileDialog(frame, "保存文件至", FileDialog.SAVE); + FileDialog filedialog = new FileDialog(frame, "保存文件至", FileDialog.SAVE);//设置文件保存框 filedialog.setVisible(true); //保存文件的对话框设置为可见 - String abspath = filedialog.getDirectory() + filedialog.getFile(); //获取保存的路径和设置的文件名 name=filedialog.getFile(); - frame.setTitle(name+" - 记事本"); - if(name==null)//若无选择文件,则返回原先窗口 + path=filedialog.getDirectory(); + if(name==null||path==null)//若关闭保存框,则返回原先窗口 return; - currentpath = filedialog.getDirectory() + filedialog.getFile(); - tst=textarea.getText(); - try {//用来检测是否设置了保存路径 - BufferedWriter wr = new BufferedWriter(new FileWriter(abspath)); //设置输出文件名为保存的路径下面的文件名 - String s = textarea.getText(); //获取文本域字符串 - wr.write(s); //开始写入 - wr.close(); //写完关闭 - } - catch (IOException e) { - e.printStackTrace(); + if(name!=null||path!=null) { + file=new File(path,name); + currentpath = file.getName() + file.getAbsolutePath();//获取选中文件的路径 + try { + BufferedWriter wr = new BufferedWriter(new FileWriter(file)); + String s = textarea.getText(); //获取文本域字符串 + wr.write(s); //开始写入 + wr.close(); //写完关闭 + tst=textarea.getText(); + frame.setTitle(name+" - 记事本"); + } + catch (IOException e) { + e.printStackTrace(); + } } } //文件保存 private void save() { - if(name==null) { + if(currentpath==null) { this.saveAs(); - if(name==null){ + if(name==null||path==null){ return; } } else { try { - BufferedWriter wr = new BufferedWriter(new FileWriter(currentpath)); + BufferedWriter wr = new BufferedWriter(new FileWriter(file)); String s = textarea.getText(); wr.write(s); wr.close(); + tst=textarea.getText(); } catch (IOException e) { e.printStackTrace(); } - tst=textarea.getText(); + } } - public static void main(String[] args) { - Notepad window=new Notepad(); + public static void main(String[] args) { //生成Windows风格的UI界面 (此代码为借鉴) try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException + } + catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { e.printStackTrace(); - } + }; + Notepad window=new Notepad(); } } -- Gitee From b303f8653ef52a5384b71d4c3b97f4448b8ea064 Mon Sep 17 00:00:00 2001 From: yuxiaoshuo Date: Wed, 25 May 2022 14:40:35 +0800 Subject: [PATCH 11/16] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E5=85=B3=E9=97=AD=E7=9A=84=E5=88=A4=E6=96=AD=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=9F=BA=E6=9C=AC=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E4=BA=86=E8=AE=B0=E4=BA=8B=E6=9C=AC=E7=9A=84=E5=BC=80?= =?UTF-8?q?=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 94 +++++++++++++-------------------- 1 file changed, 36 insertions(+), 58 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 41d4aaa..66def27 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -28,9 +28,7 @@ public class Notepad implements ActionListener { private JCheckBoxMenuItem itemautowrap,itemstatusbar; private JToolBar toolstate; private JLabel label1,label2; - private JFileChooser filechooser; private FileDialog filedialog; - private boolean flag = true;// 需要一个flag变量,判断当前文件是否存在源文件:true,有;false,没有 private String txt = "",tst,name,path; private String currentpath = null; private File file; @@ -98,11 +96,13 @@ public class Notepad implements ActionListener { menu5.add(itemhelp); menu5.addSeparator(); menu5.add(itemabout); + menubar.add(menu1); menubar.add(menu2); menubar.add(menu3); menubar.add(menu4); menubar.add(menu5); + frame.setJMenuBar(menubar); frame.add(pane); @@ -120,7 +120,7 @@ public class Notepad implements ActionListener { frame.setBounds(20,30,900,550); frame.setLocationRelativeTo(null);//设置窗口居中显示 frame.setVisible(true); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); textarea.setFont(new Font("宋体",Font.PLAIN,20)); menubar.setBackground(Color.white); add(); @@ -131,14 +131,15 @@ public class Notepad implements ActionListener { toolstate.setBackground(Color.LIGHT_GRAY); toolstate.add(label1 = new JLabel(" 第"+line+"行,第"+col+"列 ")); toolstate.add(label2 = new JLabel(" 一共"+sum+"字")); - frame.add(toolstate,BorderLayout.SOUTH); - toolstate.setVisible(true); - toolstate.setFloatable(false); + frame.add(toolstate,BorderLayout.SOUTH); //将状态栏加到窗口的最下方 + toolstate.setVisible(true); //状态栏设为可见 + toolstate.setFloatable(false); //状态栏设为不可移动 } public void add() { + //添加菜单项监听器 itemnew.addActionListener(this); itemwindow.addActionListener(this); @@ -279,7 +280,12 @@ public class Notepad implements ActionListener { // "新窗口"功能 else if(command.equals("新窗口(W)")) { - Notepad note=new Notepad(); + Notepad window=new Notepad(); + window.frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + window.Exit(); + } + }); } // "打开"功能 @@ -321,51 +327,17 @@ public class Notepad implements ActionListener { // "保存"功能 else if(command.equals("保存(S)")) { - save(); - + save(); } // "另存为"功能 else if(command.equals("另存为(A)")) { - saveAs(); - + saveAs(); } // "退出"功能 else if(command.equals("退出(X)")) { - String content = new String(textarea.getText()); - if (currentpath!=null) { // 如果有源文件 - if (tst.equals(content)) { // 判断文本是否有改动(此为无改动) - System.exit(0); - } - else { // 有改动 - int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", - JOptionPane.YES_NO_CANCEL_OPTION);// 添加确认提示框 - if (i == JOptionPane.OK_OPTION) { - save(); - System.exit(0); - } - else if (i == JOptionPane.NO_OPTION) { - System.exit(0); - } - } - } - else { // 无源文件 - if (txt.equals(content)) { // 判断文本是否有改动 - System.exit(0); - } - else { // 有改动 - int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", - JOptionPane.YES_NO_CANCEL_OPTION); - if (i == JOptionPane.OK_OPTION) { - saveAs(); - System.exit(0); - } - else if (i == JOptionPane.NO_OPTION) { - System.exit(0); - } - } - } + Exit(); } // ——"编辑"菜单 @@ -468,15 +440,16 @@ public class Notepad implements ActionListener { // 关闭窗口时 public void Exit() { String content = new String(textarea.getText()); - if (flag) { // 如果有源文件 - if (txt.equals(content)) { // 判断文本是否有改动(此为无改动) + if (currentpath!=null) { // 如果有源文件 + if (tst.equals(content)) { // 与旧文本相比-判断文本是否有改动(此为无改动) System.exit(0); } else { // 有改动 - int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将不保存", "保存", JOptionPane.YES_NO_CANCEL_OPTION);//添加确认提示框 if (i == JOptionPane.OK_OPTION) { save(); + System.exit(0); } else if (i == JOptionPane.NO_OPTION) { System.exit(0); @@ -484,14 +457,15 @@ public class Notepad implements ActionListener { } } else { // 无源文件 - if (txt.equals(content)) { // 判断文本是否有改动 + if (txt.equals(content)) { // 与空文本相比-判断文本是否有改动(此为无改动) System.exit(0); } else { // 有改动 - int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将直接退出", "保存", + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将不保存", "保存", JOptionPane.YES_NO_CANCEL_OPTION); if (i == JOptionPane.OK_OPTION) { - save(); + saveAs(); + System.exit(0); } else if (i == JOptionPane.NO_OPTION) { System.exit(0); @@ -501,25 +475,24 @@ public class Notepad implements ActionListener { } - //打开文件 private void open() { FileDialog filedialog = new FileDialog(frame, "请选择文件", FileDialog.LOAD);//设置文件打开框 - filedialog.setVisible(true); //保存文件的对话框设置为可见 + filedialog.setVisible(true); //打开文件的对话框设置为可见 name=filedialog.getFile(); path=filedialog.getDirectory(); - if(name==null||path==null) - return; - if(name!=null&&path!=null) { + if(name==null||path==null)//若没有选中文件,则返回原来窗口 + return; + if(name!=null&&path!=null) { // 选中了文件 file=new File(path,name); currentpath = file.getName() + file.getAbsolutePath();//获取选中文件的路径 - + //读取文件 try { bu = new BufferedReader(new FileReader(file)); String str=""; while((str=bu.readLine())!=null) textarea.append(str); - tst=textarea.getText(); + tst=textarea.getText(); // 获取打开文件的内容 frame.setTitle(name+" - 记事本"); } catch (Exception e2) { @@ -554,7 +527,7 @@ public class Notepad implements ActionListener { String s = textarea.getText(); //获取文本域字符串 wr.write(s); //开始写入 wr.close(); //写完关闭 - tst=textarea.getText(); + tst=textarea.getText(); //获取保存文件的内容 frame.setTitle(name+" - 记事本"); } catch (IOException e) { @@ -597,5 +570,10 @@ public class Notepad implements ActionListener { e.printStackTrace(); }; Notepad window=new Notepad(); + window.frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + window.Exit(); + } + }); } } -- Gitee From 11e18c678e0f34adf4939759ef7efb799db42a01 Mon Sep 17 00:00:00 2001 From: yuxiaoshuo Date: Thu, 26 May 2022 13:36:35 +0800 Subject: [PATCH 12/16] =?UTF-8?q?=E5=8F=91=E7=8E=B0=E6=89=93=E5=BC=80?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=8A=9F=E8=83=BD=E6=9C=89=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E5=B9=B6=E4=BF=AE=E6=94=B9=E5=A5=BD=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 66def27..4730d34 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -255,7 +255,7 @@ public class Notepad implements ActionListener { } } else { // 无源文件 - if (txt.equals(content)) { // 判断文本是否有改动 + if (txt.equals(content)) { // 判断文本是否有改动(此为无改动) textarea.setText(""); currentpath=null; frame.setTitle("无标题 - 记事本"); @@ -292,7 +292,7 @@ public class Notepad implements ActionListener { else if(command.equals("打开(O)")) { String content = new String(textarea.getText()); if (currentpath!=null) { // 如果有源文件 - if (tst.equals(content)) { // 判断文本是否有改动(此为无改动) + if (tst.equals(content)) { // 判断源文件文本是否有改动(此为无改动) open(); } else { // 有改动 @@ -441,7 +441,7 @@ public class Notepad implements ActionListener { public void Exit() { String content = new String(textarea.getText()); if (currentpath!=null) { // 如果有源文件 - if (tst.equals(content)) { // 与旧文本相比-判断文本是否有改动(此为无改动) + if (tst.equals(content)) { // 判断源文件文本是否有改动(此为无改动) System.exit(0); } else { // 有改动 @@ -457,7 +457,7 @@ public class Notepad implements ActionListener { } } else { // 无源文件 - if (txt.equals(content)) { // 与空文本相比-判断文本是否有改动(此为无改动) + if (txt.equals(content)) { // 判断文本是否有改动(此为无改动) System.exit(0); } else { // 有改动 @@ -490,8 +490,10 @@ public class Notepad implements ActionListener { try { bu = new BufferedReader(new FileReader(file)); String str=""; + String all=""; while((str=bu.readLine())!=null) - textarea.append(str); + all+=str; + textarea.setText(all); tst=textarea.getText(); // 获取打开文件的内容 frame.setTitle(name+" - 记事本"); } -- Gitee From 2e8a51fefe3bcf1c65ba5dd778dd9c1a3b1cfedc Mon Sep 17 00:00:00 2001 From: yuxiaoshuo Date: Sat, 28 May 2022 21:59:04 +0800 Subject: [PATCH 13/16] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E7=BC=A9?= =?UTF-8?q?=E6=94=BE=E5=8A=9F=E8=83=BD=E5=92=8C=E6=9F=A5=E6=89=BE=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=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 | 266 +++++++++++++++++++++++++++++++- 1 file changed, 258 insertions(+), 8 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 4730d34..67472c5 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -23,8 +23,9 @@ public class Notepad implements ActionListener { private Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //调用系统剪贴板方法 private UndoManager undomanager = new UndoManager(); private JMenuBar menubar; - private JMenu menu1,menu2,menu3,menu4,menu5; - private JMenuItem itemnew,itemwindow,itemopen,itemsave,itemsaveas,itemexit,itemrevoke,itemshear,itemcopy,itempaste,itemdelete,itemselectall,itemhelp,itemabout; + private JMenu menu1,menu2,menu3,menu4,menu5,menu6; + private JMenuItem itemnew,itemwindow,itemopen,itemsave,itemsaveas,itemexit,itemrevoke,itemrecovery,itemshear,itemcopy, + itempaste,itemdelete,itemsearch,itemreplace,itemselectall,itemhelp,itemabout,itemlarge,itemsmall,itemre; private JCheckBoxMenuItem itemautowrap,itemstatusbar; private JToolBar toolstate; private JLabel label1,label2; @@ -36,11 +37,13 @@ public class Notepad implements ActionListener { //右键菜单 JPopupMenu popupmenu; - JMenuItem poprevoke,popshear,popcopy,poppaste,popdelete,popselectall; + JMenuItem poprevoke,poprecovery,popshear,popcopy,poppaste,popdelete,popselectall; int line=1;//文本行数 int col=1;//文本列数 int sum=0; + int i=0;//用于缩放文本 + public Notepad() { //创建菜单 menubar=new JMenuBar(); @@ -49,6 +52,7 @@ public class Notepad implements ActionListener { menu3=new JMenu("格式(O)"); menu4=new JMenu("查看(V)"); menu5=new JMenu("帮助(H)"); + menu6=new JMenu("缩放(Z)"); itemnew=new JMenuItem("新建(N)"); itemwindow=new JMenuItem("新窗口(W)"); itemopen=new JMenuItem("打开(O)"); @@ -56,19 +60,27 @@ public class Notepad implements ActionListener { itemsaveas=new JMenuItem("另存为(A)"); itemexit=new JMenuItem("退出(X)"); itemrevoke=new JMenuItem("撤销(U)"); + itemrecovery=new JMenuItem("恢复(R)"); itemshear=new JMenuItem("剪切(T)"); itemcopy=new JMenuItem("复制(C)"); itempaste=new JMenuItem("粘贴(P)"); itemdelete=new JMenuItem("删除(L)"); + itemsearch=new JMenuItem("查找(F)"); + itemreplace=new JMenuItem("替换(R)"); itemselectall=new JMenuItem("全选(A)"); itemautowrap=new JCheckBoxMenuItem("自动换行(W)", false); itemstatusbar=new JCheckBoxMenuItem("状态栏(S)", true); + itemlarge=new JMenuItem("放大(I)"); + itemsmall=new JMenuItem("缩小(O)"); + itemre=new JMenuItem("恢复默认缩放"); itemhelp=new JMenuItem("查看帮助(H)"); itemabout=new JMenuItem("关于记事本(A)"); + //创建右键弹出菜单 popupmenu=new JPopupMenu(); poprevoke=new JMenuItem("撤销(U)"); + poprecovery=new JMenuItem("恢复(R)"); popshear=new JMenuItem("剪切(T)"); popcopy=new JMenuItem("复制(C)"); poppaste=new JMenuItem("粘贴(P)"); @@ -84,14 +96,22 @@ public class Notepad implements ActionListener { menu1.addSeparator();//添加分割线 menu1.add(itemexit); menu2.add(itemrevoke); + menu2.add(itemrecovery); menu2.addSeparator(); menu2.add(itemshear); menu2.add(itemcopy); menu2.add(itempaste); menu2.add(itemdelete); menu2.addSeparator(); + menu2.add(itemsearch); + menu2.add(itemreplace); + menu2.addSeparator(); menu2.add(itemselectall); menu3.add(itemautowrap); + menu6.add(itemlarge); + menu6.add(itemsmall); + menu6.add(itemre); + menu4.add(menu6); menu4.add(itemstatusbar); menu5.add(itemhelp); menu5.addSeparator(); @@ -108,6 +128,7 @@ public class Notepad implements ActionListener { //添加到右键菜单 popupmenu.add(poprevoke); + popupmenu.add(poprecovery); popupmenu.addSeparator(); popupmenu.add(popshear); popupmenu.add(popcopy); @@ -148,18 +169,25 @@ public class Notepad implements ActionListener { itemsaveas.addActionListener(this); itemexit.addActionListener(this); itemrevoke.addActionListener(this); + itemrecovery.addActionListener(this); itemshear.addActionListener(this); itemcopy.addActionListener(this); itempaste.addActionListener(this); itemdelete.addActionListener(this); + itemsearch.addActionListener(this); + itemreplace.addActionListener(this); itemselectall.addActionListener(this); itemautowrap.addActionListener(this); + itemlarge.addActionListener(this); + itemsmall.addActionListener(this); + itemre.addActionListener(this); itemstatusbar.addActionListener(this); itemhelp.addActionListener(this); itemabout.addActionListener(this); //添加右键菜单监听器 poprevoke.addActionListener(this); + poprecovery.addActionListener(this); popshear.addActionListener(this); popcopy.addActionListener(this); poppaste.addActionListener(this); @@ -189,17 +217,13 @@ public class Notepad implements ActionListener { textarea.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (e.isPopupTrigger())// 返回此鼠标事件是否为该平台的弹出菜单触发事件 - { popupmenu.show(e.getComponent(), e.getX(), e.getY());// 在组件调用者的坐标空间中的位置 X、Y 显示弹出菜单 - } textarea.requestFocus();// 编辑区获取焦点 } public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger())// 返回此鼠标事件是否为该平台的弹出菜单触发事件 - { popupmenu.show(e.getComponent(), e.getX(), e.getY());// 在组件调用者的坐标空间中的位置 X、Y 显示弹出菜单 - } textarea.requestFocus();// 编辑区获取焦点 } }); @@ -217,12 +241,17 @@ public class Notepad implements ActionListener { itemsave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,2)); itemsaveas.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,3)); itemrevoke.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,2)); + itemrecovery.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,2)); itemshear.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,2)); itemcopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,2)); itempaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,2)); itemdelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); // 设置快捷键DELETE + itemsearch.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,2)); + itemreplace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,2)); itemselectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,2)); - + itemlarge.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ADD,2)); + itemsmall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT,2)); + itemre.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0,2)); } public void actionPerformed(ActionEvent e) { @@ -348,6 +377,13 @@ public class Notepad implements ActionListener { } } + //"恢复"功能 + else if(command.equals("恢复(R)")) { + if (undomanager.canUndo()) { + undomanager.redo(); + } + } + // "剪切"功能 else if(command.equals("剪切(T)")) { String temp = textarea.getSelectedText(); @@ -396,6 +432,11 @@ public class Notepad implements ActionListener { textarea.replaceRange("", textarea.getSelectionStart(), textarea.getSelectionEnd()); //将选中文本清空 } + //"查找"与"替换"功能 + else if(command.equals("查找(F)")||command.equals("替换(R)")) { + Search(); + } + // "全选"功能 else if(command.equals("全选(A)")) { textarea.selectAll(); @@ -413,6 +454,20 @@ public class Notepad implements ActionListener { } // ——"查看"菜单 + // "缩放"功能 + else if(command.equals("放大(I)")) { + i=i+2; + textarea.setFont(new Font("宋体",Font.PLAIN,20+i)); + } + else if(command.equals("缩小(O)")) { + i=i-2; + textarea.setFont(new Font("宋体",Font.PLAIN,20+i)); + } + else if(command.equals("恢复默认缩放")) { + textarea.setFont(new Font("宋体",Font.PLAIN,20)); + } + + // "状态栏"功能 else if(command.equals("状态栏(S)")) { if (itemstatusbar.isSelected()) { @@ -561,6 +616,201 @@ public class Notepad implements ActionListener { } } + + //查找与替换(此部分代码有借鉴!!) + public void Search() { + JDialog findDialog = new JDialog(frame, "替换", true); + Container con = findDialog.getContentPane(); + con.setLayout(new FlowLayout(FlowLayout.LEFT)); + JLabel searchlabel = new JLabel("查找内容(N) :"); + JLabel replacelabel = new JLabel("替换为(P)  :"); + JTextField findText = new JTextField(20); + JTextField replaceText = new JTextField(20); + JCheckBox matchcase = new JCheckBox("区分大小写"); + ButtonGroup bGroup = new ButtonGroup(); + JRadioButton up = new JRadioButton("向上(U)"); + JRadioButton down = new JRadioButton("向下(D)"); + down.setSelected(true); + bGroup.add(up); + bGroup.add(down); + JButton searchNext = new JButton("查找下一个(F)"); + JButton replace = new JButton("替换(R)"); + JButton replaceAll = new JButton("全部替换(A)"); + searchNext.setPreferredSize(new Dimension(110, 22)); + replace.setPreferredSize(new Dimension(110, 22)); + replaceAll.setPreferredSize(new Dimension(110, 22)); + // "替换"按钮的事件处理 + replace.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (replaceText.getText().length() == 0 && textarea.getSelectedText() != null) + textarea.replaceSelection(""); + if (replaceText.getText().length() > 0 && textarea.getSelectedText() != null) + textarea.replaceSelection(replaceText.getText()); + } + }); + + // "替换全部"按钮的事件处理 + replaceAll.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + textarea.setCaretPosition(0); // 将光标放到编辑区开头 + int a = 0, b = 0, replaceCount = 0; + if (findText.getText().length() == 0) { + JOptionPane.showMessageDialog(findDialog, "请填写查找内容!", "提示", JOptionPane.WARNING_MESSAGE); + findText.requestFocus(true); + return; + } + while (a > -1) { + int FindStartPos = textarea.getCaretPosition(); + String str1, str2, str3, str4, strA, strB; + str1 = textarea.getText(); + str2 = str1.toLowerCase(); + str3 = findText.getText(); + str4 = str3.toLowerCase(); + if (matchcase.isSelected()) { + strA = str1; + strB = str3; + } + else { + strA = str2; + strB = str4; + } + + if (up.isSelected()) { + if (textarea.getSelectedText() == null) + a = strA.lastIndexOf(strB, FindStartPos - 1); + else + a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1); + } else if (down.isSelected()) { + if (textarea.getSelectedText() == null) + a = strA.indexOf(strB, FindStartPos); + else + a = strA.indexOf(strB, FindStartPos - findText.getText().length() + 1); + } + + if (a > -1) { + if (up.isSelected()) { + textarea.setCaretPosition(a); + b = findText.getText().length(); + textarea.select(a, a + b); + } + else if (down.isSelected()) { + textarea.setCaretPosition(a); + b = findText.getText().length(); + textarea.select(a, a + b); + } + } + else { + if (replaceCount == 0) + JOptionPane.showMessageDialog(findDialog, "找不到您查找的内容!", "记事本", JOptionPane.INFORMATION_MESSAGE); + else + JOptionPane.showMessageDialog(findDialog, "成功替换" + replaceCount + "个匹配内容!", "替换成功", JOptionPane.INFORMATION_MESSAGE); + } + if (replaceText.getText().length() == 0 && textarea.getSelectedText() != null) { + textarea.replaceSelection(""); + replaceCount++; + } + if (replaceText.getText().length() > 0 && textarea.getSelectedText() != null) { + textarea.replaceSelection(replaceText.getText()); + replaceCount++; + } + }// end while + } + }); + + // "查找下一个"按钮事件处理 + searchNext.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + int a = 0, b = 0; + int FindStartPos = textarea.getCaretPosition(); + String str1, str2, str3, str4, strA, strB; + str1 = textarea.getText(); + str2 = str1.toLowerCase(); + str3 = findText.getText(); + str4 = str3.toLowerCase(); + // "区分大小写"的CheckBox被选中 + if (matchcase.isSelected()) { + strA = str1; + strB = str3; + } else { + strA = str2; + strB = str4; + } + + if (up.isSelected()) { + if (textarea.getSelectedText() == null) { + a = strA.lastIndexOf(strB, FindStartPos - 1); + } else { + a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1); + } + } else if (down.isSelected()) { + if (textarea.getSelectedText() == null) + a = strA.indexOf(strB, FindStartPos); + else + a = strA.indexOf(strB, FindStartPos - findText.getText().length() + 1); + } + if (a > -1) { + if (up.isSelected()) { + textarea.setCaretPosition(a); + b = findText.getText().length(); + textarea.select(a, a + b); + } + else if (down.isSelected()) { + textarea.setCaretPosition(a); + b = findText.getText().length(); + textarea.select(a, a + b); + } + } + else + JOptionPane.showMessageDialog(null, "找不到您查找的内容!", "记事本", JOptionPane.INFORMATION_MESSAGE); + } + }); + + // "取消"按钮及事件处理 + JButton cancel = new JButton("取消"); + cancel.setPreferredSize(new Dimension(110, 22)); + cancel.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + findDialog.dispose(); + } + }); + + // 创建对话框的界面 + JPanel direction = new JPanel(); + direction.setBorder(BorderFactory.createTitledBorder("方向 ")); + direction.add(up); + direction.add(down); + direction.setPreferredSize(new Dimension(170, 60)); + + JPanel replacePanel = new JPanel(); + replacePanel.setLayout(new GridLayout(2, 1)); + replacePanel.add(replace); + replacePanel.add(replaceAll); + + JPanel toppanel = new JPanel(); + JPanel centerpanel = new JPanel(); + JPanel bottompanel = new JPanel(); + toppanel.add(searchlabel); + toppanel.add(findText); + toppanel.add(searchNext); + centerpanel.add(replacelabel); + centerpanel.add(replaceText); + centerpanel.add(replacePanel); + bottompanel.add(matchcase); + bottompanel.add(direction); + bottompanel.add(cancel); + + con.add(toppanel); + con.add(centerpanel); + con.add(bottompanel); + + // 设置替换对话框的大小、可更改大小(否)、位置和可见性 + findDialog.setSize(410, 210); + findDialog.setResizable(false); + findDialog.setLocationRelativeTo(null);//设置窗口居中 + findDialog.setVisible(true); + } + + public static void main(String[] args) { //生成Windows风格的UI界面 (此代码为借鉴) -- Gitee From 7a52aa3569dbfab93b8f7366cdf21ba19120d52d Mon Sep 17 00:00:00 2001 From: yuxiaoshuo Date: Sat, 28 May 2022 23:18:07 +0800 Subject: [PATCH 14/16] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=86=E6=81=A2?= =?UTF-8?q?=E5=A4=8D=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 | 143 +++++--------------------------- 1 file changed, 23 insertions(+), 120 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index 67472c5..f9f6b34 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -25,7 +25,7 @@ public class Notepad implements ActionListener { private JMenuBar menubar; private JMenu menu1,menu2,menu3,menu4,menu5,menu6; private JMenuItem itemnew,itemwindow,itemopen,itemsave,itemsaveas,itemexit,itemrevoke,itemrecovery,itemshear,itemcopy, - itempaste,itemdelete,itemsearch,itemreplace,itemselectall,itemhelp,itemabout,itemlarge,itemsmall,itemre; + itempaste,itemdelete,itemsearch,itemselectall,itemhelp,itemabout,itemlarge,itemsmall,itemre; private JCheckBoxMenuItem itemautowrap,itemstatusbar; private JToolBar toolstate; private JLabel label1,label2; @@ -66,7 +66,6 @@ public class Notepad implements ActionListener { itempaste=new JMenuItem("粘贴(P)"); itemdelete=new JMenuItem("删除(L)"); itemsearch=new JMenuItem("查找(F)"); - itemreplace=new JMenuItem("替换(R)"); itemselectall=new JMenuItem("全选(A)"); itemautowrap=new JCheckBoxMenuItem("自动换行(W)", false); itemstatusbar=new JCheckBoxMenuItem("状态栏(S)", true); @@ -104,7 +103,6 @@ public class Notepad implements ActionListener { menu2.add(itemdelete); menu2.addSeparator(); menu2.add(itemsearch); - menu2.add(itemreplace); menu2.addSeparator(); menu2.add(itemselectall); menu3.add(itemautowrap); @@ -175,7 +173,6 @@ public class Notepad implements ActionListener { itempaste.addActionListener(this); itemdelete.addActionListener(this); itemsearch.addActionListener(this); - itemreplace.addActionListener(this); itemselectall.addActionListener(this); itemautowrap.addActionListener(this); itemlarge.addActionListener(this); @@ -247,7 +244,6 @@ public class Notepad implements ActionListener { itempaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,2)); itemdelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); // 设置快捷键DELETE itemsearch.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,2)); - itemreplace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,2)); itemselectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,2)); itemlarge.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ADD,2)); itemsmall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT,2)); @@ -432,8 +428,8 @@ public class Notepad implements ActionListener { textarea.replaceRange("", textarea.getSelectionStart(), textarea.getSelectionEnd()); //将选中文本清空 } - //"查找"与"替换"功能 - else if(command.equals("查找(F)")||command.equals("替换(R)")) { + //"查找"功能 + else if(command.equals("查找(F)")) { Search(); } @@ -617,105 +613,22 @@ public class Notepad implements ActionListener { } - //查找与替换(此部分代码有借鉴!!) + //查找(此部分代码有借鉴) public void Search() { - JDialog findDialog = new JDialog(frame, "替换", true); - Container con = findDialog.getContentPane(); - con.setLayout(new FlowLayout(FlowLayout.LEFT)); - JLabel searchlabel = new JLabel("查找内容(N) :"); - JLabel replacelabel = new JLabel("替换为(P)  :"); + JDialog findDialog = new JDialog(frame, "查找", true); + JLabel searchlabel = new JLabel("查找内容(N) :"); JTextField findText = new JTextField(20); - JTextField replaceText = new JTextField(20); JCheckBox matchcase = new JCheckBox("区分大小写"); - ButtonGroup bGroup = new ButtonGroup(); JRadioButton up = new JRadioButton("向上(U)"); JRadioButton down = new JRadioButton("向下(D)"); - down.setSelected(true); - bGroup.add(up); - bGroup.add(down); JButton searchNext = new JButton("查找下一个(F)"); - JButton replace = new JButton("替换(R)"); - JButton replaceAll = new JButton("全部替换(A)"); - searchNext.setPreferredSize(new Dimension(110, 22)); - replace.setPreferredSize(new Dimension(110, 22)); - replaceAll.setPreferredSize(new Dimension(110, 22)); - // "替换"按钮的事件处理 - replace.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - if (replaceText.getText().length() == 0 && textarea.getSelectedText() != null) - textarea.replaceSelection(""); - if (replaceText.getText().length() > 0 && textarea.getSelectedText() != null) - textarea.replaceSelection(replaceText.getText()); - } - }); - - // "替换全部"按钮的事件处理 - replaceAll.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - textarea.setCaretPosition(0); // 将光标放到编辑区开头 - int a = 0, b = 0, replaceCount = 0; - if (findText.getText().length() == 0) { - JOptionPane.showMessageDialog(findDialog, "请填写查找内容!", "提示", JOptionPane.WARNING_MESSAGE); - findText.requestFocus(true); - return; - } - while (a > -1) { - int FindStartPos = textarea.getCaretPosition(); - String str1, str2, str3, str4, strA, strB; - str1 = textarea.getText(); - str2 = str1.toLowerCase(); - str3 = findText.getText(); - str4 = str3.toLowerCase(); - if (matchcase.isSelected()) { - strA = str1; - strB = str3; - } - else { - strA = str2; - strB = str4; - } - - if (up.isSelected()) { - if (textarea.getSelectedText() == null) - a = strA.lastIndexOf(strB, FindStartPos - 1); - else - a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1); - } else if (down.isSelected()) { - if (textarea.getSelectedText() == null) - a = strA.indexOf(strB, FindStartPos); - else - a = strA.indexOf(strB, FindStartPos - findText.getText().length() + 1); - } - - if (a > -1) { - if (up.isSelected()) { - textarea.setCaretPosition(a); - b = findText.getText().length(); - textarea.select(a, a + b); - } - else if (down.isSelected()) { - textarea.setCaretPosition(a); - b = findText.getText().length(); - textarea.select(a, a + b); - } - } - else { - if (replaceCount == 0) - JOptionPane.showMessageDialog(findDialog, "找不到您查找的内容!", "记事本", JOptionPane.INFORMATION_MESSAGE); - else - JOptionPane.showMessageDialog(findDialog, "成功替换" + replaceCount + "个匹配内容!", "替换成功", JOptionPane.INFORMATION_MESSAGE); - } - if (replaceText.getText().length() == 0 && textarea.getSelectedText() != null) { - textarea.replaceSelection(""); - replaceCount++; - } - if (replaceText.getText().length() > 0 && textarea.getSelectedText() != null) { - textarea.replaceSelection(replaceText.getText()); - replaceCount++; - } - }// end while - } - }); + ButtonGroup bGroup = new ButtonGroup(); + Container con = findDialog.getContentPane(); + con.setLayout(new FlowLayout(FlowLayout.LEFT));//设置布局 + down.setSelected(true);//向下框设为选中 + bGroup.add(up);//分组 + bGroup.add(down); + searchNext.setPreferredSize(new Dimension(110, 22));//设置按钮的大小 // "查找下一个"按钮事件处理 searchNext.addActionListener(new ActionListener() { @@ -731,18 +644,19 @@ public class Notepad implements ActionListener { if (matchcase.isSelected()) { strA = str1; strB = str3; - } else { + } + else { strA = str2; strB = str4; } if (up.isSelected()) { - if (textarea.getSelectedText() == null) { - a = strA.lastIndexOf(strB, FindStartPos - 1); - } else { - a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1); - } - } else if (down.isSelected()) { + if (textarea.getSelectedText() == null) + a = strA.lastIndexOf(strB, FindStartPos - 1); + else + a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1); + } + else if (down.isSelected()) { if (textarea.getSelectedText() == null) a = strA.indexOf(strB, FindStartPos); else @@ -774,37 +688,26 @@ public class Notepad implements ActionListener { } }); - // 创建对话框的界面 + // 创建查找对话框的界面 JPanel direction = new JPanel(); direction.setBorder(BorderFactory.createTitledBorder("方向 ")); direction.add(up); direction.add(down); direction.setPreferredSize(new Dimension(170, 60)); - JPanel replacePanel = new JPanel(); - replacePanel.setLayout(new GridLayout(2, 1)); - replacePanel.add(replace); - replacePanel.add(replaceAll); - JPanel toppanel = new JPanel(); - JPanel centerpanel = new JPanel(); JPanel bottompanel = new JPanel(); toppanel.add(searchlabel); toppanel.add(findText); toppanel.add(searchNext); - centerpanel.add(replacelabel); - centerpanel.add(replaceText); - centerpanel.add(replacePanel); bottompanel.add(matchcase); bottompanel.add(direction); bottompanel.add(cancel); con.add(toppanel); - con.add(centerpanel); con.add(bottompanel); - // 设置替换对话框的大小、可更改大小(否)、位置和可见性 - findDialog.setSize(410, 210); + findDialog.setSize(410, 180); findDialog.setResizable(false); findDialog.setLocationRelativeTo(null);//设置窗口居中 findDialog.setVisible(true); -- Gitee From a97f4abc0bf1d6ed32eefa1d88a62e07d0017d24 Mon Sep 17 00:00:00 2001 From: yuxiaoshuo Date: Sun, 29 May 2022 15:05:52 +0800 Subject: [PATCH 15/16] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=E7=9A=84=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Notepad.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java index f9f6b34..6b08f0e 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -622,6 +622,7 @@ public class Notepad implements ActionListener { JRadioButton up = new JRadioButton("向上(U)"); JRadioButton down = new JRadioButton("向下(D)"); JButton searchNext = new JButton("查找下一个(F)"); + JButton cancel = new JButton("取消"); ButtonGroup bGroup = new ButtonGroup(); Container con = findDialog.getContentPane(); con.setLayout(new FlowLayout(FlowLayout.LEFT));//设置布局 @@ -629,6 +630,7 @@ public class Notepad implements ActionListener { bGroup.add(up);//分组 bGroup.add(down); searchNext.setPreferredSize(new Dimension(110, 22));//设置按钮的大小 + cancel.setPreferredSize(new Dimension(110, 22)); // "查找下一个"按钮事件处理 searchNext.addActionListener(new ActionListener() { @@ -679,9 +681,7 @@ public class Notepad implements ActionListener { } }); - // "取消"按钮及事件处理 - JButton cancel = new JButton("取消"); - cancel.setPreferredSize(new Dimension(110, 22)); + // "取消"按钮事件处理 cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { findDialog.dispose(); -- Gitee From 2922f457c2254532d6728e5083739e9196a6932c Mon Sep 17 00:00:00 2001 From: yuxiaoshuo Date: Sun, 29 May 2022 17:13:52 +0800 Subject: [PATCH 16/16] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E6=96=B0?= =?UTF-8?q?=E7=AA=97=E5=8F=A3=E7=9A=84=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= 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 6b08f0e..dc1118f 100644 --- a/src/java2022spring/Notepad.java +++ b/src/java2022spring/Notepad.java @@ -308,7 +308,39 @@ public class Notepad implements ActionListener { Notepad window=new Notepad(); window.frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { - window.Exit(); + String content = new String(textarea.getText()); + if (currentpath!=null) { // 如果有源文件 + if (tst.equals(content)) { // 判断源文件文本是否有改动(此为无改动) + window.frame.dispose(); + } + else { // 有改动 + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将不保存", "保存", + JOptionPane.YES_NO_CANCEL_OPTION);//添加确认提示框 + if (i == JOptionPane.OK_OPTION) { + save(); + window.frame.dispose(); + } + else if (i == JOptionPane.NO_OPTION) { + window.frame.dispose(); + } + } + } + else { // 无源文件 + if (txt.equals(content)) { // 判断文本是否有改动(此为无改动) + window.frame.dispose(); + } + else { // 有改动 + int i = JOptionPane.showConfirmDialog(null, "是否保存更改?点击“否”将不保存", "保存", + JOptionPane.YES_NO_CANCEL_OPTION); + if (i == JOptionPane.OK_OPTION) { + saveAs(); + window.frame.dispose(); + } + else if (i == JOptionPane.NO_OPTION) { + window.frame.dispose(); + } + } + } } }); } -- Gitee