diff --git a/.classpath b/.classpath deleted file mode 100644 index ac9ce57c2f4d3a99eb1868e101f3060156474663..0000000000000000000000000000000000000000 --- a/.classpath +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/.gitignore b/.gitignore deleted file mode 100644 index ae3c1726048cd06b9a143e0376ed46dd9b9a8d53..0000000000000000000000000000000000000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin/ diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000000000000000000000000000000000000..0e124206de2d87620445e3c6d129e5de6504af30 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +java2022spring \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..05483570e041eb648703359441d61bf9a5feeb13 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..1148eb3cebe0e7a72dae0af25120dbb4ab82a410 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000000000000000000000000000000000000..e96534fb27b68192f27f985d3879e173ec77adb8 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000000000000000000000000000000000000..4c37ac5d35c6d5d7e4e67287404d8c3aa712c78c --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,187 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1653567046270 + + + 1653567351085 + + + 1654162147261 + + + 1654433749753 + + + 1654675507306 + + + 1654684554397 + + + 1654696445715 + + + 1654696468026 + + + 1654731487466 + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.project b/.project deleted file mode 100644 index b7e8a74e9dd98bfb5b42b70cd6033510d6823948..0000000000000000000000000000000000000000 --- a/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - java2022spring - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index dbc27c5dc60ef7094a88953087ea0294b6c44a38..0000000000000000000000000000000000000000 --- a/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,14 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=17 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning -org.eclipse.jdt.core.compiler.release=enabled -org.eclipse.jdt.core.compiler.source=17 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..29f81d812f3e768fa89638d1f72920dbfd1413a8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/java2022spring1.iml b/java2022spring1.iml new file mode 100644 index 0000000000000000000000000000000000000000..e1006ff5a795e7aeb2b4bc32124e81f3ee4e910c --- /dev/null +++ b/java2022spring1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/java2022spring1/java2022spring/Notepad$1.class b/out/production/java2022spring1/java2022spring/Notepad$1.class new file mode 100644 index 0000000000000000000000000000000000000000..85e2a13f1ec15885b52ba3d4df70d29d3035df91 Binary files /dev/null and b/out/production/java2022spring1/java2022spring/Notepad$1.class differ diff --git a/out/production/java2022spring1/java2022spring/Notepad$2.class b/out/production/java2022spring1/java2022spring/Notepad$2.class new file mode 100644 index 0000000000000000000000000000000000000000..a58d0c5765bc3d3f832cd73b9ffb3b20b4d985e8 Binary files /dev/null and b/out/production/java2022spring1/java2022spring/Notepad$2.class differ diff --git a/out/production/java2022spring1/java2022spring/Notepad$3.class b/out/production/java2022spring1/java2022spring/Notepad$3.class new file mode 100644 index 0000000000000000000000000000000000000000..29322d3398572590c2bc6a241f50a8e664038238 Binary files /dev/null and b/out/production/java2022spring1/java2022spring/Notepad$3.class differ diff --git a/out/production/java2022spring1/java2022spring/Notepad.class b/out/production/java2022spring1/java2022spring/Notepad.class new file mode 100644 index 0000000000000000000000000000000000000000..668da2bc6aa0c557284151e93c429140fe99e362 Binary files /dev/null and b/out/production/java2022spring1/java2022spring/Notepad.class differ diff --git a/out/production/java2022spring1/java2022spring/SaveF.class b/out/production/java2022spring1/java2022spring/SaveF.class new file mode 100644 index 0000000000000000000000000000000000000000..90555abf046388d768f166369dc85e9194545bf8 Binary files /dev/null and b/out/production/java2022spring1/java2022spring/SaveF.class differ diff --git a/out/production/java2022spring1/java2022spring/T1.class b/out/production/java2022spring1/java2022spring/T1.class new file mode 100644 index 0000000000000000000000000000000000000000..1ac8a9db3f335fb7be11ddfbd5e7a277e7046c46 Binary files /dev/null and b/out/production/java2022spring1/java2022spring/T1.class differ diff --git a/readme.md b/readme.md deleted file mode 100644 index 4af16add33b576d095308a1888db35774618715d..0000000000000000000000000000000000000000 --- a/readme.md +++ /dev/null @@ -1,2 +0,0 @@ -# ѧ -0103 \ No newline at end of file diff --git a/src/java2022spring/Notepad.java b/src/java2022spring/Notepad.java new file mode 100644 index 0000000000000000000000000000000000000000..40c0f1e336c07311a56ae121c1a978585aa6917c --- /dev/null +++ b/src/java2022spring/Notepad.java @@ -0,0 +1,546 @@ +package java2022spring; + +import java.awt.*; +import java.awt.event.*; +import java.io.*; +import javax.swing.*; +import javax.swing.event.CaretEvent; +import javax.swing.event.CaretListener; +import javax.swing.filechooser.FileFilter; +import javax.swing.undo.UndoManager; + +public class Notepad implements ActionListener { + private JFrame frame = new JFrame("小许的记事本"); + private JTextArea textArea = new JTextArea(); + private JMenuBar menuBar = new JMenuBar(); + private int fonts = 20; + private boolean flag = true; + private boolean autoLineFlag = true; + private boolean bottomFlag = true; + private String txt = ""; + private File f; + private String encoding = "UTF-8"; + private String res = ""; // 命令行界面 + private String value; + UndoManager undoManager = new UndoManager(); + //菜单栏内容 + JMenu file, edit, view, help, out; + //“文件”的菜单项 + JMenuItem New, Open, Save, SaveAs, Exit; + //“编辑”的菜单项 + JMenuItem Undo, Redo, Cut, Copy, Paste, Delete, All; + //“格式”的菜单项 + JMenuItem autoLine; + //查看 菜单栏 + JMenuItem showBar; + //“帮助”的菜单项 + JMenuItem About; + + // 在主类中定义如下菜单项,它们就是右键菜单 + JPopupMenu popupMenu; + JMenuItem popupMenu_Undo, popupMenu_Redo, popupMenu_Cut, popupMenu_Copy, popupMenu_Paste, popupMenu_Delete, + popupMenu_All; + + JLabel statistic; + JToolBar bottomBar; + + + public Notepad() { + // 菜单栏内容 + file = new JMenu("文件(F)"); + edit = new JMenu("编辑(E)"); + out = new JMenu("格式(O)"); + view = new JMenu("查看(V)"); + help = new JMenu("帮助(H)"); + frameSetting(); + menuBar.add(file); + menuBar.add(edit); + menuBar.add(out); + menuBar.add(view); + menuBar.add(help); + // 文本编辑区注册右键菜单事件 + textArea.addMouseListener(new MouseAdapter() { + public void mousePressed(MouseEvent e) { + if (e.isPopupTrigger()) + { + popupMenu.show(e.getComponent(), e.getX(), e.getY()); + } + textArea.requestFocus(); + } + + public void mouseReleased(MouseEvent e) { + if (e.isPopupTrigger()) + { + popupMenu.show(e.getComponent(), e.getX(), e.getY()); + } + textArea.requestFocus(); + } + }); + + textArea.requestFocus(); + frame.setJMenuBar(menuBar); + // 文字编辑界面的设置 + textArea.setLineWrap(autoLineFlag); + autoLine.setText(autoLineFlag ? "√ 自动换行" : "× 自动换行"); + JScrollPane jsp = new JScrollPane(textArea);// 滚动窗口面板 + frame.add(jsp); + textArea.setFont(new Font("宋体", Font.PLAIN, fonts)); + frame.setSize(800, 600); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);// 意思是关闭按钮失效 + + statistic = new JLabel("共 0 行,共 0 列, 字数: 0"); + statistic.setFont(new Font("黑体", 0, 15)); + bottomBar = new JToolBar(); + bottomBar.add(statistic); + bottomBar.setFloatable(false); + frame.getContentPane().add(bottomBar, BorderLayout.SOUTH); + + + textArea.addCaretListener(new CaretListener() { + @Override + public void caretUpdate(CaretEvent e) { + try { + int len = textArea.getDocument().getLength(); + int offset = e.getDot() ; + int row = textArea.getLineOfOffset(offset); + int col = e.getDot() - textArea.getLineStartOffset(row); + statistic.setText(String.format("第 %d 行,第 %d 列, 字数: %d",row+1, col+1, len)); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + }); + + } + + + @Override + public void actionPerformed(ActionEvent e) { + String commamd = e.getActionCommand(); + System.out.println("执行的命令->" + commamd); + // “新建”功能 + if ("新建".equals(commamd)) { + String tst = new String(textArea.getText()); + if (!flag) { + if (txt.equals(tst)) { + flag = true; + NewFile(); + } else { + int i = JOptionPane.showConfirmDialog(null, "是否保存您所做的更改?点击“否”将直接退出不做更改!", "确认保存", + JOptionPane.YES_NO_CANCEL_OPTION); + if (i == JOptionPane.OK_OPTION) { + write(); + flag = true; + NewFile(); + } else if (i == JOptionPane.NO_OPTION) { + flag = true; + NewFile(); + } + } + } else { + if (txt.equals(tst)) { + flag = true; + NewFile(); + } else { + int i = JOptionPane.showConfirmDialog(null, "是否保存您所做的更改?点击“否”将直接退出不做更改!", "确认保存", + JOptionPane.YES_NO_CANCEL_OPTION); + if (i == JOptionPane.OK_OPTION) { + SaveF rep = new SaveF(); + f = rep.f; + flag = true; + write(); + NewFile(); + } else if (i == JOptionPane.NO_OPTION) { + flag = true; + NewFile(); + } + } + } + // “打开”功能 + } else if ("打开...".equals(commamd)) { + JFileChooser rep = new JFileChooser("%homepath%"); + rep.setDialogType(JFileChooser.OPEN_DIALOG); + int returnVal = rep.showOpenDialog(null); + if (returnVal == JFileChooser.APPROVE_OPTION) { + f = rep.getSelectedFile(); + frame.setTitle(f.getName()); + encoding = "UTF-8"; + try { + res = read("UTF-8"); + } catch (IOException e1) { + e1.printStackTrace(); + } + flag = false; + value = res; + textArea.setText(res); + txt = textArea.getText(); + } + // “保存”功能 + } else if ("保存".equals(commamd)) { + if (flag) { + SaveF rep = new SaveF(); + f = rep.f; + flag = false; + } + write(); + frame.setTitle(f.getName()); + txt = textArea.getText(); + // “另存为”功能 + } else if ("另存为...".equals(commamd)) { + SaveF rep = new SaveF(); + f = rep.f; + flag = false; + write(); + frame.setTitle(f.getName()); + // 其他功能 + } else if ("退出".equals(commamd)) { + Exit(); + } else if ("撤销".equals(commamd)) { + if (undoManager.canUndo()) { + undoManager.undo(); + } + } else if ("重做".equals(commamd)) { + if (undoManager.canRedo()) { + undoManager.redo(); + } + } else if ("剪切".equals(commamd)) { + value = textArea.getText(); + textArea.cut(); + } else if ("复制".equals(commamd)) { + textArea.copy(); + } else if ("粘贴".equals(commamd)) { + value = textArea.getText(); + textArea.paste(); + } else if ("删除".equals(commamd)) { + value = textArea.getText(); + textArea.replaceSelection(null); + } else if ("全选".equals(commamd)) { + textArea.selectAll(); + // 关于选项卡 + } else if ("关于".equals(commamd)) { + JDialog dia = new JDialog(frame, "关于Notepad- -"); + dia.setSize(250, 160); + dia.setResizable(false); + JLabel l = new JLabel( + " 记事本
 许文瀚的设计性实验作业
 部分代码参考自网络
  "); + l.setFont(new Font("微软雅黑", Font.BOLD, 15)); + dia.add(l, BorderLayout.CENTER); + dia.setLocationRelativeTo(null); + dia.setVisible(true); + dia.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + } + } + + private void frameSetting() { + // 文件菜单 + file.setMnemonic('F');// 设置快捷键ALT+F + New = new JMenuItem("新建"); + New.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, 2)); + New.addActionListener(this); + Open = new JMenuItem("打开..."); + Open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, 2)); + Open.addActionListener(this); + Save = new JMenuItem("保存"); + Save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, 2)); + Save.addActionListener(this); + SaveAs = new JMenuItem("另存为..."); + SaveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, 3)); + SaveAs.addActionListener(this); + Exit = new JMenuItem("退出"); + Exit.addActionListener(this); + // 编辑菜单 + edit.setMnemonic('E');// 设置快捷键ALT+E + Undo = new JMenuItem("撤销"); + Undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, 2)); + Undo.addActionListener(this); + Redo = new JMenuItem("重做"); + Redo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, 2)); + Redo.addActionListener(this); + Cut = new JMenuItem("剪切"); + Cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, 2)); + Cut.addActionListener(this); + Copy = new JMenuItem("复制"); + Copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, 2)); + Copy.addActionListener(this); + Paste = new JMenuItem("粘贴"); + Paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, 2)); + Paste.addActionListener(this); + Delete = new JMenuItem("删除"); + Delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); + Delete.addActionListener(this); + All = new JMenuItem("全选"); + All.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 2)); + All.addActionListener(this); + // 格式菜单 + out.setMnemonic('O');// 设置快捷键ALT+O + autoLine = new JMenuItem("自动换行"); + autoLine.addActionListener(e -> { + autoLineFlag = !autoLineFlag; + textArea.setLineWrap(autoLineFlag); + autoLine.setText(autoLineFlag ? "√ 自动换行" : "× 自动换行"); + }); + // 查看菜单 + view.setMnemonic('V');// 设置快捷键ALT+V + showBar = new JMenuItem(bottomFlag ? "√ 统计状态栏" : "× 统计状态栏"); + showBar.addActionListener(e -> { + bottomFlag = !bottomFlag; + bottomBar.setVisible(bottomFlag); + showBar.setText(bottomFlag ? "√ 统计状态栏" : "× 统计状态栏"); + }); + // 帮助菜单 + help.setMnemonic('H');// 设置快捷键ALT+H + About = new JMenuItem("关于"); + About.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 3)); + About.addActionListener(this); + // 添加到菜单 + file.add(New); + file.addSeparator(); + file.add(Open); + file.add(Save); + file.add(SaveAs); + file.addSeparator(); + file.add(Exit); + + edit.add(Undo); + edit.add(Redo); + edit.addSeparator(); + edit.add(Cut); + edit.add(Copy); + edit.add(Paste); + edit.add(Delete); + edit.add(All); + + out.add(autoLine); + + view.add(showBar); + + help.add(About); + + // 创建右键弹出菜单 + popupMenu = new JPopupMenu(); + popupMenu_Undo = new JMenuItem("撤销"); + popupMenu_Undo.addActionListener(this); + popupMenu_Redo = new JMenuItem("重做"); + popupMenu_Redo.addActionListener(this); + popupMenu_Cut = new JMenuItem("剪切"); + popupMenu_Cut.addActionListener(this); + popupMenu_Copy = new JMenuItem("复制"); + popupMenu_Copy.addActionListener(this); + popupMenu_Paste = new JMenuItem("粘贴"); + popupMenu_Paste.addActionListener(this); + popupMenu_Delete = new JMenuItem("删除"); + popupMenu_Delete.addActionListener(this); + popupMenu_All = new JMenuItem("全选"); + popupMenu_All.addActionListener(this); + // 向右键菜单添加菜单项和分隔符 + popupMenu.add(popupMenu_Undo); + popupMenu.add(popupMenu_Redo); + popupMenu.addSeparator(); + popupMenu.add(popupMenu_Cut); + popupMenu.add(popupMenu_Copy); + popupMenu.add(popupMenu_Paste); + popupMenu.add(popupMenu_Delete); + popupMenu.addSeparator(); + popupMenu.add(popupMenu_All); + } + + // 读取命令 + public String read() { // ANSI读取 + String temp = ""; + try { + FileInputStream fis = new FileInputStream(f.getAbsolutePath()); + byte[] b = new byte[1024]; + while (true) { + int num = fis.read(b); + if (num == -1) + break; + temp = temp + new String(b, 0, num); + } + fis.close(); + } catch (Exception e1) { + e1.printStackTrace(); + } + return temp; + } + + @SuppressWarnings("resource") + public String read(String encoding) throws IOException { // UTF-8读取 + File file = new File(f.getAbsolutePath()); + String message = "";// 获取文件内容 + if (file.isFile() && file.exists()) { + BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding)); + String buff; + while ((buff = br.readLine()) != null) { + message = message + buff + "\n"; + } + } + return message; + } + + public void write() { + if (f.exists()) { + int i = JOptionPane.showConfirmDialog(null, "此位置已出现重名文件,是否保存该文件并删除之前文件", "确认保存", JOptionPane.YES_NO_OPTION); + if (i == JOptionPane.OK_OPTION) { + try { + if (encoding.equals("UTF-8")) { + BufferedWriter fos = new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(f), encoding)); + System.out.println("已成功删除已有文件并保持当前文件"); + fos.write(textArea.getText()); + System.out.println("22222"); + fos.close(); + } else { + Writer fos = new OutputStreamWriter(new FileOutputStream(f, false), "GB2312"); + System.out.println("已成功删除已有文件并保持当前文件"); + fos.write(textArea.getText()); + fos.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } else + try { + if (encoding.equals("UTF-8")) { + BufferedWriter fos = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), encoding)); + System.out.println("已成功删除已有文件并保持当前文件"); + fos.write(textArea.getText()); + System.out.println("22222"); + fos.close(); + } else { + Writer fos = new OutputStreamWriter(new FileOutputStream(f, false), "GB2312"); + System.out.println("已成功删除已有文件并保持当前文件"); + fos.write(textArea.getText()); + fos.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + // 新建命令 + public void NewFile() { + frame.dispose(); + Notepad note = new Notepad(); + // 关闭的时候询问是否保存文件 + note.frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + note.Exit(); + } + }); + } + + public void Exit() { + String tst = new String(textArea.getText()); + if (!flag) { + if (txt.equals(tst)) { + System.exit(0); + } else { + int i = JOptionPane.showConfirmDialog(null, "是否保存您所做的更改?点击“否”将直接退出不做更改!", "确认保存", + JOptionPane.YES_NO_CANCEL_OPTION); + if (i == JOptionPane.OK_OPTION) { + write(); + } else if (i == JOptionPane.NO_OPTION) { + System.exit(0); + } + } + } else { + if (txt.equals(tst)) { + System.exit(0); + } else { + int i = JOptionPane.showConfirmDialog(null, "是否保存您所做的更改?点击“否”将直接退出不做更改!", "确认保存", + JOptionPane.YES_NO_CANCEL_OPTION); + if (i == JOptionPane.OK_OPTION) { + SaveF rep = new SaveF(); + f = rep.f; + write(); + } else if (i == JOptionPane.NO_OPTION) { + System.exit(0); + } + } + } + } + + public static void main(String[] args) { + // 生成Windows风格的UI界面 + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException + | UnsupportedLookAndFeelException e) { + e.printStackTrace(); + } + ; + Notepad note = new Notepad(); + note.frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + note.Exit(); + } + }); + } + +} + +class T1 extends FileFilter { + String ends; + String description; + + public T1(String ends, String description) { + this.ends = ends; + this.description = description; + } + + @Override + public boolean accept(File file) { + if (file.isDirectory()) + return true; + String fileName = file.getName(); + if (fileName.toUpperCase().endsWith(this.ends.toUpperCase())) + return true; + return false; + } + + @Override + public String getDescription() { + if (this.description == "所有文件") + this.description = "文件"; + return this.description; + } + public String getEnds() { + return this.ends; + } +} + +class SaveF { // 保存文件类 + File f; + JFileChooser rep = new JFileChooser("%homepath%"); + T1 txtFilter = new T1(".txt", "文件文档 (*.txt)"); + T1 txtFilter1 = new T1(".ini", "配置文件 (*.ini)"); + + SaveF() { + rep.addChoosableFileFilter(txtFilter); + rep.addChoosableFileFilter(txtFilter1); + int returnVal = rep.showDialog(null, "保存"); + if (returnVal == JFileChooser.APPROVE_OPTION) { + if (!(rep.getFileFilter().getDescription() == "所有文件")) { + File file = rep.getSelectedFile(); + T1 filter = (T1) rep.getFileFilter(); + String ends = filter.getEnds(); + File newFile = null; + if (file.getAbsolutePath().toUpperCase().endsWith(ends.toUpperCase())) { + newFile = file; + System.out.println("保存路径:" + newFile.getAbsolutePath()); + f = new File(newFile.getAbsolutePath()); + } else { + newFile = new File(file.getAbsolutePath() + ends); + System.out.println("保存路径:" + newFile.getAbsolutePath()); + f = new File(newFile.getAbsolutePath()); + } + } else { + f = rep.getSelectedFile(); + System.out.println("保存路径:" + f); + } + } + } +} \ No newline at end of file diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java deleted file mode 100644 index 24deb29bfc0e5f50fdedcd21b504e77b529d48b6..0000000000000000000000000000000000000000 --- 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!"); - } -}