diff --git a/src/java2022spring/MainClass.java b/src/java2022spring/MainClass.java new file mode 100644 index 0000000000000000000000000000000000000000..1ea20bf3cd598c3be945a934d554c4bf238ffe2f --- /dev/null +++ b/src/java2022spring/MainClass.java @@ -0,0 +1,13 @@ +package java2022spring; +import java.awt.Font; +import java.time.LocalTime; +import java.util.Calendar; +import javax.swing.*; + +public class MainClass { + public static void main(String[] args) { + Notebook note = new Notebook(); + + } + +} diff --git a/src/java2022spring/Notebook.java b/src/java2022spring/Notebook.java new file mode 100644 index 0000000000000000000000000000000000000000..5a091e7f7e19184db85f7fe7391bef30015c606e --- /dev/null +++ b/src/java2022spring/Notebook.java @@ -0,0 +1,630 @@ +package java2022spring; +import java.awt.*; +import java.text.SimpleDateFormat; +import java.awt.event.*; +import java.io.*; +import java.util.Calendar; +import javax.swing.event.CaretEvent; +import javax.swing.event.CaretListener; +import javax.swing.*; +import javax.swing.undo.UndoManager; + + +import java.awt.event.*; +import javax.swing.*; +public class Notebook { + int i=20; //文本框初始字体大小 + JFrame frame=new JFrame("记事本"); + JMenuBar Bar=new JMenuBar(); + JTextArea text=new JTextArea(); + JPanel panBottom; + JLabel lblStatus,time ;//状态栏 + + JScrollPane jsp=new JScrollPane(text); + + Font font =new Font("宋体", Font.PLAIN,i); + + + JMenu fileMemu,editMenu,FormatMenu,ViewMenu,HelpMenu;//菜单栏 + + JMenuItem NewFile,OpenFile,SaveFile,SaveAsFile,ExitFile; + //文件的菜单项 + JMenuItem UndoEdit,CutEdit,CopyEdit,PasteEdit,DeleteEdit,FindEdit,ReplaceEdit,GoToEdit,AllEdit,TimeEdit; + //编辑的菜单项 + JMenuItem AutoWrapFormat,TextFormat; + //格式菜单项 + JMenu ZoomView; + JMenuItem StatusBarView,Enlarge,Reduce; + //查看的菜单项 + JMenuItem HelpAbout,About; + //帮助的菜单项 + + + + UndoManager undoManager = new UndoManager(); + + JPopupMenu popupMenu; + JMenuItem popupUndo, popupRedo, popupCut, popupCopy, popupPaste, popupDelete,popupAll; + //右键弹出菜单 + + public Notebook(){ + fileMemu=new JMenu("文件"); + editMenu=new JMenu("编辑"); + FormatMenu=new JMenu("格式"); + ViewMenu=new JMenu("查看"); + HelpMenu=new JMenu("帮助"); + + Bar.add(fileMemu); + Bar.add(editMenu); + Bar.add(FormatMenu); + Bar.add(ViewMenu); + Bar.add(HelpMenu);//加入菜单栏 + + text.getDocument().addUndoableEditListener(undoManager);//撤销 + + lblStatus = new JLabel("第 1 行, 第 1 列"); + lblStatus.setFont(new Font("宋体", Font.PLAIN, 12)); + + + text.addMouseListener(new MouseAdapter() { + public void mouseReleased(MouseEvent e) { + if (e.isPopupTrigger()) + { + popupMenu.show(e.getComponent(), e.getX(), e.getY()); + } + } + }); //鼠标右键菜单 + + + lblStatus = new JLabel("第 1 行, 第 1 列"); + lblStatus.setFont(new Font("宋体", Font.PLAIN, 12)); + + panBottom = new JPanel(); + panBottom.setLayout(new GridLayout(1, 4)); + + + Calendar cal=Calendar.getInstance(); + int m=cal.get(Calendar.MONTH); + int d=cal.get(Calendar.DATE); + int h=cal.get(Calendar.HOUR_OF_DAY); + int mi=cal.get(Calendar.MINUTE); + int s=cal.get(Calendar.SECOND); + time=new JLabel(m+"月"+d+"日"+h+"时"+mi+"分"+s+"秒"); + time.setFont(new Font("宋体", Font.PLAIN, 12)); + + + panBottom.add(lblStatus); + panBottom.add(time); + frame.add(panBottom, BorderLayout.SOUTH); //下方状态栏 + + + frameSetting(); + frame.setJMenuBar(Bar); + frame.add(jsp); + frame.setSize(1000,700); + frame.setVisible(true);//设置窗口 + frame.setLocationRelativeTo(null); + + + } + + + + + + +private void frameSetting() { + text.setFont(font); + + text.addCaretListener(new CaretListener() { + public void caretUpdate(CaretEvent e) { + + try { + int pos = text.getCaretPosition(); + // 获取行数 + int lineOfC = text.getLineOfOffset(pos) + 1; //参考了CSDN博主「Cool丶文」的方法,原文链接:https://blog.csdn.net/qq_41588169/article/details/97616324 + // 获取列数 + int col = pos - text.getLineStartOffset(lineOfC - 1) + 1; + lblStatus.setText("第 " + lineOfC + " 行, 第 " + col + " 列 "); + } catch (Exception ex) { + System.out.println("无法获取光标位置"); + }}}); + + Thread thread =new Thread(new Runnable() { + public void run() { + while(true) { + Calendar cal=Calendar.getInstance(); + int y=cal.get(Calendar.YEAR); + int m=cal.get(Calendar.MONTH); + int d=cal.get(Calendar.DATE); + int h=cal.get(Calendar.HOUR_OF_DAY); + int mi=cal.get(Calendar.MINUTE); + int s=cal.get(Calendar.SECOND); + + + time.setText(y+"年"+m+"月"+d+"日"+h+"时"+mi+"分"+s+"秒"); + + try { + Thread.sleep(1000); + } + catch(InterruptedException e) { + + } + } + }}); + thread.start(); + + + frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + if(text.getText().length()==0) { + System.exit(0); + } + else { + if(JOptionPane.showConfirmDialog(null, "需要保存文本吗","提示",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){ + + try{FileDialog dialog = new FileDialog(frame,"请选择保存文件的路径"); + dialog.setVisible(true); + String path = dialog.getDirectory(); + String fileName = dialog.getFile(); + FileWriter fwriter = new FileWriter(new File(path,fileName)); + String textdata = text.getText(); + fwriter.write(textdata); + fwriter.close();} + catch (IOException ex){} + } + else + System.exit(0); + } + } + }); //退出前询问是否保存文件 + + frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); //使窗口关闭键失效 + + // 文件菜单 + fileMemu.setMnemonic('F');// 设置快捷键ALT+F + NewFile = new JMenuItem("新建"); + NewFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, 2)); + NewFile.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + if(text.getText().length()==0) { + Notebook note = new Notebook(); + } + else { + if(JOptionPane.showConfirmDialog(null, "需要保存文本吗","提示",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){ + + try{FileDialog dialog = new FileDialog(frame,"请选择保存文件的路径"); + dialog.setVisible(true); + String path = dialog.getDirectory(); + String fileName = dialog.getFile(); + FileWriter fwriter = new FileWriter(new File(path,fileName)); + String textdata = text.getText(); + fwriter.write(textdata); + fwriter.close(); + Notebook note = new Notebook(); + } + catch (IOException ex){} + } + else { + Notebook note = new Notebook(); + } + } + + }}); + OpenFile = new JMenuItem("打开..."); + OpenFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, 2)); + OpenFile.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + try { + FileDialog dialog = new FileDialog(frame,"请选择要打开的文件"); + dialog.setVisible(true); + String path = dialog.getDirectory(); + String fileName = dialog.getFile(); + FileInputStream input = new FileInputStream(new File(path,fileName)); + BufferedInputStream in =new BufferedInputStream(input); + String string=null; + byte [] b=new byte[1024]; + in.read(b); + int lenth=0; + String data=""; + while((lenth=input.read(b))!=-1) { + data+=new String(b,0,lenth); + } + text.setText(data); + input.close(); + in.close(); + } catch (IOException ex){ + + } + } + }); + SaveFile = new JMenuItem("保存"); + SaveFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, 2)); + SaveFile.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + try { + String path ="D:\\"; + File f = new File(path,"new File.txt"); + f.createNewFile(); + FileWriter fwriter = new FileWriter(f); + String textdata = text.getText(); + fwriter.write(textdata); + fwriter.close(); + } + catch (IOException ex){ + + } + } + }); + SaveAsFile = new JMenuItem("另存为..."); + SaveAsFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, 3)); + SaveAsFile.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + try { + FileDialog dialog = new FileDialog(frame,"请选择保存文件的路径"); + dialog.setVisible(true); + String path = dialog.getDirectory(); + String fileName = dialog.getFile(); + FileWriter fwriter = new FileWriter(new File(path,fileName)); + String textdata = text.getText(); + fwriter.write(textdata); + fwriter.close(); + } + + + catch (IOException ex){ + + } + }} + ); + ExitFile = new JMenuItem("退出"); + ExitFile.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + System.exit(0); + } + }); + + + // 编辑菜单 + editMenu.setMnemonic('E'); + UndoEdit = new JMenuItem("撤销(Z)"); + UndoEdit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, 2)); + UndoEdit.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if(undoManager.canUndo()) { + undoManager.undo(); + } + }}); + CutEdit = new JMenuItem("剪切(T)"); + CutEdit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, 2)); + CutEdit.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + text.cut(); + } + }); + CopyEdit = new JMenuItem("复制(C)"); + CopyEdit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, 2)); + CopyEdit.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + text.copy(); + }}); + PasteEdit = new JMenuItem("粘贴(V)"); + PasteEdit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, 2)); + PasteEdit.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + text.paste(); + }}); + DeleteEdit = new JMenuItem("删除(L)"); + DeleteEdit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); + DeleteEdit.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + text.replaceSelection(null); + }}); + FindEdit = new JMenuItem("查找(F)"); + FindEdit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, 2)); + FindEdit.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JDialog find = new JDialog(frame, "查找"); + find.setVisible(true); + find.setSize(250, 100); + find.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + find.setLocationRelativeTo(null); + String data= text.getText(); + + JTextField findtext= new JTextField(15); + JButton findbutton=new JButton("查找下一个"); + find.add(findtext, BorderLayout.CENTER); + find.add(findbutton, BorderLayout.SOUTH); + findbutton.addActionListener(new ActionListener() { + int start=0; + public void actionPerformed(ActionEvent e) { + String finddata=findtext.getText(); + int s = data.indexOf(finddata, start); + if (data.indexOf(finddata, start) != -1) { + text.setSelectionStart(s); + text.setSelectionEnd(s + finddata.length()); + start = s + 1; + } + else { + + } + }}); + + + }}); + ReplaceEdit = new JMenuItem("替换(R)"); + ReplaceEdit .setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, 2)); + ReplaceEdit.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JDialog find = new JDialog(frame, "查找和替换"); + find.setLayout(new java.awt.FlowLayout()); + find.setVisible(true); + find.setBounds(100,100,310,260); + find.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + find.setLocationRelativeTo(null); + String data= text.getText(); + + JTextField replacetext =new JTextField(10); + JTextField findtext= new JTextField(10); + JButton findbutton=new JButton("查找下一个"); + JButton replacebutton=new JButton("替换"); + Box boxH=Box.createHorizontalBox(); + Box boxVOne=Box.createVerticalBox(); + Box boxVTwo=Box.createVerticalBox(); + boxVOne.add(findbutton); + boxVOne.add(replacebutton); + boxVTwo.add(findtext); + boxVTwo.add(replacetext); + boxH.add(boxVOne); + boxH.add(Box.createHorizontalStrut(10)); + boxH.add(boxVTwo); + find.add(boxH); //设置替换功能窗口 + + findbutton.addActionListener(new ActionListener() { + int start=0; + public void actionPerformed(ActionEvent e) { + String finddata=findtext.getText(); + int s = data.indexOf(finddata, start); + if (data.indexOf(finddata, start) != -1) { + text.setSelectionStart(s); + text.setSelectionEnd(s + finddata.length()); + start = s + 1; + } + else { + JOptionPane.showMessageDialog(find, "查找失败!", "提示", 0); + find.dispose(); + } + }}); + + replacebutton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + String replacedata=replacetext.getText(); + text.replaceSelection(replacedata); + }}); + + }}); + GoToEdit = new JMenuItem("转到(G)"); + GoToEdit .setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, 2)); + AllEdit = new JMenuItem("全选(A)"); + AllEdit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 2)); + AllEdit.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + text.selectAll(); + }}); + TimeEdit = new JMenuItem("时间、日期(D)"); + TimeEdit .setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0)); + TimeEdit .addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + Calendar cal=Calendar.getInstance(); + int y=cal.get(Calendar.YEAR); + int m=cal.get(Calendar.MONTH); + int d=cal.get(Calendar.DATE); + int h=cal.get(Calendar.HOUR_OF_DAY); + int mi=cal.get(Calendar.MINUTE); + int s=cal.get(Calendar.SECOND); + Calendar calendar = Calendar.getInstance(); + text.setText(text.getText()+y+"年"+m+"月"+d+"日"+h+"时"+mi+"分"+s+"秒"); + } + }); + + // 格式菜单 + FormatMenu.setMnemonic('O');// + AutoWrapFormat = new JMenuItem("自动换行(W)"); + AutoWrapFormat.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, 2)); + AutoWrapFormat.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e) { + text.setLineWrap(true); + + } + }); + TextFormat = new JMenuItem("字体(F)"); + TextFormat.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, 2)); + TextFormat.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try{ + + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); + + JList fontNames = new JList(ge.getAvailableFontFamilyNames()); + + int response = JOptionPane.showConfirmDialog(null,new JScrollPane(fontNames), "请选择字体",JOptionPane.OK_CANCEL_OPTION); + + + Object selectedFont = fontNames.getSelectedValue(); + + if (response == JOptionPane.OK_OPTION && selectedFont != null) + font=new Font((String) fontNames.getSelectedValue(),Font.PLAIN,i); + text.setFont(font); + } + catch (Exception ex){ + ex.printStackTrace(); + } + + } + }); + + + // 查看菜单 + ViewMenu.setMnemonic('V'); + ZoomView = new JMenu("缩放"); + StatusBarView = new JMenuItem("状态栏"); + + StatusBarView.addActionListener(new ActionListener() { + boolean status = false; + + @Override + public void actionPerformed(ActionEvent e) { + if (status) { + panBottom.setVisible(true); + + status = false; + } else { + panBottom.setVisible(false); + status = true; + } + } + + }); + Reduce = new JMenuItem("缩小"); + Reduce.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 2)); + Reduce .addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + font=font.deriveFont(Font.PLAIN,--i); + text.setFont(font); + } + }); + Enlarge = new JMenuItem("放大"); + Enlarge.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 2)); + Enlarge .addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + font=font.deriveFont(Font.PLAIN,++i); + text.setFont(font); + } + }); + + //帮助菜单 + HelpMenu.setMnemonic('H'); + HelpAbout = new JMenuItem("查看帮助"); + HelpAbout.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(HelpAbout,"潘锦辉 202125710322 \n"+ + "1.记事本菜单栏中的功能均可使用 \n"+ + "2.记事本中的快捷键都可用 常用如复制,粘贴,撤销为ctrl+c,v,z 文本放大缩小为ctrl+up,down等\n"+ + "3.保存操作会将文本保存在D/ 路径下 \n"+ + "4.替换功能中需要先查找内容才可替换\n"+ + "5.在文本区右键点击有菜单功能" + + + + ,"关于记事本",JOptionPane.INFORMATION_MESSAGE); + } + }); + About = new JMenuItem("关于记事本"); + About.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(About, "Java Swing 程序设计 \n潘锦辉 202125710322 \n"+ + "1.笔记本仅支持打开txt文件(在UTF-8编码下中文显示乱码" + + ,"关于记事本",JOptionPane.INFORMATION_MESSAGE); + } + }); + + + + popupMenu = new JPopupMenu(); + popupUndo = new JMenuItem("撤销(U)"); + popupUndo.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if(undoManager.canUndo()) { + undoManager.undo(); + } + }}); + + popupCut = new JMenuItem("剪切(T)"); + popupCut.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + text.cut(); + } + }); + popupCopy = new JMenuItem("复制(C)"); + popupCopy.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + text.copy(); + } + }); + popupPaste = new JMenuItem("粘贴(P)"); + popupPaste.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + text.paste(); + } + }); + popupDelete = new JMenuItem("删除(D)"); + popupDelete.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + text.replaceSelection(null); + }}); + popupAll = new JMenuItem("全选(A)"); + popupAll.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + text.selectAll(); + }}); + // 鼠标右键菜单 + + + + //添加到菜单 + fileMemu.add(NewFile); + fileMemu.add(OpenFile); + fileMemu.add(SaveFile); + fileMemu.add(SaveAsFile); + fileMemu.addSeparator(); + fileMemu.add(ExitFile); + + editMenu.add(UndoEdit); + editMenu.addSeparator(); + editMenu.add(CutEdit); + editMenu.add(CopyEdit); + editMenu.add(PasteEdit); + editMenu.add(DeleteEdit); + editMenu.addSeparator(); + editMenu.add(FindEdit); + editMenu.add(ReplaceEdit); + editMenu.addSeparator(); + editMenu.add(AllEdit); + editMenu.add(TimeEdit); + + FormatMenu.add(AutoWrapFormat); + FormatMenu.add(TextFormat); + + ViewMenu.add(ZoomView); + ViewMenu.add(StatusBarView); + ZoomView.add(Enlarge); + ZoomView.add(Reduce); + + HelpMenu.add(HelpAbout); + HelpMenu.add(About); + + popupMenu.add(popupUndo); + popupMenu.addSeparator(); + popupMenu.add(popupCut); + popupMenu.add(popupCopy); + popupMenu.add(popupPaste); + popupMenu.add(popupDelete); + popupMenu.addSeparator(); + popupMenu.add(popupAll); + +} +} + + + + \ 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!"); - } -}