From 0a2a9c7c8bd1ac86024e9335a8c8608315db48b1 Mon Sep 17 00:00:00 2001 From: hjh Date: Tue, 8 Jun 2021 08:51:59 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E7=95=8C=E9=9D=A2=E5=B8=83=E5=B1=80?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E5=A5=BD=E5=95=A6=EF=BC=8C=E8=A6=81=E6=9D=A5?= =?UTF-8?q?=E5=8A=A0=E4=B8=8A=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/babyNotepad/P1.java | 151 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 src/babyNotepad/P1.java diff --git a/src/babyNotepad/P1.java b/src/babyNotepad/P1.java new file mode 100644 index 0000000..675e37f --- /dev/null +++ b/src/babyNotepad/P1.java @@ -0,0 +1,151 @@ +package babyNotepad; + +import java.awt.BorderLayout; +import java.awt.EventQueue; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; +import javax.swing.JMenuBar; +import javax.swing.JMenu; +import javax.swing.JMenuItem; +import javax.swing.JTextArea; + +public class P1 extends JFrame { + + private JPanel contentPane; + + /** + * Launch the application. + */ + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + P1 frame = new P1(); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + /** + * Create the frame. + */ + public P1() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(100, 100, 450, 300); + + JMenuBar menuBar = new JMenuBar(); + setJMenuBar(menuBar); + + JMenu mnNewMenu = new JMenu("\u6587\u4EF6(F)"); + menuBar.add(mnNewMenu); + + JMenuItem mntmNewMenuItem_1 = new JMenuItem("\u65B0\u5EFA(N)"); + mnNewMenu.add(mntmNewMenuItem_1); + + JMenuItem mntmNewMenuItem_14 = new JMenuItem("\u65B0\u7A97\u53E3(W)"); + mnNewMenu.add(mntmNewMenuItem_14); + + JMenuItem mntmNewMenuItem_15 = new JMenuItem("\u6253\u5F00(O)"); + mnNewMenu.add(mntmNewMenuItem_15); + + JMenuItem mntmNewMenuItem_16 = new JMenuItem("\u4FDD\u5B58(S)"); + mnNewMenu.add(mntmNewMenuItem_16); + + JMenuItem mntmNewMenuItem_17 = new JMenuItem("\u53E6\u5B58\u4E3A(A)"); + mnNewMenu.add(mntmNewMenuItem_17); + + JMenuItem mntmNewMenuItem_18 = new JMenuItem("\u9000\u51FA(X)"); + mnNewMenu.add(mntmNewMenuItem_18); + + JMenu mnNewMenu_1 = new JMenu("\u7F16\u8F91(E)"); + menuBar.add(mnNewMenu_1); + + JMenuItem mntmNewMenuItem_2 = new JMenuItem("\u64A4\u9500(U)"); + mnNewMenu_1.add(mntmNewMenuItem_2); + + JMenuItem mntmNewMenuItem_3 = new JMenuItem("\u526A\u5207(T)"); + mnNewMenu_1.add(mntmNewMenuItem_3); + + JMenuItem mntmNewMenuItem_4 = new JMenuItem("\u590D\u5236(C)"); + mnNewMenu_1.add(mntmNewMenuItem_4); + + JMenuItem mntmNewMenuItem_5 = new JMenuItem("\u7C98\u8D34(P)"); + mnNewMenu_1.add(mntmNewMenuItem_5); + + JMenuItem mntmNewMenuItem_6 = new JMenuItem("\u5220\u9664(L)"); + mnNewMenu_1.add(mntmNewMenuItem_6); + + JMenuItem mntmNewMenuItem_7 = new JMenuItem("\u67E5\u627E(F)"); + mnNewMenu_1.add(mntmNewMenuItem_7); + + JMenuItem mntmNewMenuItem_8 = new JMenuItem("\u67E5\u627E\u4E0B\u4E00\u4E2A(N)"); + mnNewMenu_1.add(mntmNewMenuItem_8); + + JMenuItem mntmNewMenuItem_9 = new JMenuItem("\u67E5\u627E\u4E0A\u4E00\u4E2A(V)"); + mnNewMenu_1.add(mntmNewMenuItem_9); + + JMenuItem mntmNewMenuItem_10 = new JMenuItem("\u66FF\u6362(R)"); + mnNewMenu_1.add(mntmNewMenuItem_10); + + JMenuItem mntmNewMenuItem_11 = new JMenuItem("\u8F6C\u5230(G)"); + mnNewMenu_1.add(mntmNewMenuItem_11); + + JMenuItem mntmNewMenuItem_12 = new JMenuItem("\u5168\u9009(A)"); + mnNewMenu_1.add(mntmNewMenuItem_12); + + JMenuItem mntmNewMenuItem_13 = new JMenuItem("\u65F6\u95F4/\u65E5\u671F(D)"); + mnNewMenu_1.add(mntmNewMenuItem_13); + + JMenu mnNewMenu_2 = new JMenu("\u683C\u5F0F(O)"); + menuBar.add(mnNewMenu_2); + + JMenuItem mntmNewMenuItem_19 = new JMenuItem("\u81EA\u52A8\u6362\u884C(W)"); + mnNewMenu_2.add(mntmNewMenuItem_19); + + JMenuItem mntmNewMenuItem_20 = new JMenuItem("\u5B57\u4F53(F)\u2026"); + mnNewMenu_2.add(mntmNewMenuItem_20); + + JMenu mnNewMenu_3 = new JMenu("\u67E5\u770B(V)"); + menuBar.add(mnNewMenu_3); + + JMenuItem mntmNewMenuItem_21 = new JMenuItem("\u7F29\u653E(Z)"); + mnNewMenu_3.add(mntmNewMenuItem_21); + + JMenuItem mntmNewMenuItem_22 = new JMenuItem("\u72B6\u6001\u680F(S)"); + mnNewMenu_3.add(mntmNewMenuItem_22); + + JMenu mnNewMenu_4 = new JMenu("\u5E2E\u52A9(H)"); + menuBar.add(mnNewMenu_4); + + JMenuItem mntmNewMenuItem_23 = new JMenuItem("\u67E5\u770B\u5E2E\u52A9(H)"); + mnNewMenu_4.add(mntmNewMenuItem_23); + + JMenuItem mntmNewMenuItem = new JMenuItem("\u5173\u4E8E\u8BB0\u4E8B\u672C(A)"); + mnNewMenu_4.add(mntmNewMenuItem); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + contentPane.setLayout(new BorderLayout(0, 0)); + setContentPane(contentPane); + + JTextArea textArea = new JTextArea(); + contentPane.add(textArea, BorderLayout.NORTH); + + JTextArea textArea_1 = new JTextArea(); + contentPane.add(textArea_1, BorderLayout.WEST); + + JTextArea textArea_2 = new JTextArea(); + contentPane.add(textArea_2, BorderLayout.CENTER); + + JTextArea textArea_3 = new JTextArea(); + contentPane.add(textArea_3, BorderLayout.SOUTH); + + JTextArea textArea_4 = new JTextArea(); + contentPane.add(textArea_4, BorderLayout.EAST); + } + +} -- Gitee From c9aa192af5a9b1eb8494e8be5af90320959d1fd8 Mon Sep 17 00:00:00 2001 From: hjh Date: Tue, 8 Jun 2021 09:39:58 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E5=A5=BD=E4=BA=86?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E5=B8=83=E5=B1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/{babyNotepad => notepad}/P1.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/{babyNotepad => notepad}/P1.java (96%) diff --git a/src/babyNotepad/P1.java b/src/notepad/P1.java similarity index 96% rename from src/babyNotepad/P1.java rename to src/notepad/P1.java index 675e37f..7bd4f1e 100644 --- a/src/babyNotepad/P1.java +++ b/src/notepad/P1.java @@ -1,4 +1,4 @@ -package babyNotepad; +package notepad; import java.awt.BorderLayout; import java.awt.EventQueue; -- Gitee From 6540f1e7b5379e2224aaab32b5954e64a7f03cc9 Mon Sep 17 00:00:00 2001 From: hjh Date: Thu, 10 Jun 2021 09:37:05 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=EF=BC=8C=E5=A4=8D=E5=88=B6=EF=BC=8C=E6=89=93=E5=BC=80?= =?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/notepad/P1.java | 424 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 359 insertions(+), 65 deletions(-) diff --git a/src/notepad/P1.java b/src/notepad/P1.java index 7bd4f1e..aa6d184 100644 --- a/src/notepad/P1.java +++ b/src/notepad/P1.java @@ -3,15 +3,27 @@ package notepad; import java.awt.BorderLayout; import java.awt.EventQueue; -import javax.swing.JFrame; -import javax.swing.JPanel; import javax.swing.border.EmptyBorder; -import javax.swing.JMenuBar; -import javax.swing.JMenu; -import javax.swing.JMenuItem; -import javax.swing.JTextArea; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; -public class P1 extends JFrame { +import java.util.Calendar.*; +import java.util.Date; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.awt.*; +import javax.swing.*; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import java.awt.event.*; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +public class P1 extends JFrame implements DocumentListener,ActionListener,WindowListener{ private JPanel contentPane; @@ -34,118 +46,400 @@ public class P1 extends JFrame { /** * Create the frame. */ + public P1() { + + setTitle("记事本"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); + JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); - JMenu mnNewMenu = new JMenu("\u6587\u4EF6(F)"); - menuBar.add(mnNewMenu); - JMenuItem mntmNewMenuItem_1 = new JMenuItem("\u65B0\u5EFA(N)"); - mnNewMenu.add(mntmNewMenuItem_1); - JMenuItem mntmNewMenuItem_14 = new JMenuItem("\u65B0\u7A97\u53E3(W)"); - mnNewMenu.add(mntmNewMenuItem_14); + JMenu wenjian = new JMenu("\u6587\u4EF6(F)");//文件 + menuBar.add(wenjian); + wenjian.addActionListener(this); + + JMenuItem xinjian = new JMenuItem("\u65B0\u5EFA(N)");//新建 + wenjian.add(xinjian); + xinjian.addActionListener(this); + + JMenuItem xinchuangkou = new JMenuItem("\u65B0\u7A97\u53E3(W)");//新窗口 + wenjian.add(xinchuangkou); + xinchuangkou.addActionListener(this); + + JMenuItem dakai = new JMenuItem("\u6253\u5F00(O)");//打开 + wenjian.add(dakai); + dakai.addActionListener(this); - JMenuItem mntmNewMenuItem_15 = new JMenuItem("\u6253\u5F00(O)"); - mnNewMenu.add(mntmNewMenuItem_15); + JMenuItem baocun = new JMenuItem("\u4FDD\u5B58(S)");//保存 + wenjian.add(baocun); + baocun.addActionListener(this); - JMenuItem mntmNewMenuItem_16 = new JMenuItem("\u4FDD\u5B58(S)"); - mnNewMenu.add(mntmNewMenuItem_16); + JMenuItem lingcunwei = new JMenuItem("\u53E6\u5B58\u4E3A(A)");//另存为 + wenjian.add(lingcunwei); + lingcunwei.addActionListener(this); - JMenuItem mntmNewMenuItem_17 = new JMenuItem("\u53E6\u5B58\u4E3A(A)"); - mnNewMenu.add(mntmNewMenuItem_17); + JMenuItem tuichu = new JMenuItem("\u9000\u51FA(X)");//退出 + wenjian.add(tuichu); + tuichu.addActionListener(this); - JMenuItem mntmNewMenuItem_18 = new JMenuItem("\u9000\u51FA(X)"); - mnNewMenu.add(mntmNewMenuItem_18); + JMenu bianji = new JMenu("\u7F16\u8F91(E)");//编辑 + menuBar.add(bianji); + bianji.addActionListener(this); - JMenu mnNewMenu_1 = new JMenu("\u7F16\u8F91(E)"); - menuBar.add(mnNewMenu_1); + JMenuItem chexiao = new JMenuItem("\u64A4\u9500(U)");//撤销 + bianji.add(chexiao); + chexiao.addActionListener(this); - JMenuItem mntmNewMenuItem_2 = new JMenuItem("\u64A4\u9500(U)"); - mnNewMenu_1.add(mntmNewMenuItem_2); + JMenuItem jianqie = new JMenuItem("\u526A\u5207(T)");//剪切 + bianji.add(jianqie); + jianqie.addActionListener(this); - JMenuItem mntmNewMenuItem_3 = new JMenuItem("\u526A\u5207(T)"); - mnNewMenu_1.add(mntmNewMenuItem_3); + JMenuItem fuzhi = new JMenuItem("\u590D\u5236(C)");//复制 + bianji.add(fuzhi); + fuzhi.addActionListener(this); - JMenuItem mntmNewMenuItem_4 = new JMenuItem("\u590D\u5236(C)"); - mnNewMenu_1.add(mntmNewMenuItem_4); + JMenuItem zhantie = new JMenuItem("\u7C98\u8D34(P)");//粘贴 + bianji.add(zhantie); + zhantie.addActionListener(this); - JMenuItem mntmNewMenuItem_5 = new JMenuItem("\u7C98\u8D34(P)"); - mnNewMenu_1.add(mntmNewMenuItem_5); + JMenuItem shanchu = new JMenuItem("\u5220\u9664(L)");//删除 + bianji.add(shanchu); + shanchu.addActionListener(this); - JMenuItem mntmNewMenuItem_6 = new JMenuItem("\u5220\u9664(L)"); - mnNewMenu_1.add(mntmNewMenuItem_6); + JMenuItem chazhao = new JMenuItem("\u67E5\u627E(F)");//查找 + bianji.add(chazhao); + chazhao.addActionListener(this); - JMenuItem mntmNewMenuItem_7 = new JMenuItem("\u67E5\u627E(F)"); - mnNewMenu_1.add(mntmNewMenuItem_7); + JMenuItem chazhaoxiayige = new JMenuItem("\u67E5\u627E\u4E0B\u4E00\u4E2A(N)");//查找下一个 + bianji.add(chazhaoxiayige); + chazhaoxiayige.addActionListener(this); - JMenuItem mntmNewMenuItem_8 = new JMenuItem("\u67E5\u627E\u4E0B\u4E00\u4E2A(N)"); - mnNewMenu_1.add(mntmNewMenuItem_8); + JMenuItem chazhaoshangyige = new JMenuItem("\u67E5\u627E\u4E0A\u4E00\u4E2A(V)");//查找上一个 + bianji.add(chazhaoshangyige); + chazhaoshangyige.addActionListener(this); - JMenuItem mntmNewMenuItem_9 = new JMenuItem("\u67E5\u627E\u4E0A\u4E00\u4E2A(V)"); - mnNewMenu_1.add(mntmNewMenuItem_9); + JMenuItem tihuan = new JMenuItem("\u66FF\u6362(R)");//替换 + bianji.add(tihuan); + tihuan.addActionListener(this); - JMenuItem mntmNewMenuItem_10 = new JMenuItem("\u66FF\u6362(R)"); - mnNewMenu_1.add(mntmNewMenuItem_10); + JMenuItem zhuandao = new JMenuItem("\u8F6C\u5230(G)");//转到 + bianji.add(zhuandao); + zhuandao.addActionListener(this); - JMenuItem mntmNewMenuItem_11 = new JMenuItem("\u8F6C\u5230(G)"); - mnNewMenu_1.add(mntmNewMenuItem_11); + JMenuItem quanxuan = new JMenuItem("\u5168\u9009(A)");//全选 + bianji.add(quanxuan); + quanxuan.addActionListener(this); - JMenuItem mntmNewMenuItem_12 = new JMenuItem("\u5168\u9009(A)"); - mnNewMenu_1.add(mntmNewMenuItem_12); + JMenuItem shijian = new JMenuItem("\u65F6\u95F4/\u65E5\u671F(D)");//时间 + bianji.add(shijian); + shijian.addActionListener(this); - JMenuItem mntmNewMenuItem_13 = new JMenuItem("\u65F6\u95F4/\u65E5\u671F(D)"); - mnNewMenu_1.add(mntmNewMenuItem_13); + JMenu geshi = new JMenu("\u683C\u5F0F(O)");//格式 + menuBar.add(geshi); + geshi.addActionListener(this); - JMenu mnNewMenu_2 = new JMenu("\u683C\u5F0F(O)"); - menuBar.add(mnNewMenu_2); + JMenuItem zidonhuanhang = new JMenuItem("\u81EA\u52A8\u6362\u884C(W)");//自动换行 + geshi.add(zidonhuanhang); + zidonhuanhang.addActionListener(this); - JMenuItem mntmNewMenuItem_19 = new JMenuItem("\u81EA\u52A8\u6362\u884C(W)"); - mnNewMenu_2.add(mntmNewMenuItem_19); + JMenuItem ziti = new JMenuItem("\u5B57\u4F53(F)\u2026");//字体 + geshi.add(ziti); + ziti.addActionListener(this); - JMenuItem mntmNewMenuItem_20 = new JMenuItem("\u5B57\u4F53(F)\u2026"); - mnNewMenu_2.add(mntmNewMenuItem_20); + JMenu chakan = new JMenu("\u67E5\u770B(V)");//查看 + menuBar.add(chakan); + JMenuItem zhuangtailan = new JMenuItem("\u72B6\u6001\u680F(S)");//状态栏 + chakan.add(zhuangtailan); - JMenu mnNewMenu_3 = new JMenu("\u67E5\u770B(V)"); - menuBar.add(mnNewMenu_3); + JMenuItem suofang = new JMenuItem("\u7F29\u653E(Z)");//缩放 + chakan.add(suofang); + chakan.addActionListener(this); - JMenuItem mntmNewMenuItem_21 = new JMenuItem("\u7F29\u653E(Z)"); - mnNewMenu_3.add(mntmNewMenuItem_21); + JMenu bangzhu = new JMenu("\u5E2E\u52A9(H)");//帮助 + menuBar.add(bangzhu); + bangzhu.addActionListener(this); + + JMenuItem chakanbangzhu = new JMenuItem("\u67E5\u770B\u5E2E\u52A9(H)");//查看帮助 + bangzhu.add(chakanbangzhu); + chakanbangzhu.addActionListener(this); + + JMenuItem guanyujishiben = new JMenuItem("\u5173\u4E8E\u8BB0\u4E8B\u672C(A)");//关于记事本 + bangzhu.add(guanyujishiben); + guanyujishiben.addActionListener(this); - JMenuItem mntmNewMenuItem_22 = new JMenuItem("\u72B6\u6001\u680F(S)"); - mnNewMenu_3.add(mntmNewMenuItem_22); - JMenu mnNewMenu_4 = new JMenu("\u5E2E\u52A9(H)"); - menuBar.add(mnNewMenu_4); - JMenuItem mntmNewMenuItem_23 = new JMenuItem("\u67E5\u770B\u5E2E\u52A9(H)"); - mnNewMenu_4.add(mntmNewMenuItem_23); - JMenuItem mntmNewMenuItem = new JMenuItem("\u5173\u4E8E\u8BB0\u4E8B\u672C(A)"); - mnNewMenu_4.add(mntmNewMenuItem); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JTextArea textArea = new JTextArea(); + + boolean wasSaved = true;//判断当前文件是否已保存的状态 + + contentPane.add(textArea, BorderLayout.NORTH); JTextArea textArea_1 = new JTextArea(); contentPane.add(textArea_1, BorderLayout.WEST); + (textArea_1.getDocument()).addDocumentListener(this); + getContentPane().add(new JScrollPane(textArea_1)); JTextArea textArea_2 = new JTextArea(); contentPane.add(textArea_2, BorderLayout.CENTER); + (textArea_2.getDocument()).addDocumentListener(this); + getContentPane().add(new JScrollPane(textArea_2)); JTextArea textArea_3 = new JTextArea(); contentPane.add(textArea_3, BorderLayout.SOUTH); + (textArea_3.getDocument()).addDocumentListener(this); + getContentPane().add(new JScrollPane(textArea_3)); JTextArea textArea_4 = new JTextArea(); contentPane.add(textArea_4, BorderLayout.EAST); + (textArea_4.getDocument()).addDocumentListener(this); + getContentPane().add(new JScrollPane(textArea_4)); + + + xinjian.setAccelerator(KeyStroke.getKeyStroke('N',InputEvent.CTRL_MASK)); //快捷键 + xinchuangkou.setAccelerator(KeyStroke.getKeyStroke('W',InputEvent.CTRL_MASK)); + dakai.setAccelerator(KeyStroke.getKeyStroke('O',InputEvent.CTRL_MASK)); + baocun.setAccelerator(KeyStroke.getKeyStroke('S',InputEvent.CTRL_MASK)); + lingcunwei.setAccelerator(KeyStroke.getKeyStroke('A',InputEvent.CTRL_MASK)); + tuichu.setAccelerator(KeyStroke.getKeyStroke('X',InputEvent.CTRL_MASK)); + + chexiao.setAccelerator(KeyStroke.getKeyStroke('U',InputEvent.CTRL_MASK)); + jianqie.setAccelerator(KeyStroke.getKeyStroke('T',InputEvent.CTRL_MASK)); + fuzhi.setAccelerator(KeyStroke.getKeyStroke('C',InputEvent.CTRL_MASK)); + zhantie.setAccelerator(KeyStroke.getKeyStroke('V',InputEvent.CTRL_MASK)); + shanchu.setAccelerator(KeyStroke.getKeyStroke('L',InputEvent.CTRL_MASK)); + chazhao.setAccelerator(KeyStroke.getKeyStroke('F',InputEvent.CTRL_MASK)); + chazhaoxiayige.setAccelerator(KeyStroke.getKeyStroke('I',InputEvent.CTRL_MASK)); + chazhaoshangyige.setAccelerator(KeyStroke.getKeyStroke('P',InputEvent.CTRL_MASK)); + tihuan.setAccelerator(KeyStroke.getKeyStroke('R',InputEvent.CTRL_MASK)); + zhuandao.setAccelerator(KeyStroke.getKeyStroke('G',InputEvent.CTRL_MASK)); + quanxuan.setAccelerator(KeyStroke.getKeyStroke('K',InputEvent.CTRL_MASK)); + shijian.setAccelerator(KeyStroke.getKeyStroke('D',InputEvent.CTRL_MASK)); + + + zidonhuanhang.setAccelerator(KeyStroke.getKeyStroke('W',InputEvent.CTRL_MASK)); + ziti.setAccelerator(KeyStroke.getKeyStroke('Q',InputEvent.CTRL_MASK)); + + xinjian.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e){ + textArea.setText(""); + }//新建功能 + }); + xinjian.setMnemonic('N'); + xinjian.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK)); + FileDialog open; + FileDialog save; + open = new FileDialog(this,"打开(O)",FileDialog.LOAD); + save = new FileDialog(this,"另存为(A)",FileDialog.SAVE); + dakai.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + open.setVisible(true); + try{ BufferedReader in = new BufferedReader(new FileReader(open.getDirectory() +open.getFile())); + String str = null; + while ((str = in.readLine()) != null){ + System.out.println(str); + textArea.append(str + "\n"); + } + in.close(); + } + catch (IOException er1) { + throw new RuntimeException("文件打开失败!"); + } + } + });//打开功能 + + baocun.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + save.setVisible(true); + try{ FileOutputStream out = new FileOutputStream(save.getDirectory() + save.getFile() + ".txt"); + String str = textArea.getText(); + out.write(str.getBytes()); + out.close(); + } + catch (IOException er1) { + throw new RuntimeException("文件保存失败!"); + } + } + });//保存功能 + + fuzhi.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e) { + textArea.copy(); + }});//复制功能 + + } + +private void save() { + // TODO 自动生成的方法存根 + } +void init() { +this.addWindowListener(this); + } + +@Override +public void windowOpened(WindowEvent e) { + // TODO 自动生成的方法存根 + +} + +@Override +public void windowClosing(WindowEvent e) { + // TODO 自动生成的方法存根 + +} + +@Override +public void windowClosed(WindowEvent e) { + // TODO 自动生成的方法存根 + +} + +@Override +public void windowIconified(WindowEvent e) { + // TODO 自动生成的方法存根 + +} + +@Override +public void windowDeiconified(WindowEvent e) { + // TODO 自动生成的方法存根 + +} + +@Override +public void windowActivated(WindowEvent e) { + // TODO 自动生成的方法存根 + +} + +@Override +public void windowDeactivated(WindowEvent e) { + // TODO 自动生成的方法存根 + +} + +@Override +public void actionPerformed(ActionEvent e) { + // TODO 自动生成的方法存根 + +} + +@Override +public void insertUpdate(DocumentEvent e) { + // TODO 自动生成的方法存根 + +} + +@Override +public void removeUpdate(DocumentEvent e) { + // TODO 自动生成的方法存根 + } + +@Override +public void changedUpdate(DocumentEvent e) { + // TODO 自动生成的方法存根 + +}} + + + + +/*public void save(){ + + JFileChooser fileDialog=new JFileChooser(); + BufferedReader in; + FileReader fileReader; + BufferedWriter out; + FileWriter fileWriter; + int flag=0; + int flag1=0; + File dir; + String name; + init(); + //setDefaultCloseOperation(int operation); + setVisible(true); + + int state=fileDialog.showSaveDialog(this); + + if(state==JFileChooser.APPROVE_OPTION){ + try{ + dir=fileDialog.getCurrentDirectory(); + + name=fileDialog.getSelectedFile().getName(); + + File file=new File(dir,name); + + fileWriter=new FileWriter(file); + + out=new BufferedWriter(fileWriter); + + // out.write(textArea.getText()); + + out.close(); + + fileWriter.close(); + + } + + catch(IOException exp){} + + + + } + +/*public void actionPerformed(ActionEvent e){ +if(e.getSource()==item1){ + int n=JOptionPane.showConfirmDialog(this,"文件还未保存,确认是否保存","确认对话框",JOptionPane.YES_NO_OPTION); + if(n==JOptionPane.YES_OPTION) + save(); + else if(n==JOptionPane.NO_OPTION) + + wenjian.setText(null); + +if(e.getSource()==baocun)//保存 + { + +int flag; +int flag1; +if(flag==1&&flag1==0){ + save();flag=0;} + +else if(flag==1&&flag1==1){ +try{ +String dir; +String name; +File file=new File(dir,name); + + FileWriter fileWriter = new FileWriter(file); + + BufferedWriter out = new BufferedWriter(fileWriter); + + AbstractButton area; + out.write(area.getText()); } + + catch(IOException exp){} + +flag=0; + + } + +flag1=1; + +}}*/ + -- Gitee From d7f424ed8188d8fdb0c46a566e328241e50a6bbf Mon Sep 17 00:00:00 2001 From: hjh Date: Thu, 10 Jun 2021 13:14:14 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E9=87=8D=E5=8F=91=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=8F=A6=E5=AD=98=E4=B8=BA?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nullnull.txt | 0 src/notepad/P1.java | 68 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 nullnull.txt diff --git a/nullnull.txt b/nullnull.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/notepad/P1.java b/src/notepad/P1.java index aa6d184..96699b9 100644 --- a/src/notepad/P1.java +++ b/src/notepad/P1.java @@ -262,6 +262,33 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window });//打开功能 baocun.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + save.setVisible(true); + try { + FileOutputStream out = new FileOutputStream(save.getDirectory() + save.getFile(), false);//这里flase是覆盖,否则是加到原文件末尾 + String s = textArea.getText(); + byte[] b = s.getBytes(); + out.write(b); + out.close(); + } catch (IOException ex) { + System.out.println(ex); + }}});//保存功能 + + /*baocun.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + save.setVisible(true); + try{ FileOutputStream out = new FileOutputStream(save.getDirectory() + save.getFile() + ".txt"); + String str = textArea.getText(); + out.write(str.getBytes()); + out.close(); + } + catch (IOException er1) { + throw new RuntimeException("文件保存失败!"); + } + } + });//保存功能*/ + + lingcunwei.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { save.setVisible(true); try{ FileOutputStream out = new FileOutputStream(save.getDirectory() + save.getFile() + ".txt"); @@ -273,7 +300,7 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window throw new RuntimeException("文件保存失败!"); } } - });//保存功能 + });//另存为功能 fuzhi.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { @@ -282,6 +309,45 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window } + + + + + /* JPopupMenu popupmenuR=new JPopupMenu(); + textArea.add(popupmenuR); + textArea.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + if(e.getModifiers()==MouseEvent.BUTTON3_MASK){ + popupmenuR.show(textArea, e.getX(), e.getY()); + } + } + }); + JMenuItem Undo1=new JMenuItem("撤消(U)"); + JMenuItem Cut1=new JMenuItem("剪切(T)"); + JMenuItem Copy1=new JMenuItem("复制(C)"); + JMenuItem Paste1=new JMenuItem("粘贴(P)"); + JMenuItem Delete1=new JMenuItem("删除(L)"); + JMenuItem SelectAll=new JMenuItem("全选(A)"); + // + Undo1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); + Cut1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); + Copy1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); + Paste1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); + Delete1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); + SelectAll.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); + // + popupmenuR.add(Undo1); + popupmenuR.addSeparator(); + popupmenuR.add(Copy1); + popupmenuR.addSeparator(); + popupmenuR.add(Cut1); + popupmenuR.addSeparator(); + popupmenuR.add(Paste1); + popupmenuR.addSeparator(); + popupmenuR.add(Delete1); + popupmenuR.addSeparator(); + popupmenuR.add(SelectAll); + */ private void save() { // TODO 自动生成的方法存根 -- Gitee From 777826a498f74aa9e79f1349718bf6a412c52143 Mon Sep 17 00:00:00 2001 From: hjh Date: Thu, 10 Jun 2021 18:38:28 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E6=9F=A5=E6=89=BE=20=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nullnull | 1 + nullnull.txt | 1 + src/notepad/P1.java | 108 ++++++++++++++------ src/notepad/P2.java | 205 ++++++++++++++++++++++++++++++++++++++ src/notepad/P3.java | 148 ++++++++++++++++++++++++++++ src/notepad/P4.java | 233 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 664 insertions(+), 32 deletions(-) create mode 100644 nullnull create mode 100644 src/notepad/P2.java create mode 100644 src/notepad/P3.java create mode 100644 src/notepad/P4.java diff --git a/nullnull b/nullnull new file mode 100644 index 0000000..85c8dfa --- /dev/null +++ b/nullnull @@ -0,0 +1 @@ +哈哈哈我 diff --git a/nullnull.txt b/nullnull.txt index e69de29..85c8dfa 100644 --- a/nullnull.txt +++ b/nullnull.txt @@ -0,0 +1 @@ +哈哈哈我 diff --git a/src/notepad/P1.java b/src/notepad/P1.java index 96699b9..9e3ada4 100644 --- a/src/notepad/P1.java +++ b/src/notepad/P1.java @@ -6,6 +6,7 @@ import java.awt.EventQueue; import javax.swing.border.EmptyBorder; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; +import javax.swing.undo.UndoManager; import java.util.Calendar.*; import java.util.Date; @@ -26,7 +27,8 @@ import java.io.IOException; public class P1 extends JFrame implements DocumentListener,ActionListener,WindowListener{ private JPanel contentPane; - + JFrame f=new JFrame(); + JFrame jframe=new JFrame(); /** * Launch the application. */ @@ -116,14 +118,6 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window bianji.add(chazhao); chazhao.addActionListener(this); - JMenuItem chazhaoxiayige = new JMenuItem("\u67E5\u627E\u4E0B\u4E00\u4E2A(N)");//查找下一个 - bianji.add(chazhaoxiayige); - chazhaoxiayige.addActionListener(this); - - JMenuItem chazhaoshangyige = new JMenuItem("\u67E5\u627E\u4E0A\u4E00\u4E2A(V)");//查找上一个 - bianji.add(chazhaoshangyige); - chazhaoshangyige.addActionListener(this); - JMenuItem tihuan = new JMenuItem("\u66FF\u6362(R)");//替换 bianji.add(tihuan); tihuan.addActionListener(this); @@ -182,13 +176,12 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window setContentPane(contentPane); JTextArea textArea = new JTextArea(); + contentPane.add(textArea, BorderLayout.CENTER); - boolean wasSaved = true;//判断当前文件是否已保存的状态 - - contentPane.add(textArea, BorderLayout.NORTH); + boolean wasSaved = true; - JTextArea textArea_1 = new JTextArea(); + /*JTextArea textArea_1 = new JTextArea(); contentPane.add(textArea_1, BorderLayout.WEST); (textArea_1.getDocument()).addDocumentListener(this); getContentPane().add(new JScrollPane(textArea_1)); @@ -206,7 +199,7 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window JTextArea textArea_4 = new JTextArea(); contentPane.add(textArea_4, BorderLayout.EAST); (textArea_4.getDocument()).addDocumentListener(this); - getContentPane().add(new JScrollPane(textArea_4)); + getContentPane().add(new JScrollPane(textArea_4));*/ xinjian.setAccelerator(KeyStroke.getKeyStroke('N',InputEvent.CTRL_MASK)); //快捷键 @@ -221,9 +214,7 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window fuzhi.setAccelerator(KeyStroke.getKeyStroke('C',InputEvent.CTRL_MASK)); zhantie.setAccelerator(KeyStroke.getKeyStroke('V',InputEvent.CTRL_MASK)); shanchu.setAccelerator(KeyStroke.getKeyStroke('L',InputEvent.CTRL_MASK)); - chazhao.setAccelerator(KeyStroke.getKeyStroke('F',InputEvent.CTRL_MASK)); - chazhaoxiayige.setAccelerator(KeyStroke.getKeyStroke('I',InputEvent.CTRL_MASK)); - chazhaoshangyige.setAccelerator(KeyStroke.getKeyStroke('P',InputEvent.CTRL_MASK)); + chazhao.setAccelerator(KeyStroke.getKeyStroke('F',InputEvent.CTRL_MASK)); tihuan.setAccelerator(KeyStroke.getKeyStroke('R',InputEvent.CTRL_MASK)); zhuandao.setAccelerator(KeyStroke.getKeyStroke('G',InputEvent.CTRL_MASK)); quanxuan.setAccelerator(KeyStroke.getKeyStroke('K',InputEvent.CTRL_MASK)); @@ -244,6 +235,7 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window FileDialog save; open = new FileDialog(this,"打开(O)",FileDialog.LOAD); save = new FileDialog(this,"另存为(A)",FileDialog.SAVE); + dakai.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { open.setVisible(true); @@ -273,20 +265,6 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window } catch (IOException ex) { System.out.println(ex); }}});//保存功能 - - /*baocun.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - save.setVisible(true); - try{ FileOutputStream out = new FileOutputStream(save.getDirectory() + save.getFile() + ".txt"); - String str = textArea.getText(); - out.write(str.getBytes()); - out.close(); - } - catch (IOException er1) { - throw new RuntimeException("文件保存失败!"); - } - } - });//保存功能*/ lingcunwei.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -302,18 +280,84 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window } });//另存为功能 + tuichu.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + System.exit(0); + } + });//退出功能 + + UndoManager undo = new UndoManager(); + textArea.getDocument().addUndoableEditListener(undo); + chexiao.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e) { + if(undo.canUndo()) + undo.undo(); + } + });//撤销功能 + + jianqie.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + textArea.cut(); + } + });//剪切功能 + fuzhi.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { textArea.copy(); }});//复制功能 + zhantie.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + textArea.paste(); + } + }); //粘帖功能 + + shanchu.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd()); + } + });//删除功能 + + chazhao.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + new P3(f,textArea); + } + });//查找功能 + + tihuan.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e) { + new P2(jframe,textArea); + } });//替换功能 + + quanxuan.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e) { + textArea.selectAll(); + } + });//全选功能 + + ziti.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e) { + new P4(); + }}); } + + + + + + + - /* JPopupMenu popupmenuR=new JPopupMenu(); + protected void find() { + // TODO 自动生成的方法存根 + + } + +/* JPopupMenu popupmenuR=new JPopupMenu(); textArea.add(popupmenuR); textArea.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { diff --git a/src/notepad/P2.java b/src/notepad/P2.java new file mode 100644 index 0000000..a72fa05 --- /dev/null +++ b/src/notepad/P2.java @@ -0,0 +1,205 @@ +package notepad; + +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.swing.ButtonGroup; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JDialog; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JRadioButton; +import javax.swing.JTextArea; +import javax.swing.WindowConstants; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; + +public class P2 { + public P2(JFrame jframe,JTextArea textArea) { + JDialog jdlg = new JDialog(jframe, "替换", true); + jdlg.setSize(453, 200); + jdlg.setLocationRelativeTo(null); + jdlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); + + JPanel panel=new JPanel(); + panel.setLayout(null); + JLabel label=new JLabel("查找内容:"); + JLabel label2=new JLabel("替换为:"); + JLabel count=new JLabel("找到0个"); + JTextArea ftext=new JTextArea(); + JTextArea rtext=new JTextArea(); + JButton findnext=new JButton("查找下一个"); + JButton replace=new JButton("替换"); + JButton allreplace=new JButton("全部替换"); + JButton no=new JButton("取消"); + JCheckBox matchcase=new JCheckBox("区分大小写"); + //创建两个单选按钮 + JRadioButton up = new JRadioButton("向上替换"); + JRadioButton down = new JRadioButton("向下替换"); + //创建按钮组,把两个单选按钮添加到该组 + ButtonGroup btnGroup = new ButtonGroup(); + btnGroup.add(up); + btnGroup.add(down); + // 设置第一个单选按钮选中 + up.setSelected(true); + + label.setBounds(20,20,93,22); + label2.setBounds(20,45,93,22); + count.setBounds(20, 55, 80, 85); + ftext.setBounds(100,20,200,22); + rtext.setBounds(100,48,200,22); + findnext.setBounds(320, 18, 105, 32); + replace.setBounds(320, 55, 105, 32); + allreplace.setBounds(320, 92, 105, 32); + no.setBounds(320, 128, 105, 32); + matchcase.setBounds(15, 120, 100, 55); + up.setBounds(130, 120, 80, 55); + down.setBounds(230, 120, 80, 55); + + //初始化按键状态 + findnext.setEnabled(false); + replace.setEnabled(false); + allreplace.setEnabled(false); + + //查找文本框的监听器,改变按钮状态 + ftext.getDocument().addDocumentListener(new DocumentListener() { + @Override + public void removeUpdate(DocumentEvent e) { + if(ftext.getText().equals("")) { + findnext.setEnabled(false); + replace.setEnabled(false); + allreplace.setEnabled(false); + } + + } + @Override + public void insertUpdate(DocumentEvent e) { + findnext.setEnabled(true); + replace.setEnabled(true); + allreplace.setEnabled(true); + } + @Override + public void changedUpdate(DocumentEvent e) { + // TODO 自动生成的方法存根 + } + }); + //查找下一个的监听器 + findnext.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + int c=0; + int a = 0, b = 0; + int FindStartPos = textArea.getCaretPosition(); + String strA,strB; + // 选中区分大小写,大小写转换 + if (matchcase.isSelected()) { + strA = textArea.getText(); + strB = ftext.getText(); + } else { + strA = textArea.getText().toLowerCase(); + strB = ftext.getText().toLowerCase(); + } + //向上查找,否则向下查找 + if (up.isSelected()) { + a = strA.lastIndexOf(strB, FindStartPos - ftext.getText().length() - 1); + } else if (down.isSelected()) { + a = strA.indexOf(strB, FindStartPos - ftext.getText().length() + 1); + } + //查找到边界 + if (a > -1) { + if (up.isSelected()) { + textArea.setCaretPosition(a); + b = ftext.getText().length(); + textArea.select(a, a + b); + } else if (down.isSelected()) { + textArea.setCaretPosition(a); + b = ftext.getText().length(); + textArea.select(a, a + b); + } + } else { + JOptionPane.showMessageDialog(null, "找不到查找的内容", + "查找", JOptionPane.INFORMATION_MESSAGE); + } + //显示关键字的总数量 + Pattern p=Pattern.compile(ftext.getText()); + Matcher m=p.matcher(textArea.getText()); + while(m.find()) { + c++; + } + count.setText("找到"+c+"个"); + } + }); + + //替换的监听器 + replace.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (rtext.getText().length() == 0 && textArea.getSelectedText() != null) { + textArea.replaceSelection(""); + } + if (rtext.getText().length() > 0 && textArea.getSelectedText() != null) { + textArea.replaceSelection(rtext.getText()); + } + } + }); + //替换全部的监听器 + allreplace.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + //显示关键字的总数量 + int c=0; + Pattern p=Pattern.compile(ftext.getText()); + Matcher m=p.matcher(textArea.getText()); + while(m.find()) { + c++; + } + count.setText("替换"+c+"个"); + //替换全部关键字 + String str=textArea.getText(); + str=str.replace(ftext.getText(), rtext.getText()); + textArea.setText(str); + + } + }); + + //取消的监听器 + no.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + jdlg.setVisible(false); + } + }); + + label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16)); + label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16)); + count.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16)); + ftext.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16)); + rtext.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16)); + findnext.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 13)); + replace.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 13)); + allreplace.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 13)); + no.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 13)); + + panel.add(label); + panel.add(label2); + panel.add(count); + panel.add(ftext); + panel.add(rtext); + panel.add(findnext); + panel.add(no); + panel.add(replace); + panel.add(allreplace); + panel.add(matchcase); + panel.add(up); + panel.add(down); + + jdlg.setResizable(false); + jdlg.setContentPane(panel); + jdlg.setVisible(true); + } +} \ No newline at end of file diff --git a/src/notepad/P3.java b/src/notepad/P3.java new file mode 100644 index 0000000..ec66fcc --- /dev/null +++ b/src/notepad/P3.java @@ -0,0 +1,148 @@ +package notepad; + + + +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.swing.ButtonGroup; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JDialog; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JRadioButton; +import javax.swing.JTextArea; +import javax.swing.WindowConstants; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; + +public class P3 { + public P3(JFrame f,JTextArea textArea) { + JDialog jdlg = new JDialog(f, "查找(F)", true); + jdlg.setSize(453, 150); + jdlg.setLocationRelativeTo(null); + jdlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); + + JPanel panel=new JPanel(); + panel.setLayout(null); + JLabel label=new JLabel("查找内容:"); + JLabel count=new JLabel("找到0个"); + JTextArea ftext=new JTextArea(); + JButton findnext=new JButton("查找下一个"); + JButton no=new JButton("取消"); + JCheckBox matchcase=new JCheckBox("区分大小写"); + //创建两个单选按钮 + JRadioButton up = new JRadioButton("向上查找"); + JRadioButton down = new JRadioButton("向下查找"); + //创建按钮组,把两个单选按钮添加到该组 + ButtonGroup btnGroup = new ButtonGroup(); + btnGroup.add(up); + btnGroup.add(down); + // 设置第一个单选按钮选中 + up.setSelected(true); + + label.setBounds(20,20,93,22); + count.setBounds(20, 55, 80, 35); + ftext.setBounds(100,20,200,22); + findnext.setBounds(320, 18, 105, 32); + no.setBounds(320, 53, 105, 32); + matchcase.setBounds(15, 90, 100, 25); + up.setBounds(130, 90, 80, 25); + down.setBounds(230, 90, 80, 25); + + //初始化查找按键 + findnext.setEnabled(false); + //设置文本框和按钮的状态 + ftext.getDocument().addDocumentListener(new DocumentListener() { + @Override + public void removeUpdate(DocumentEvent e) { + if(ftext.getText().equals("")) + findnext.setEnabled(false); + } + @Override + public void insertUpdate(DocumentEvent e) { + findnext.setEnabled(true); + } + @Override + public void changedUpdate(DocumentEvent e) { + // TODO 自动生成的方法存根 + } + }); + //查找下一个的监听器 + findnext.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + int c=0; + int a = 0, b = 0; + int FindStartPos = textArea.getCaretPosition(); + String strA,strB; + // 选中区分大小写,大小写转换 + if (matchcase.isSelected()) { + strA = textArea.getText(); + strB = ftext.getText(); + } else { + strA = textArea.getText().toLowerCase(); + strB = ftext.getText().toLowerCase(); + } + //向上查找,否则向下查找 + if (up.isSelected()) { + a = strA.lastIndexOf(strB, FindStartPos - ftext.getText().length() - 1); + } else if (down.isSelected()) { + a = strA.indexOf(strB, FindStartPos - ftext.getText().length() + 1); + } + //查找到边界 + if (a > -1) { + if (up.isSelected()) { + textArea.setCaretPosition(a); + b = ftext.getText().length(); + textArea.select(a, a + b); + } else if (down.isSelected()) { + textArea.setCaretPosition(a); + b = ftext.getText().length(); + textArea.select(a, a + b); + } + } else { + JOptionPane.showMessageDialog(null, "找不到查找的内容", + "查找", JOptionPane.INFORMATION_MESSAGE); + } + //显示关键字的总数量 + Pattern p=Pattern.compile(ftext.getText()); + Matcher m=p.matcher(textArea.getText()); + while(m.find()) { + c++; + } + count.setText("找到"+c+"个"); + } + }); + //取消的监听器 + no.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + jdlg.setVisible(false); + } + }); + + label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16)); + count.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16)); + ftext.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16)); + findnext.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 13)); + no.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 13)); + + panel.add(label); + panel.add(count); + panel.add(ftext); + panel.add(findnext); + panel.add(no); + panel.add(matchcase); + panel.add(up); + panel.add(down); + + jdlg.setResizable(false); + jdlg.setContentPane(panel); + jdlg.setVisible(true); + } +} \ No newline at end of file diff --git a/src/notepad/P4.java b/src/notepad/P4.java new file mode 100644 index 0000000..3dc2f54 --- /dev/null +++ b/src/notepad/P4.java @@ -0,0 +1,233 @@ +package notepad; + + + +import java.awt.*; + +import java.awt.event.*; + +import javax.swing.*; + +import javax.swing.event.*; + +public class P4 extends JDialog implements ActionListener,ListSelectionListener{ +String ss,ss3; + +JTextArea ta1; + +String fontName="宋体" ; + +int fontStyle=Font.PLAIN ; + +int fontSize=16; + +Font font=new Font(fontName,fontStyle,fontSize); + +GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); + +String fontName1[]=ge.getAvailableFontFamilyNames(); + +JList list1=new JList(fontName1); + +String name[]={"常规","粗体","倾斜","粗偏斜体"}; + +JList list2=new JList(name); + +String size[]={"12","14","16","18","20","22"}; + +JList list3=new JList(size); + +JButton button1=new JButton("确定"); + +JButton button2=new JButton("取消"); + +JPanel p =new JPanel(); + +JPanel p1=new JPanel(); + +JPanel p2=new JPanel(); + +JPanel p3=new JPanel(); + + + +JLabel l1=new JLabel("字体(F):"); + +JLabel l2=new JLabel("字形(Y):"); + +JLabel l3=new JLabel("大小(S):"); + +JTextField f1=new JTextField("宋体",10); + +JTextField f2=new JTextField("常规",10); + +JTextField f3=new JTextField("12",10); + + P4(){ +init(); + +setBounds(560,300,560,400); + +setResizable(false); + +} + + + +public void setTa(JTextArea ta2){ +ta1=ta2; + +} + +void init(){ +p.setLayout(null); + +p1.setLayout(new BorderLayout()); + +p2.setLayout(new BorderLayout()); + +p3.setLayout(new BorderLayout()); + +l1.setBounds(50,30,80,30); + +l2.setBounds(240,30,80,30); + +l3.setBounds(400,30,80,30); + +f1.setBounds(50,60,110,25); + +f2.setBounds(240,60,80,25); + +f3.setBounds(400,60,80,25); + +p1.setBounds(50,90,110,200); + +p2.setBounds(240,90,80,200); + +p3.setBounds(400,90,80,200); + +button1.setBounds(360, 300, 70, 40); + +button2.setBounds(460, 300, 70, 40); + +p.add(l1); + +p.add(l2); + +p.add(l3); + +p.add(f1); + +p.add(f2); + +p.add(f3); + +p1.add( new JScrollPane(list1)); + +p2.add(new JScrollPane(list2)); + + p3.add(new JScrollPane(list3)); + +p.add(p1,BorderLayout.CENTER); + + p.add(p2,BorderLayout.CENTER); + + p.add(p3,BorderLayout.CENTER); + +p.add(button1); + +p.add(button2); + + + +add(p); + +list1.addListSelectionListener(this); + +list2.addListSelectionListener(this); + +list3.addListSelectionListener(this); + +button1.addActionListener(this); + +button2.addActionListener(this); + +} + +public void actionPerformed(ActionEvent e){ +if(e.getSource()==button1){ +font=new Font(fontName,fontStyle,fontSize); + +ta1.setFont(font); + +} + +if(e.getSource()==button2){ + this.dispose(); + + f1.setText("宋体"); + + f2.setText("常规"); + + f3.setText("12");//再次修改字体时不起作用 + +} + +} + + + + + +public void valueChanged (ListSelectionEvent e){ + +if(e.getSource()==list1){ +int m=list1.getSelectedIndex(); + +fontName=fontName1[m]; + +f1.setText(fontName); + +} + +if(e.getSource()==list2){ +int m=list2.getSelectedIndex(); + +ss=name[m]; + +if(ss.equals("粗体")) + +fontStyle=Font.BOLD; + +if(ss.equals("倾斜")) + +fontStyle=Font.ITALIC ; + +if(ss.equals("常规")) + +fontStyle=Font.PLAIN; + +if(ss.equals("粗偏斜体")) + +fontStyle=Font.BOLD+Font.ITALIC; + + f2.setText(ss); + +} + +if(e.getSource()==list3){ + + +int m=list3.getSelectedIndex(); + +ss3=size[m]; + +fontSize=Integer.parseInt(ss3); + +f3.setText(ss3); + +} + +} + +} \ No newline at end of file -- Gitee From c6c7926d0147d20fef65a7b8c836f8e3de368218 Mon Sep 17 00:00:00 2001 From: hjh Date: Thu, 10 Jun 2021 23:11:21 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E4=BA=86=E8=87=AA=E5=8A=A8=E6=8D=A2?= =?UTF-8?q?=E8=A1=8C=EF=BC=8C=E5=85=B3=E4=BA=8E=E8=AE=B0=E4=BA=8B=E6=9C=AC?= =?UTF-8?q?=EF=BC=8C=E6=9F=A5=E7=9C=8B=E5=B8=AE=E5=8A=A9=EF=BC=8C=E5=AD=97?= =?UTF-8?q?=E4=BD=93=E8=AE=BE=E7=BD=AE=E7=AD=89=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nullnull | 1 - src/notepad/P1.java | 129 ++++++++++++---- src/notepad/P4.java | 366 +++++++++++++++++--------------------------- 3 files changed, 239 insertions(+), 257 deletions(-) diff --git a/nullnull b/nullnull index 85c8dfa..e69de29 100644 --- a/nullnull +++ b/nullnull @@ -1 +0,0 @@ -哈哈哈我 diff --git a/src/notepad/P1.java b/src/notepad/P1.java index 9e3ada4..ad2ed11 100644 --- a/src/notepad/P1.java +++ b/src/notepad/P1.java @@ -6,6 +6,7 @@ import java.awt.EventQueue; import javax.swing.border.EmptyBorder; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; +import javax.swing.text.TextAction; import javax.swing.undo.UndoManager; import java.util.Calendar.*; @@ -70,10 +71,6 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window wenjian.add(xinjian); xinjian.addActionListener(this); - JMenuItem xinchuangkou = new JMenuItem("\u65B0\u7A97\u53E3(W)");//新窗口 - wenjian.add(xinchuangkou); - xinchuangkou.addActionListener(this); - JMenuItem dakai = new JMenuItem("\u6253\u5F00(O)");//打开 wenjian.add(dakai); dakai.addActionListener(this); @@ -122,10 +119,6 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window bianji.add(tihuan); tihuan.addActionListener(this); - JMenuItem zhuandao = new JMenuItem("\u8F6C\u5230(G)");//转到 - bianji.add(zhuandao); - zhuandao.addActionListener(this); - JMenuItem quanxuan = new JMenuItem("\u5168\u9009(A)");//全选 bianji.add(quanxuan); quanxuan.addActionListener(this); @@ -138,23 +131,10 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window menuBar.add(geshi); geshi.addActionListener(this); - JMenuItem zidonhuanhang = new JMenuItem("\u81EA\u52A8\u6362\u884C(W)");//自动换行 - geshi.add(zidonhuanhang); - zidonhuanhang.addActionListener(this); - JMenuItem ziti = new JMenuItem("\u5B57\u4F53(F)\u2026");//字体 geshi.add(ziti); ziti.addActionListener(this); - JMenu chakan = new JMenu("\u67E5\u770B(V)");//查看 - menuBar.add(chakan); - JMenuItem zhuangtailan = new JMenuItem("\u72B6\u6001\u680F(S)");//状态栏 - chakan.add(zhuangtailan); - - JMenuItem suofang = new JMenuItem("\u7F29\u653E(Z)");//缩放 - chakan.add(suofang); - chakan.addActionListener(this); - JMenu bangzhu = new JMenu("\u5E2E\u52A9(H)");//帮助 menuBar.add(bangzhu); bangzhu.addActionListener(this); @@ -202,8 +182,7 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window getContentPane().add(new JScrollPane(textArea_4));*/ - xinjian.setAccelerator(KeyStroke.getKeyStroke('N',InputEvent.CTRL_MASK)); //快捷键 - xinchuangkou.setAccelerator(KeyStroke.getKeyStroke('W',InputEvent.CTRL_MASK)); + xinjian.setAccelerator(KeyStroke.getKeyStroke('N',InputEvent.CTRL_MASK)); dakai.setAccelerator(KeyStroke.getKeyStroke('O',InputEvent.CTRL_MASK)); baocun.setAccelerator(KeyStroke.getKeyStroke('S',InputEvent.CTRL_MASK)); lingcunwei.setAccelerator(KeyStroke.getKeyStroke('A',InputEvent.CTRL_MASK)); @@ -215,14 +194,14 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window zhantie.setAccelerator(KeyStroke.getKeyStroke('V',InputEvent.CTRL_MASK)); shanchu.setAccelerator(KeyStroke.getKeyStroke('L',InputEvent.CTRL_MASK)); chazhao.setAccelerator(KeyStroke.getKeyStroke('F',InputEvent.CTRL_MASK)); - tihuan.setAccelerator(KeyStroke.getKeyStroke('R',InputEvent.CTRL_MASK)); - zhuandao.setAccelerator(KeyStroke.getKeyStroke('G',InputEvent.CTRL_MASK)); + tihuan.setAccelerator(KeyStroke.getKeyStroke('R',InputEvent.CTRL_MASK)); quanxuan.setAccelerator(KeyStroke.getKeyStroke('K',InputEvent.CTRL_MASK)); - shijian.setAccelerator(KeyStroke.getKeyStroke('D',InputEvent.CTRL_MASK)); - - - zidonhuanhang.setAccelerator(KeyStroke.getKeyStroke('W',InputEvent.CTRL_MASK)); + shijian.setAccelerator(KeyStroke.getKeyStroke('D',InputEvent.CTRL_MASK)); ziti.setAccelerator(KeyStroke.getKeyStroke('Q',InputEvent.CTRL_MASK)); + + JMenuItem zidonhuanhang = new JMenuItem("\u81EA\u52A8\u6362\u884C(W)");//自动换行 + zidonhuanhang.setSelected(true); + geshi.add(zidonhuanhang); xinjian.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ @@ -335,14 +314,98 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window } });//全选功能 + shijian.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + textArea.append(new Date().toString()); + } + });//时间功能 + + zidonhuanhang.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + int flag=1; + flag=-flag; + + if(flag==1) + textArea.setLineWrap(false); + else + textArea.setLineWrap(true); + } + });//自动换行 + ziti.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { - new P4(); - }}); + new P4(f,textArea); + }});//字体功能 + + guanyujishiben.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e) + { + JOptionPane.showMessageDialog(new JFrame(),"熬夜记事本","About",JOptionPane.INFORMATION_MESSAGE); + } + });//关于记事本 + + + + chakanbangzhu.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e) + { + JOptionPane.showMessageDialog(new JFrame(),"1.自动换行功能无需勾选,自动实现。\n2.可使用快捷键提高速度。\n3.文本框可鼠标拉动改变大小。\n4.有问题可参考百度。","About",JOptionPane.INFORMATION_MESSAGE); + } + });//查看帮助 } - + /* JPopupMenu popupmenuR=new JPopupMenu(); + textArea.add(popupmenuR); + textArea.addMouseListener( + new MouseAdapter() { + public void mouseReleased(MouseEvent e) { + if(e.getButton() == MouseEvent.BUTTON3_MASK) + popupmenuR.show(fuzhi, e.getX()-30, e.getY()); + } + + public void mouseClicked(MouseEvent e) { + if(e.getButton() == MouseEvent.BUTTON1) + popupmenuR.setVisible(false); + } } ); */ + + + /* JPopupMenu popupmenuR=new JPopupMenu(); + textArea.add(popupmenuR); + textArea.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + if(e.getModifiers()==MouseEvent.BUTTON3_MASK){ + popupmenuR.show(textArea, e.getX(), e.getY()); + } + } + });} + JMenuItem Undo1=new JMenuItem("撤消(U)"); + JMenuItem Cut1=new JMenuItem("剪切(T)"); + JMenuItem Copy1=new JMenuItem("复制(C)"); + JMenuItem Paste1=new JMenuItem("粘贴(P)"); + JMenuItem Delete1=new JMenuItem("删除(L)"); + JMenuItem SelectAll=new JMenuItem("全选(A)"); + + Undo1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); + Cut1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); + Copy1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); + Paste1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); + Delete1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); + SelectAll.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); + + popupmenuR.add(chexiao); + popupmenuR.addSeparator(); + popupmenuR.add(jianqie); + popupmenuR.addSeparator(); + popupmenuR.add(fuzhi); + popupmenuR.addSeparator(); + popupmenuR.add(zhantie); + popupmenuR.addSeparator(); + popupmenuR.add(shanchu); + popupmenuR.addSeparator(); + popupmenuR.add(quanxuan); + }//右键 + */ - + diff --git a/src/notepad/P4.java b/src/notepad/P4.java index 3dc2f54..b57ce1f 100644 --- a/src/notepad/P4.java +++ b/src/notepad/P4.java @@ -3,231 +3,151 @@ package notepad; import java.awt.*; - import java.awt.event.*; - import javax.swing.*; -import javax.swing.event.*; - -public class P4 extends JDialog implements ActionListener,ListSelectionListener{ -String ss,ss3; - -JTextArea ta1; - -String fontName="宋体" ; - -int fontStyle=Font.PLAIN ; - -int fontSize=16; - -Font font=new Font(fontName,fontStyle,fontSize); - -GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); - -String fontName1[]=ge.getAvailableFontFamilyNames(); - -JList list1=new JList(fontName1); - -String name[]={"常规","粗体","倾斜","粗偏斜体"}; - -JList list2=new JList(name); - -String size[]={"12","14","16","18","20","22"}; - -JList list3=new JList(size); - -JButton button1=new JButton("确定"); - -JButton button2=new JButton("取消"); - -JPanel p =new JPanel(); - -JPanel p1=new JPanel(); - -JPanel p2=new JPanel(); - -JPanel p3=new JPanel(); - - - -JLabel l1=new JLabel("字体(F):"); - -JLabel l2=new JLabel("字形(Y):"); - -JLabel l3=new JLabel("大小(S):"); - -JTextField f1=new JTextField("宋体",10); - -JTextField f2=new JTextField("常规",10); - -JTextField f3=new JTextField("12",10); - - P4(){ -init(); - -setBounds(560,300,560,400); - -setResizable(false); - -} - - - -public void setTa(JTextArea ta2){ -ta1=ta2; - -} - -void init(){ -p.setLayout(null); - -p1.setLayout(new BorderLayout()); - -p2.setLayout(new BorderLayout()); - -p3.setLayout(new BorderLayout()); - -l1.setBounds(50,30,80,30); - -l2.setBounds(240,30,80,30); - -l3.setBounds(400,30,80,30); - -f1.setBounds(50,60,110,25); - -f2.setBounds(240,60,80,25); - -f3.setBounds(400,60,80,25); - -p1.setBounds(50,90,110,200); - -p2.setBounds(240,90,80,200); - -p3.setBounds(400,90,80,200); - -button1.setBounds(360, 300, 70, 40); - -button2.setBounds(460, 300, 70, 40); - -p.add(l1); - -p.add(l2); - -p.add(l3); - -p.add(f1); - -p.add(f2); - -p.add(f3); - -p1.add( new JScrollPane(list1)); - -p2.add(new JScrollPane(list2)); - - p3.add(new JScrollPane(list3)); - -p.add(p1,BorderLayout.CENTER); - - p.add(p2,BorderLayout.CENTER); - - p.add(p3,BorderLayout.CENTER); - -p.add(button1); - -p.add(button2); - - - -add(p); - -list1.addListSelectionListener(this); - -list2.addListSelectionListener(this); - -list3.addListSelectionListener(this); - -button1.addActionListener(this); - -button2.addActionListener(this); - -} - -public void actionPerformed(ActionEvent e){ -if(e.getSource()==button1){ -font=new Font(fontName,fontStyle,fontSize); - -ta1.setFont(font); - -} - -if(e.getSource()==button2){ - this.dispose(); - - f1.setText("宋体"); - - f2.setText("常规"); - - f3.setText("12");//再次修改字体时不起作用 - -} - -} - +public class P4 extends JDialog{ + public P4(JFrame f,JTextArea textArea) { + + //获取系统字体库 + GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); + String[] fontNames = e.getAvailableFontFamilyNames(); + String[] fontStyle=new String[]{"常规","粗体","斜体","粗偏斜体"}; + Integer[] fontsize=new Integer[] {8,9,10,11,12,13,14,16,18,20,22,24,26,28,30,32,36,48,72}; + + JDialog ziti = new JDialog(f, "字体", true); + ziti.setSize(453, 325); + ziti.setLocationRelativeTo(null); + ziti.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); + + + JPanel panel=new JPanel(); + panel.setLayout(null); + + JButton yes=new JButton("确定"); + JButton no=new JButton("取消"); + JButton setfontcolor=new JButton("字体颜色"); + JLabel fonts=new JLabel("字体(F):"); + JLabel style=new JLabel("字形(Y):"); + JLabel size=new JLabel("大小(S):"); + JLabel cg=new JLabel("AaBbCc"); + + // 创建下拉列表框 + final JComboBox comboBox = new JComboBox(fontNames); + final JComboBox comboBox2=new JComboBox(fontStyle); + final JComboBox comboBox3=new JComboBox(fontsize); + + + + // 添加条目选中状态改变的监听器 + comboBox.addItemListener(new ItemListener() { + @Override + public void itemStateChanged(ItemEvent e) { + // 只处理选中的状态 + if (e.getStateChange() == ItemEvent.SELECTED) { + cg.setFont(new Font((String) comboBox.getSelectedItem(), + cg.getFont().getStyle(), cg.getFont().getSize())); + } + } + }); + comboBox2.addItemListener(new ItemListener() { + public void itemStateChanged(ItemEvent e) { + if(e.getStateChange()==ItemEvent.SELECTED) { + if(comboBox2.getSelectedItem().equals("常规")) { + cg.setFont(new Font(cg.getFont().getName(), Font.PLAIN, + cg.getFont().getSize())); + } + else if(comboBox2.getSelectedItem().equals("粗体")){ + cg.setFont(new Font(cg.getFont().getName(), Font.BOLD, + cg.getFont().getSize())); + } + else if(comboBox2.getSelectedItem().equals("斜体")){ + cg.setFont(new Font(cg.getFont().getName(), Font.ITALIC, + cg.getFont().getSize())); + } + else if(comboBox2.getSelectedItem().equals("粗偏斜体")) { + cg.setFont(new Font(cg.getFont().getName(), Font.BOLD+ Font.ITALIC, + cg.getFont().getSize())); + } + } + } + }); + comboBox3.addItemListener(new ItemListener() { + public void itemStateChanged(ItemEvent e) { + if(e.getStateChange()==ItemEvent.SELECTED) { + cg.setFont(new Font(cg.getFont().getName(), cg.getFont().getStyle(), + (int) comboBox3.getSelectedItem())); + + } + } + }); + + setfontcolor.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Color color = JColorChooser.showDialog(f, "选取颜色", null); + // 如果用户取消或关闭窗口, 则返回的 color 为 null + if (color == null) { + return; + } + textArea.setForeground(color); + } + }); + + yes.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + textArea.setFont(cg.getFont()); + ziti.setVisible(false); + } + }); + + no.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + ziti.setVisible(false); + } + }); + + //设置元件位置和宽高 + fonts.setBounds(20,20,45,18); + style.setBounds(190,20,45,18); + size.setBounds(313,20,45,18); + comboBox.setBounds(20,42,170,22); + comboBox2.setBounds(190,42,119,22); + comboBox3.setBounds(309, 42, 119, 22); + cg.setBounds(30,89,378,137); + setfontcolor.setBounds(20,249,102,30); + yes.setBounds(300,249,62,30); + no.setBounds(366,249,62,30); + + fonts.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16)); + style.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16)); + size.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16)); + cg.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 12)); + setfontcolor.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 12)); + yes.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 13)); + no.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 13)); + comboBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16)); + comboBox2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16)); + comboBox3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16)); - - - -public void valueChanged (ListSelectionEvent e){ - -if(e.getSource()==list1){ -int m=list1.getSelectedIndex(); - -fontName=fontName1[m]; - -f1.setText(fontName); - -} - -if(e.getSource()==list2){ -int m=list2.getSelectedIndex(); - -ss=name[m]; - -if(ss.equals("粗体")) - -fontStyle=Font.BOLD; - -if(ss.equals("倾斜")) - -fontStyle=Font.ITALIC ; - -if(ss.equals("常规")) - -fontStyle=Font.PLAIN; - -if(ss.equals("粗偏斜体")) - -fontStyle=Font.BOLD+Font.ITALIC; - - f2.setText(ss); - -} - -if(e.getSource()==list3){ - - -int m=list3.getSelectedIndex(); - -ss3=size[m]; - -fontSize=Integer.parseInt(ss3); - -f3.setText(ss3); - -} - -} - + // 设置字体下拉框为文本框的字体样式 + comboBox.setSelectedItem(textArea.getFont().getFontName()); + comboBox3.setSelectedItem(textArea.getFont().getSize()); + comboBox2.setSelectedIndex(textArea.getFont().getStyle()); + // 添加到内容面板 + panel.add(fonts); + panel.add(style); + panel.add(size); + panel.add(comboBox); + panel.add(comboBox2); + panel.add(comboBox3); + panel.add(setfontcolor); + panel.add(yes); + panel.add(no); + panel.add(cg); + + ziti.setResizable(false); + ziti.setContentPane(panel); + ziti.setVisible(true); + } } \ No newline at end of file -- Gitee From 25ce13a2d8cfeab8f4c610b9aa7f3013280ba983 Mon Sep 17 00:00:00 2001 From: hjh Date: Fri, 11 Jun 2021 15:07:14 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E6=A0=8F=E5=8A=9F=E8=83=BD=EF=BC=8C=E9=BC=A0=E6=A0=87=E5=8F=B3?= =?UTF-8?q?=E9=94=AE=E8=8F=9C=E5=8D=95=E3=80=82=E8=AE=B0=E4=BA=8B=E6=9C=AC?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nullnull.txt | 1 - src/notepad/P1.java | 347 ++++++++++++++++++-------------------------- src/notepad/P4.java | 2 +- src/notepad/P5.java | 0 4 files changed, 144 insertions(+), 206 deletions(-) create mode 100644 src/notepad/P5.java diff --git a/nullnull.txt b/nullnull.txt index 85c8dfa..e69de29 100644 --- a/nullnull.txt +++ b/nullnull.txt @@ -1 +0,0 @@ -哈哈哈我 diff --git a/src/notepad/P1.java b/src/notepad/P1.java index ad2ed11..c62c2ae 100644 --- a/src/notepad/P1.java +++ b/src/notepad/P1.java @@ -4,8 +4,11 @@ import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.border.EmptyBorder; +import javax.swing.event.CaretEvent; +import javax.swing.event.CaretListener; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; +import javax.swing.text.BadLocationException; import javax.swing.text.TextAction; import javax.swing.undo.UndoManager; @@ -30,9 +33,9 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window private JPanel contentPane; JFrame f=new JFrame(); JFrame jframe=new JFrame(); - /** - * Launch the application. - */ + + JLabel Label=new JLabel("当前字数:"+0+" 行"+1+" 列"+1); //创建状态栏 + public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { @@ -45,10 +48,6 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window } }); } - - /** - * Create the frame. - */ public P1() { @@ -59,9 +58,7 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window JMenuBar menuBar = new JMenuBar(); - setJMenuBar(menuBar); - - + setJMenuBar(menuBar); JMenu wenjian = new JMenu("\u6587\u4EF6(F)");//文件 menuBar.add(wenjian); @@ -135,6 +132,9 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window geshi.add(ziti); ziti.addActionListener(this); + JMenu chakan = new JMenu("\u67E5\u770B(V)"); + menuBar.add(chakan); + JMenu bangzhu = new JMenu("\u5E2E\u52A9(H)");//帮助 menuBar.add(bangzhu); bangzhu.addActionListener(this); @@ -145,11 +145,8 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window JMenuItem guanyujishiben = new JMenuItem("\u5173\u4E8E\u8BB0\u4E8B\u672C(A)");//关于记事本 bangzhu.add(guanyujishiben); - guanyujishiben.addActionListener(this); - - - - + guanyujishiben.addActionListener(this); + contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); @@ -159,35 +156,13 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window contentPane.add(textArea, BorderLayout.CENTER); - boolean wasSaved = true; - - /*JTextArea textArea_1 = new JTextArea(); - contentPane.add(textArea_1, BorderLayout.WEST); - (textArea_1.getDocument()).addDocumentListener(this); - getContentPane().add(new JScrollPane(textArea_1)); - - JTextArea textArea_2 = new JTextArea(); - contentPane.add(textArea_2, BorderLayout.CENTER); - (textArea_2.getDocument()).addDocumentListener(this); - getContentPane().add(new JScrollPane(textArea_2)); - - JTextArea textArea_3 = new JTextArea(); - contentPane.add(textArea_3, BorderLayout.SOUTH); - (textArea_3.getDocument()).addDocumentListener(this); - getContentPane().add(new JScrollPane(textArea_3)); - - JTextArea textArea_4 = new JTextArea(); - contentPane.add(textArea_4, BorderLayout.EAST); - (textArea_4.getDocument()).addDocumentListener(this); - getContentPane().add(new JScrollPane(textArea_4));*/ - + boolean wasSaved = true; xinjian.setAccelerator(KeyStroke.getKeyStroke('N',InputEvent.CTRL_MASK)); dakai.setAccelerator(KeyStroke.getKeyStroke('O',InputEvent.CTRL_MASK)); baocun.setAccelerator(KeyStroke.getKeyStroke('S',InputEvent.CTRL_MASK)); lingcunwei.setAccelerator(KeyStroke.getKeyStroke('A',InputEvent.CTRL_MASK)); - tuichu.setAccelerator(KeyStroke.getKeyStroke('X',InputEvent.CTRL_MASK)); - + tuichu.setAccelerator(KeyStroke.getKeyStroke('X',InputEvent.CTRL_MASK)); chexiao.setAccelerator(KeyStroke.getKeyStroke('U',InputEvent.CTRL_MASK)); jianqie.setAccelerator(KeyStroke.getKeyStroke('T',InputEvent.CTRL_MASK)); fuzhi.setAccelerator(KeyStroke.getKeyStroke('C',InputEvent.CTRL_MASK)); @@ -197,6 +172,24 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window tihuan.setAccelerator(KeyStroke.getKeyStroke('R',InputEvent.CTRL_MASK)); quanxuan.setAccelerator(KeyStroke.getKeyStroke('K',InputEvent.CTRL_MASK)); shijian.setAccelerator(KeyStroke.getKeyStroke('D',InputEvent.CTRL_MASK)); + + JMenuItem chexiao1 = new JMenuItem("\u64A4\u9500"); + bianji.add(chexiao1); + + JMenuItem jianqie1 = new JMenuItem("\u526A\u5207"); + bianji.add(jianqie1); + + JMenuItem fuzhi1 = new JMenuItem("\u590D\u5236"); + bianji.add(fuzhi1); + + JMenuItem zhantie1 = new JMenuItem("\u7C98\u8D34"); + bianji.add(zhantie1); + + JMenuItem shanchu1 = new JMenuItem("\u5220\u9664"); + bianji.add(shanchu1); + + JMenuItem quanxuan1 = new JMenuItem("\u5168\u9009"); + bianji.add(quanxuan1); ziti.setAccelerator(KeyStroke.getKeyStroke('Q',InputEvent.CTRL_MASK)); JMenuItem zidonhuanhang = new JMenuItem("\u81EA\u52A8\u6362\u884C(W)");//自动换行 @@ -213,8 +206,8 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window FileDialog open; FileDialog save; open = new FileDialog(this,"打开(O)",FileDialog.LOAD); - save = new FileDialog(this,"另存为(A)",FileDialog.SAVE); - + save = new FileDialog(this,"保存(S)",FileDialog.SAVE); + dakai.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { open.setVisible(true); @@ -243,7 +236,7 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window out.close(); } catch (IOException ex) { System.out.println(ex); - }}});//保存功能 + }}});//保存功能 lingcunwei.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -259,6 +252,45 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window } });//另存为功能 + JCheckBoxMenuItem view1=new JCheckBoxMenuItem("状态栏(S)");//状态栏 + textArea.addCaretListener(new CaretListener() { + public void caretUpdate(CaretEvent e) { + int position=textArea.getCaretPosition(); + int currentNumber=0; + int offset = e.getDot(); + int start = 0; + try { + start = textArea.getLineOfOffset(offset); + } + catch (BadLocationException e2) { + e2.printStackTrace(); + } + int end = 0; + try { + end = e.getDot() - textArea.getLineStartOffset(start); + } + catch (BadLocationException e1) { + e1.printStackTrace(); + } + String content = textArea.getText().replaceAll("\\s+", ""); + currentNumber=content.length(); + Label.setText("当前字数:"+String.valueOf(currentNumber)+" 行"+(start+1)+" 列"+(end+1)); + } + });//统计字数和行号 + this.add(Label,BorderLayout.SOUTH); + view1.setState(true); + chakan.add(view1); + Label.setVisible(true); + Label.setHorizontalAlignment(SwingConstants.RIGHT); + view1.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if(view1.getState()) + Label.setVisible(true); + else + Label.setVisible(false); + } + });//状态栏功能 + tuichu.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.exit(0); @@ -340,35 +372,57 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window guanyujishiben.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { - JOptionPane.showMessageDialog(new JFrame(),"熬夜记事本","About",JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(new JFrame(),"熬夜记事本","关于记事本",JOptionPane.INFORMATION_MESSAGE); } - });//关于记事本 - - + });//关于记事本 chakanbangzhu.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { - JOptionPane.showMessageDialog(new JFrame(),"1.自动换行功能无需勾选,自动实现。\n2.可使用快捷键提高速度。\n3.文本框可鼠标拉动改变大小。\n4.有问题可参考百度。","About",JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(new JFrame(),"1.自动换行功能需点击,才能实现。\n2.可使用快捷键提高速度。\n3.文本框可鼠标拉动改变大小。\n4.有问题可参考百度。","查看帮助",JOptionPane.INFORMATION_MESSAGE); } - });//查看帮助 - } - /* JPopupMenu popupmenuR=new JPopupMenu(); - textArea.add(popupmenuR); - textArea.addMouseListener( - new MouseAdapter() { - public void mouseReleased(MouseEvent e) { - if(e.getButton() == MouseEvent.BUTTON3_MASK) - popupmenuR.show(fuzhi, e.getX()-30, e.getY()); - } - - public void mouseClicked(MouseEvent e) { - if(e.getButton() == MouseEvent.BUTTON1) - popupmenuR.setVisible(false); - } } ); */ - + });//查看帮助 + - /* JPopupMenu popupmenuR=new JPopupMenu(); + UndoManager undo1 = new UndoManager(); + textArea.getDocument().addUndoableEditListener(undo); + chexiao1.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e) { + if(undo.canUndo()) + undo.undo(); + } + });//右键撤销功能 + + jianqie1.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + textArea.cut(); + } + });//右键剪切功能 + + fuzhi1.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e) { + textArea.copy(); + }});//右键复制功能 + + zhantie1.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + textArea.paste(); + } + }); //右键粘帖功能 + + shanchu1.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd()); + } + });//右键删除功能 + + quanxuan1.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e) { + textArea.selectAll(); + } + });//右键全选功能 + + JPopupMenu popupmenuR=new JPopupMenu(); textArea.add(popupmenuR); textArea.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { @@ -376,38 +430,27 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window popupmenuR.show(textArea, e.getX(), e.getY()); } } - });} + }); JMenuItem Undo1=new JMenuItem("撤消(U)"); JMenuItem Cut1=new JMenuItem("剪切(T)"); JMenuItem Copy1=new JMenuItem("复制(C)"); JMenuItem Paste1=new JMenuItem("粘贴(P)"); JMenuItem Delete1=new JMenuItem("删除(L)"); JMenuItem SelectAll=new JMenuItem("全选(A)"); - - Undo1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); - Cut1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); - Copy1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); - Paste1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); - Delete1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); - SelectAll.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); - - popupmenuR.add(chexiao); + + popupmenuR.add(chexiao1); popupmenuR.addSeparator(); - popupmenuR.add(jianqie); + popupmenuR.add(jianqie1); popupmenuR.addSeparator(); - popupmenuR.add(fuzhi); + popupmenuR.add(fuzhi1); popupmenuR.addSeparator(); - popupmenuR.add(zhantie); + popupmenuR.add(zhantie1); popupmenuR.addSeparator(); - popupmenuR.add(shanchu); + popupmenuR.add(shanchu1); popupmenuR.addSeparator(); - popupmenuR.add(quanxuan); - }//右键 - */ - - - - + popupmenuR.add(quanxuan1); + } + @@ -415,46 +458,24 @@ public class P1 extends JFrame implements DocumentListener,ActionListener,Window + + + + + + + + + + + + protected void find() { // TODO 自动生成的方法存根 } -/* JPopupMenu popupmenuR=new JPopupMenu(); - textArea.add(popupmenuR); - textArea.addMouseListener(new MouseAdapter() { - public void mouseClicked(MouseEvent e) { - if(e.getModifiers()==MouseEvent.BUTTON3_MASK){ - popupmenuR.show(textArea, e.getX(), e.getY()); - } - } - }); - JMenuItem Undo1=new JMenuItem("撤消(U)"); - JMenuItem Cut1=new JMenuItem("剪切(T)"); - JMenuItem Copy1=new JMenuItem("复制(C)"); - JMenuItem Paste1=new JMenuItem("粘贴(P)"); - JMenuItem Delete1=new JMenuItem("删除(L)"); - JMenuItem SelectAll=new JMenuItem("全选(A)"); - // - Undo1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); - Cut1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); - Copy1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); - Paste1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); - Delete1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); - SelectAll.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15)); - // - popupmenuR.add(Undo1); - popupmenuR.addSeparator(); - popupmenuR.add(Copy1); - popupmenuR.addSeparator(); - popupmenuR.add(Cut1); - popupmenuR.addSeparator(); - popupmenuR.add(Paste1); - popupmenuR.addSeparator(); - popupmenuR.add(Delete1); - popupmenuR.addSeparator(); - popupmenuR.add(SelectAll); - */ + private void save() { // TODO 自动生成的方法存根 @@ -528,91 +549,9 @@ public void removeUpdate(DocumentEvent e) { public void changedUpdate(DocumentEvent e) { // TODO 自动生成的方法存根 -}} +} +} -/*public void save(){ - - JFileChooser fileDialog=new JFileChooser(); - BufferedReader in; - FileReader fileReader; - BufferedWriter out; - FileWriter fileWriter; - int flag=0; - int flag1=0; - File dir; - String name; - init(); - //setDefaultCloseOperation(int operation); - setVisible(true); - - int state=fileDialog.showSaveDialog(this); - - if(state==JFileChooser.APPROVE_OPTION){ - try{ - dir=fileDialog.getCurrentDirectory(); - - name=fileDialog.getSelectedFile().getName(); - - File file=new File(dir,name); - - fileWriter=new FileWriter(file); - - out=new BufferedWriter(fileWriter); - - // out.write(textArea.getText()); - - out.close(); - - fileWriter.close(); - - } - - catch(IOException exp){} - - - - } - -/*public void actionPerformed(ActionEvent e){ -if(e.getSource()==item1){ - int n=JOptionPane.showConfirmDialog(this,"文件还未保存,确认是否保存","确认对话框",JOptionPane.YES_NO_OPTION); - if(n==JOptionPane.YES_OPTION) - save(); - else if(n==JOptionPane.NO_OPTION) - - wenjian.setText(null); - -if(e.getSource()==baocun)//保存 - { - -int flag; -int flag1; -if(flag==1&&flag1==0){ - save();flag=0;} - -else if(flag==1&&flag1==1){ -try{ -String dir; -String name; -File file=new File(dir,name); - - FileWriter fileWriter = new FileWriter(file); - - BufferedWriter out = new BufferedWriter(fileWriter); - - AbstractButton area; - out.write(area.getText()); } - - catch(IOException exp){} - -flag=0; - - } - -flag1=1; - -}}*/ - diff --git a/src/notepad/P4.java b/src/notepad/P4.java index b57ce1f..bd73497 100644 --- a/src/notepad/P4.java +++ b/src/notepad/P4.java @@ -15,7 +15,7 @@ public class P4 extends JDialog{ String[] fontStyle=new String[]{"常规","粗体","斜体","粗偏斜体"}; Integer[] fontsize=new Integer[] {8,9,10,11,12,13,14,16,18,20,22,24,26,28,30,32,36,48,72}; - JDialog ziti = new JDialog(f, "字体", true); + JDialog ziti = new JDialog(f, "字体(F)", true); ziti.setSize(453, 325); ziti.setLocationRelativeTo(null); ziti.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); diff --git a/src/notepad/P5.java b/src/notepad/P5.java new file mode 100644 index 0000000..e69de29 -- Gitee