From 6a6652eb408e42812917ac689566362964f29339 Mon Sep 17 00:00:00 2001 From: Yin01 Date: Thu, 3 Jun 2021 20:49:04 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E6=A1=86=E6=9E=B61.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/MyFrame.java | 141 ++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 src/java2020spring/MyFrame.java diff --git a/src/java2020spring/MyFrame.java b/src/java2020spring/MyFrame.java new file mode 100644 index 0000000..a9bba35 --- /dev/null +++ b/src/java2020spring/MyFrame.java @@ -0,0 +1,141 @@ +package java2020spring; + +import javax.swing.*; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.undo.UndoManager; +import java.awt.*; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.Transferable; +import java.awt.event.*; +import java.io.*; +import java.text.SimpleDateFormat; +import java.util.Date; + /* + * *Created by HaiyinChen + */ +public class MyFrame extends JFrame{ +private static final int DEFAULT_FONTSIZE = 20; //默认字体大小 +private JTextArea textArea = null; //多行文本框 +private JScrollPane pane = null; //带滚动条的面板 +private JMenuBar menuBar = null; //菜单条 +private JMenu file = null; //文件菜单 +private JMenu edit = null; //编辑菜单 +private JMenu format = null; //格式菜单 +private JMenu check = null; //查看菜单 +private JMenu help = null; //帮助菜单 +private JPopupMenu popupMenu = null; //右键弹出式菜单 +/*-------------------------文件菜单项--------------------------*/ +private JMenuItem fileNew = null; //新建 +private JMenuItem fileOpen = null; //打开 +private JMenuItem fileSave = null; //保存 +private JMenuItem fileElseSave = null; //另存为 +private JMenuItem fileExit = null; //退出 +/*-------------------------文件菜单项---------------------------*/ +/*-------------------------编辑菜单项--------------------------*/ +private JMenuItem editUndo = null; //撤销 +private JMenuItem editCut = null; //剪切 +private JMenuItem editCopy = null; //复制 +private JMenuItem editPaste = null; //粘贴 +private JMenuItem editClear = null; //删除,清空当前内容 +/*------------------------编辑菜单项-----------------------------*/ +/*------------------------格式菜单项-----------------------------*/ +private JMenuItem formatWrap = null; //自动换行 +private JMenuItem formatChooseFont = null; //字体选择 +private JMenuItem formatChoosFontColor = null; //字体颜色选择 +/*------------------------格式菜单项--------------------------------*/ +/*------------------------查看菜单项----------------------------------*/ +private JMenuItem checkStatus = null; //状态栏 +/*------------------------查看菜单项------------------------------*/ +/*------------------------帮助菜单项----------------------------*/ + private JMenuItem helpLook = null; //查看帮助 + private JMenuItem helpAbout = null; //关于记事本 + /*-----------------------帮助菜单项---------------------------*/ + /*-------------------------弹出菜单项-------------------------*/ + private JMenuItem popCut = null; //剪切 + private JMenuItem popCopy = null; //复制 + private JMenuItem popPaste = null; //粘贴 + /*-------------------------弹出菜单项-----------------------------*/ + + public MyFrame(String s,int x,int y,int w,int h) { //MyFrame的构造方法 + setLocation(x,y); + setTitle(s); + setSize(w,h); + init(); + setVisible(true); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + //初始化相关组件的方法 + + } + //初始化相关组件 + private void init() { + /*--------------------------多行文本框--------------------------------*/ + textArea = new JTextArea(); //创建一个多行文本框 + textArea.setFont(new Font("Courier",Font.PLAIN,16)); // 文本框字体风格,int风格,字体号 + textArea.setCaretColor(Color.BLUE); // 光标颜色 + textArea.setSelectedTextColor(Color.WHITE); //选中字体的颜色 + textArea.setSelectionColor(Color.PINK); //选中背景颜色 + textArea.setLineWrap(true); //自动换行 + textArea.setTabSize(4); //设置选项卡要扩大到的字符数 + textArea.getDocument(); + /*--------------------------多行文本框----------------------------*/ + pane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, + JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); //创建一个滚动条面板 + pane.setOpaque(false); //设置面板透明 + pane.getViewport().setOpaque(false); + Container cpan = this.getContentPane(); //获取当前框架的内容面板 + cpan.add(pane,BorderLayout.CENTER); // 将多行文本框添加到面板中央 + menuBar = new JMenuBar(); //创建菜单条 + /*-------------------------------文件菜单------------------------------*/ + file = new JMenu("文件(F)"); //创建文件菜单 + file.setMnemonic('F'); //助记符 + fileNew = new JMenuItem("新建(N)"); //创建新建菜单项 + fileNew.setAccelerator(KeyStroke.getKeyStroke("ctrl N")); // 快捷键 + fileNew.setMnemonic('N'); //助记符 + fileOpen = new JMenuItem("打开(O)"); //创建打开菜单项 + fileOpen.setAccelerator(KeyStroke.getKeyStroke("ctrl O")); + fileOpen.setMnemonic('O'); + fileSave = new JMenuItem("保存(S)"); //创建保存菜单项 + fileSave.setAccelerator(KeyStroke.getKeyStroke("ctrl S")); + fileSave.setMnemonic('S'); + fileElseSave = new JMenuItem("另存为(A)"); //创建另存为菜单项 + fileElseSave.setAccelerator(KeyStroke.getKeyStroke("ctrl A")); + fileElseSave.setMnemonic('A'); + fileExit = new JMenuItem("退出(X)"); //创建退出菜单项 + fileExit.setMnemonic('X'); +/*-----------------------------把菜单项加到文件菜单中--------------------------*/ + file.add(fileNew); + file.add(fileOpen); + file.add(fileSave); + file.add(fileElseSave); + file.addSeparator(); //添加分隔符 + file.add(fileExit); +/*----------------------------把菜单项加到文件菜单中------------------------*/ + + +/*-----------------------------把菜单加到菜单条-------------------------------*/ + menuBar.add(file); + + +/*----------------------------把菜单加到菜单条-------------------------------*/ +/*----------------------------把菜单条放在布局的上面---------------------------*/ + add(menuBar,BorderLayout.NORTH); + + +/*----------------------------把菜单条放在布局的上面---------------------------*/ +/*----------------------------把面板放在布局的中间----------------------------*/ + add(pane,BorderLayout.CENTER); + + +/*----------------------------把面板放在布局的中间----------------------------*/ + } + + + + + + + + + +} -- Gitee From 194c93f47f069fcf3ec4cdb4ad54f518a27841b4 Mon Sep 17 00:00:00 2001 From: Yin01 Date: Fri, 4 Jun 2021 20:59:58 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E6=A1=86=E6=9E=B62.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/MyFrame.java | 50 ++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/src/java2020spring/MyFrame.java b/src/java2020spring/MyFrame.java index a9bba35..cb1a6f1 100644 --- a/src/java2020spring/MyFrame.java +++ b/src/java2020spring/MyFrame.java @@ -91,18 +91,32 @@ private JMenuItem checkStatus = null; //状态 file.setMnemonic('F'); //助记符 fileNew = new JMenuItem("新建(N)"); //创建新建菜单项 fileNew.setAccelerator(KeyStroke.getKeyStroke("ctrl N")); // 快捷键 - fileNew.setMnemonic('N'); //助记符 fileOpen = new JMenuItem("打开(O)"); //创建打开菜单项 fileOpen.setAccelerator(KeyStroke.getKeyStroke("ctrl O")); - fileOpen.setMnemonic('O'); fileSave = new JMenuItem("保存(S)"); //创建保存菜单项 fileSave.setAccelerator(KeyStroke.getKeyStroke("ctrl S")); - fileSave.setMnemonic('S'); fileElseSave = new JMenuItem("另存为(A)"); //创建另存为菜单项 fileElseSave.setAccelerator(KeyStroke.getKeyStroke("ctrl A")); - fileElseSave.setMnemonic('A'); fileExit = new JMenuItem("退出(X)"); //创建退出菜单项 - fileExit.setMnemonic('X'); +/*-------------------------------文件菜单-------------------------------------*/ +/*-------------------------------编辑菜单------------------------------------*/ + edit = new JMenu("编辑菜单(E)"); //创建编辑菜单 + edit.setMnemonic('E'); //助记符 + editUndo = new JMenuItem("撤销(Z)"); //创建撤销菜单项 + editUndo.setAccelerator(KeyStroke.getKeyStroke("ctrl Z")); //快捷键 + editCut = new JMenuItem("剪切(X)"); //创建剪切菜单项 + editCut.setAccelerator(KeyStroke.getKeyStroke("ctrl X")); + editCopy = new JMenuItem("复制(C)"); //创建复制菜单项 + editCopy.setAccelerator(KeyStroke.getKeyStroke("ctrl C")); //快捷键 + editPaste = new JMenuItem("粘贴(V)"); //创建粘贴菜单项 + editPaste.setAccelerator(KeyStroke.getKeyStroke("ctrl V")); //快捷键 + editClear = new JMenuItem("清除"); //创建清除菜单项 +/*-------------------------------编辑菜单------------------------------------*/ + + + + + /*-----------------------------把菜单项加到文件菜单中--------------------------*/ file.add(fileNew); file.add(fileOpen); @@ -111,23 +125,25 @@ private JMenuItem checkStatus = null; //状态 file.addSeparator(); //添加分隔符 file.add(fileExit); /*----------------------------把菜单项加到文件菜单中------------------------*/ - - +/*----------------------------把菜单项加到编辑菜单中-------------------------*/ + edit.add(editUndo); + edit.addSeparator(); //添加分隔符 + edit.add(editCut); + edit.add(editCopy); + edit.add(editPaste); + edit.addSeparator(); //添加分隔符 + edit.add(editClear); +/*----------------------------把菜单项加到编辑菜单中-------------------------*/ /*-----------------------------把菜单加到菜单条-------------------------------*/ menuBar.add(file); + menuBar.add(edit); /*----------------------------把菜单加到菜单条-------------------------------*/ -/*----------------------------把菜单条放在布局的上面---------------------------*/ - add(menuBar,BorderLayout.NORTH); - - -/*----------------------------把菜单条放在布局的上面---------------------------*/ -/*----------------------------把面板放在布局的中间----------------------------*/ - add(pane,BorderLayout.CENTER); - - -/*----------------------------把面板放在布局的中间----------------------------*/ + + add(menuBar,BorderLayout.NORTH); //把菜单条放在布局的上面 + add(pane,BorderLayout.CENTER); //把面板放在布局的中间 + } -- Gitee From 318ea8835c7bef2030ffd92758701e7ffec2d977 Mon Sep 17 00:00:00 2001 From: Yin01 Date: Sat, 5 Jun 2021 23:26:16 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E6=A1=86=E6=9E=B63.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/MyFrame.java | 98 +++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 35 deletions(-) diff --git a/src/java2020spring/MyFrame.java b/src/java2020spring/MyFrame.java index cb1a6f1..6747ae5 100644 --- a/src/java2020spring/MyFrame.java +++ b/src/java2020spring/MyFrame.java @@ -42,20 +42,20 @@ private JMenuItem editClear = null; //删 /*------------------------格式菜单项-----------------------------*/ private JMenuItem formatWrap = null; //自动换行 private JMenuItem formatChooseFont = null; //字体选择 -private JMenuItem formatChoosFontColor = null; //字体颜色选择 +private JMenuItem formatChooseFontColor = null; //字体颜色选择 /*------------------------格式菜单项--------------------------------*/ /*------------------------查看菜单项----------------------------------*/ private JMenuItem checkStatus = null; //状态栏 /*------------------------查看菜单项------------------------------*/ /*------------------------帮助菜单项----------------------------*/ - private JMenuItem helpLook = null; //查看帮助 - private JMenuItem helpAbout = null; //关于记事本 - /*-----------------------帮助菜单项---------------------------*/ - /*-------------------------弹出菜单项-------------------------*/ - private JMenuItem popCut = null; //剪切 - private JMenuItem popCopy = null; //复制 - private JMenuItem popPaste = null; //粘贴 - /*-------------------------弹出菜单项-----------------------------*/ +private JMenuItem helpLook = null; //查看帮助 +private JMenuItem helpAbout = null; //关于记事本 +/*-----------------------帮助菜单项---------------------------*/ +/*-------------------------弹出菜单项-------------------------*/ +private JMenuItem popCut = null; //剪切 +private JMenuItem popCopy = null; //复制 +private JMenuItem popPaste = null; //粘贴 +/*-------------------------弹出菜单项-----------------------------*/ public MyFrame(String s,int x,int y,int w,int h) { //MyFrame的构造方法 setLocation(x,y); @@ -69,7 +69,7 @@ private JMenuItem checkStatus = null; //状态 } //初始化相关组件 private void init() { - /*--------------------------多行文本框--------------------------------*/ + /*------------多行文本框-----------------*/ textArea = new JTextArea(); //创建一个多行文本框 textArea.setFont(new Font("Courier",Font.PLAIN,16)); // 文本框字体风格,int风格,字体号 textArea.setCaretColor(Color.BLUE); // 光标颜色 @@ -78,7 +78,7 @@ private JMenuItem checkStatus = null; //状态 textArea.setLineWrap(true); //自动换行 textArea.setTabSize(4); //设置选项卡要扩大到的字符数 textArea.getDocument(); - /*--------------------------多行文本框----------------------------*/ + /*-----------多行文本框------------------*/ pane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); //创建一个滚动条面板 pane.setOpaque(false); //设置面板透明 @@ -86,7 +86,7 @@ private JMenuItem checkStatus = null; //状态 Container cpan = this.getContentPane(); //获取当前框架的内容面板 cpan.add(pane,BorderLayout.CENTER); // 将多行文本框添加到面板中央 menuBar = new JMenuBar(); //创建菜单条 - /*-------------------------------文件菜单------------------------------*/ + /*--------------文件菜单---------------*/ file = new JMenu("文件(F)"); //创建文件菜单 file.setMnemonic('F'); //助记符 fileNew = new JMenuItem("新建(N)"); //创建新建菜单项 @@ -98,8 +98,15 @@ private JMenuItem checkStatus = null; //状态 fileElseSave = new JMenuItem("另存为(A)"); //创建另存为菜单项 fileElseSave.setAccelerator(KeyStroke.getKeyStroke("ctrl A")); fileExit = new JMenuItem("退出(X)"); //创建退出菜单项 -/*-------------------------------文件菜单-------------------------------------*/ -/*-------------------------------编辑菜单------------------------------------*/ + //把菜单项加到文件菜单中 + file.add(fileNew); + file.add(fileOpen); + file.add(fileSave); + file.add(fileElseSave); + file.addSeparator(); //添加分隔符 + file.add(fileExit); + /*-------------文件菜单----------------------*/ + /*----------------编辑菜单-------------------*/ edit = new JMenu("编辑菜单(E)"); //创建编辑菜单 edit.setMnemonic('E'); //助记符 editUndo = new JMenuItem("撤销(Z)"); //创建撤销菜单项 @@ -111,21 +118,7 @@ private JMenuItem checkStatus = null; //状态 editPaste = new JMenuItem("粘贴(V)"); //创建粘贴菜单项 editPaste.setAccelerator(KeyStroke.getKeyStroke("ctrl V")); //快捷键 editClear = new JMenuItem("清除"); //创建清除菜单项 -/*-------------------------------编辑菜单------------------------------------*/ - - - - - -/*-----------------------------把菜单项加到文件菜单中--------------------------*/ - file.add(fileNew); - file.add(fileOpen); - file.add(fileSave); - file.add(fileElseSave); - file.addSeparator(); //添加分隔符 - file.add(fileExit); -/*----------------------------把菜单项加到文件菜单中------------------------*/ -/*----------------------------把菜单项加到编辑菜单中-------------------------*/ + //把菜单项加到编辑菜单中 edit.add(editUndo); edit.addSeparator(); //添加分隔符 edit.add(editCut); @@ -133,16 +126,51 @@ private JMenuItem checkStatus = null; //状态 edit.add(editPaste); edit.addSeparator(); //添加分隔符 edit.add(editClear); -/*----------------------------把菜单项加到编辑菜单中-------------------------*/ -/*-----------------------------把菜单加到菜单条-------------------------------*/ + /*--------------编辑菜单-------------------------*/ + /*------------格式菜单--------------------------*/ + format = new JMenu("格式(M)"); //创建格式菜单 + format.setMnemonic('M'); + formatWrap = new JMenuItem("自动换行(W)"); //创建自动换行菜单项 + formatChooseFont = new JMenuItem("字体选择(T)"); + formatChooseFontColor = new JMenuItem("字体颜色(R)"); + //把菜单项加入到格式菜单中 + format.add(formatWrap); + format.add(formatChooseFont); + format.add(formatChooseFontColor); + /*-----------格式菜单------------------------*/ + /*----------------查看菜单------------------*/ + check = new JMenu("查看(C)"); //创建查看菜单 + check.setMnemonic('C'); + checkStatus = new JMenuItem("状态栏(S)"); //创建状态栏菜单项 + //把菜单项加到查看菜单中 + check.add(checkStatus); + /*------------------查看菜单------------------*/ + /*--------------帮助菜单---------------------*/ + help = new JMenu("帮助(H)"); //创建帮助菜单 + help.setMnemonic('H'); + helpLook = new JMenuItem("查看帮助(L)"); //创建查看帮助菜单项 + helpAbout = new JMenuItem("关于记事本(A)"); + //把菜单项加入到帮助菜单 + help.add(helpLook); + help.add(helpAbout); + /*----------------帮助菜单----------------------*/ + /*----------------右键菜单----------------------*/ + popupMenu = new JPopupMenu(); + + + + /*----------------右键菜单----------------------*/ + /*----------------把菜单加到菜单条----------------*/ menuBar.add(file); menuBar.add(edit); - - -/*----------------------------把菜单加到菜单条-------------------------------*/ - + menuBar.add(format); + menuBar.add(check); + menuBar.add(help); + /*--------------把菜单加到菜单条-----------------*/ add(menuBar,BorderLayout.NORTH); //把菜单条放在布局的上面 add(pane,BorderLayout.CENTER); //把面板放在布局的中间 + + } -- Gitee From 072b486ba3c47a82952e1ea546523ca90f30379a Mon Sep 17 00:00:00 2001 From: Yin01 Date: Mon, 7 Jun 2021 23:55:54 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E7=9B=91=E8=A7=86=E5=99=A81.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/MyFrame.java | 74 ++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 14 deletions(-) diff --git a/src/java2020spring/MyFrame.java b/src/java2020spring/MyFrame.java index 6747ae5..0c07353 100644 --- a/src/java2020spring/MyFrame.java +++ b/src/java2020spring/MyFrame.java @@ -25,6 +25,8 @@ private JMenu format = null; // private JMenu check = null; //查看菜单 private JMenu help = null; //帮助菜单 private JPopupMenu popupMenu = null; //右键弹出式菜单 +private JToolBar toolBar = null; //底部状态栏 +private String codeStyle = "UTF-8"; //编码格式 /*-------------------------文件菜单项--------------------------*/ private JMenuItem fileNew = null; //新建 private JMenuItem fileOpen = null; //打开 @@ -51,17 +53,23 @@ private JMenuItem checkStatus = null; //状态 private JMenuItem helpLook = null; //查看帮助 private JMenuItem helpAbout = null; //关于记事本 /*-----------------------帮助菜单项---------------------------*/ -/*-------------------------弹出菜单项-------------------------*/ +/*-------------------------右键弹出菜单项-------------------------*/ private JMenuItem popCut = null; //剪切 private JMenuItem popCopy = null; //复制 private JMenuItem popPaste = null; //粘贴 -/*-------------------------弹出菜单项-----------------------------*/ +/*-------------------------右键弹出菜单项--------------------------*/ +/*-------------------------状态栏------------------------------*/ +private JLabel label = null; //当前字数 +private JLabel labelTime = null; //日期 +private JLabel labelCodeStyle = null; //编码 +/*-------------------------状态栏------------------------------*/ public MyFrame(String s,int x,int y,int w,int h) { //MyFrame的构造方法 setLocation(x,y); setTitle(s); setSize(w,h); init(); + listener(); setVisible(true); setDefaultCloseOperation(DISPOSE_ON_CLOSE); //初始化相关组件的方法 @@ -155,10 +163,13 @@ private JMenuItem popPaste = null; //粘 help.add(helpAbout); /*----------------帮助菜单----------------------*/ /*----------------右键菜单----------------------*/ - popupMenu = new JPopupMenu(); - - - + popupMenu = new JPopupMenu(); //创建右键菜单 + popCut = new JMenuItem("剪切"); + popCopy = new JMenuItem("复制"); + popPaste = new JMenuItem("粘贴"); + popupMenu.add(popCut); + popupMenu.add(popCopy); + popupMenu.add(popPaste); /*----------------右键菜单----------------------*/ /*----------------把菜单加到菜单条----------------*/ menuBar.add(file); @@ -166,20 +177,55 @@ private JMenuItem popPaste = null; //粘 menuBar.add(format); menuBar.add(check); menuBar.add(help); - /*--------------把菜单加到菜单条-----------------*/ + /*----------------把菜单加到菜单条----------------*/ + /*-------------状态栏-------------------------*/ + toolBar = new JToolBar(); + toolBar.setFloatable(false); //设置工具栏不能移动 + SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd"); //时间格式 + label = new JLabel("当前字数",SwingConstants.CENTER); + labelTime = new JLabel("日期"+time.format(new Date())); + labelCodeStyle = new JLabel("编码"+codeStyle); + toolBar.add(label); + toolBar.addSeparator(new Dimension(220,5)); //添加分隔符 + toolBar.add(labelTime); + toolBar.addSeparator(new Dimension(220,5)); //添加分隔符 + toolBar.add(labelCodeStyle); + /*-------------状态栏-------------------------*/ add(menuBar,BorderLayout.NORTH); //把菜单条放在布局的上面 add(pane,BorderLayout.CENTER); //把面板放在布局的中间 - - - - } - - - + add(toolBar,BorderLayout.SOUTH); //把底部状态栏加到布局的底部 + } + //监视器 + private void listener() { + textArea.addMouseListener(new MouseAdapter() { //为文本框添加鼠标监视器 + public void mousePressed(MouseEvent e) { + //按下鼠标键触发的鼠标事件 + //补充 + } + public void mouseReleased(MouseEvent e) { + //释放鼠标键触发的鼠标事件 + //补充 + } + }); + textArea.getDocument().addDocumentListener(new DocumentListener() { + //监视文本区改变 + public void insertUpdate(DocumentEvent e) { + //补充 + + } + public void removeUpdate(DocumentEvent e) { + //补充 + } + public void changedUpdate(DocumentEvent e) { + //补充 + } + }); + + } } -- Gitee From 855f9ec188a5284ffd9e4aa2dfea6f510ebfb636 Mon Sep 17 00:00:00 2001 From: Yin01 Date: Tue, 8 Jun 2021 21:58:47 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E4=B8=BB=E8=A6=81=E5=81=9A=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E9=A1=B9=E7=9B=B8=E5=BA=94=E7=9A=84=E7=9B=91=E8=A7=86?= =?UTF-8?q?=E5=99=A8=EF=BC=8C=E5=81=9A=E4=BA=86=E6=96=B0=E5=BB=BA=E5=92=8C?= =?UTF-8?q?=E6=89=93=E5=BC=80=E7=9B=91=E8=A7=86=E5=99=A8=E3=80=82=20?= =?UTF-8?q?=E7=9B=91=E8=A7=86=E5=99=A81.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/MyFrame.java | 62 ++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/src/java2020spring/MyFrame.java b/src/java2020spring/MyFrame.java index 0c07353..e857995 100644 --- a/src/java2020spring/MyFrame.java +++ b/src/java2020spring/MyFrame.java @@ -27,6 +27,8 @@ private JMenu help = null; // private JPopupMenu popupMenu = null; //右键弹出式菜单 private JToolBar toolBar = null; //底部状态栏 private String codeStyle = "UTF-8"; //编码格式 +private JFileChooser chooser = null; //文件选择对象 +private File fileChooser = null; //文件对象 /*-------------------------文件菜单项--------------------------*/ private JMenuItem fileNew = null; //新建 private JMenuItem fileOpen = null; //打开 @@ -220,7 +222,65 @@ private JLabel labelCodeStyle = null; // //补充 } }); - +/*--------------------------------菜单项相应的监视器--------------------------------*/ + fileNew.addActionListener(new ActionListener() { + //新建监视器 + public void actionPerformed(ActionEvent e) { + if(!textArea.getText().equals("")) { //文本框有内容 + int have = JOptionPane.showConfirmDialog(null,"当前内容没有保存,需要保存吗?","提示!!!",JOptionPane.YES_NO_OPTION); + //选择存储结果 + if(have==JOptionPane.YES_OPTION) { //先保存,再新建 + //补 **保存 + textArea.setText(""); + this.setTitle("新建记事本"); + file = null; + } + else { //清空页面,再新建 + textArea.setText(""); + this.setTitle("新建记事本"); + file = null; + } + } + } + private void setTitle(String string) {} //新建记事本标题的方法 + }); + fileOpen.addActionListener(new ActionListener() { + //打开文件监视器 + public void actionPerformed(ActionEvent e) { + try { + chooser = new JFileChooser("新加卷A:/javakeshe/savefile/"); //设置打开时默认目录 + int result = chooser.showOpenDialog(null); + if(result==JFileChooser.APPROVE_OPTION) { //点击打开按钮 + fileChooser = chooser.getSelectedFile(); + int length = (int)fileChooser.length(); + FileReader reader = new FileReader(fileChooser); + char[] ch = new char[length]; + reader.read(ch); // 将文件读进char数组 + reader.close(); + textArea.setText(new String(ch).trim()); //删除字符串的头尾空白符 + setTitle(fileChooser.getName()); //框架标题设置为文件名 + } + }catch(Exception c) { + JOptionPane.showMessageDialog(null,c); + } + } + }); + fileSave.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + } + }); + fileElseSave.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + } + }); + fileExit.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + } + }); + /*--------------------------------菜单项相应的监视器--------------------------------*/ -- Gitee From 0e5343d76e36e5c7922bc037e4f1bd83c0e70a08 Mon Sep 17 00:00:00 2001 From: Yin01 Date: Wed, 9 Jun 2021 23:38:54 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=8F=9C=E5=8D=95=E9=A1=B9=E7=9A=84=E7=9B=91=E8=A7=86?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/MyFrame.java | 166 ++++++++++++++++++++++++++++---- 1 file changed, 149 insertions(+), 17 deletions(-) diff --git a/src/java2020spring/MyFrame.java b/src/java2020spring/MyFrame.java index e857995..6a02e0e 100644 --- a/src/java2020spring/MyFrame.java +++ b/src/java2020spring/MyFrame.java @@ -3,12 +3,14 @@ package java2020spring; import javax.swing.*; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; +import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.undo.UndoManager; import java.awt.*; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.event.*; import java.io.*; +import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; /* @@ -29,6 +31,7 @@ private JToolBar toolBar = null; // private String codeStyle = "UTF-8"; //编码格式 private JFileChooser chooser = null; //文件选择对象 private File fileChooser = null; //文件对象 +private UndoManager manager= null; //撤销操作对象 /*-------------------------文件菜单项--------------------------*/ private JMenuItem fileNew = null; //新建 private JMenuItem fileOpen = null; //打开 @@ -223,14 +226,33 @@ private JLabel labelCodeStyle = null; // } }); /*--------------------------------菜单项相应的监视器--------------------------------*/ - fileNew.addActionListener(new ActionListener() { - //新建监视器 + fileNew.addActionListener(new ActionListener() { //新建匿名监视器 + @Override public void actionPerformed(ActionEvent e) { if(!textArea.getText().equals("")) { //文本框有内容 int have = JOptionPane.showConfirmDialog(null,"当前内容没有保存,需要保存吗?","提示!!!",JOptionPane.YES_NO_OPTION); - //选择存储结果 + //确认对话框 if(have==JOptionPane.YES_OPTION) { //先保存,再新建 - //补 **保存 + //保存 + String path; + String content = textArea.getText(); + JFileChooser chooser = new JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件(*.txt)","txt"); + chooser.setFileFilter(filter); + int res = chooser.showSaveDialog(null); //自定义保存对话框 + if(res == JFileChooser.APPROVE_OPTION) { //点击保存按钮 + path = chooser.getSelectedFile().getAbsolutePath()+".txt"; //获得保存路径 + try { + + OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path),"gbk"); + + writer.write(content); + writer.close(); + } + catch(IOException e2) { + e2.printStackTrace(); + } + } //保存 textArea.setText(""); this.setTitle("新建记事本"); file = null; @@ -242,14 +264,14 @@ private JLabel labelCodeStyle = null; // } } } - private void setTitle(String string) {} //新建记事本标题的方法 + private void setTitle(String string) {} //新建记事本标题的方法 }); - fileOpen.addActionListener(new ActionListener() { - //打开文件监视器 + fileOpen.addActionListener(new ActionListener() { //打开文件匿名监视器 + @Override public void actionPerformed(ActionEvent e) { try { - chooser = new JFileChooser("新加卷A:/javakeshe/savefile/"); //设置打开时默认目录 - int result = chooser.showOpenDialog(null); + chooser = new JFileChooser("A:/javakeshe/savefile/"); //设置打开时默认目录 + int result = chooser.showOpenDialog(null); //自定义打开对话框 if(result==JFileChooser.APPROVE_OPTION) { //点击打开按钮 fileChooser = chooser.getSelectedFile(); int length = (int)fileChooser.length(); @@ -260,26 +282,136 @@ private JLabel labelCodeStyle = null; // textArea.setText(new String(ch).trim()); //删除字符串的头尾空白符 setTitle(fileChooser.getName()); //框架标题设置为文件名 } - }catch(Exception c) { - JOptionPane.showMessageDialog(null,c); + }catch(Exception e1) { + JOptionPane.showMessageDialog(null,e1); } } }); - fileSave.addActionListener(new ActionListener() { + fileSave.addActionListener(new ActionListener() { //保存匿名监视器 + @Override public void actionPerformed(ActionEvent e) { - + String path; + String content = textArea.getText(); + JFileChooser chooser = new JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件(*.txt)","txt"); + chooser.setFileFilter(filter); + int res = chooser.showSaveDialog(null); //自定义保存对话框 + if(res == JFileChooser.APPROVE_OPTION) { //点击保存按钮 + path = chooser.getSelectedFile().getAbsolutePath()+".txt"; //获得保存路径 + try { + + OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path),"gbk"); + + writer.write(content); + writer.close(); + } + catch(IOException e2) { + e2.printStackTrace(); + } + } } }); - fileElseSave.addActionListener(new ActionListener() { + fileElseSave.addActionListener(new ActionListener() { //另存为匿名监视器 + @Override public void actionPerformed(ActionEvent e) { - + String path; + String content = textArea.getText(); + JFileChooser chooser = new JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件(*.txt)","txt"); + chooser.setFileFilter(filter); + int res = chooser.showSaveDialog(null); //自定义保存对话框 + if(res == JFileChooser.APPROVE_OPTION) { //点击保存按钮 + path = chooser.getSelectedFile().getAbsolutePath()+".txt"; //获得保存路径 + try { + OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path),"gbk"); + + writer.write(content); + writer.close(); + } + catch(IOException e2) { + e2.printStackTrace(); + } + } } }); - fileExit.addActionListener(new ActionListener() { + fileExit.addActionListener(new ActionListener() { //退出匿名监视器 + @Override public void actionPerformed(ActionEvent e) { - + if(!textArea.getText().equals("")) { + int re = JOptionPane.showConfirmDialog(null,"当前内容没有保存,您需要保存吗?","提示",JOptionPane.YES_NO_OPTION); + if(re==JOptionPane.YES_OPTION) { + //保存 + String path; + String content = textArea.getText(); + JFileChooser chooser = new JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件(*.txt)","txt"); + chooser.setFileFilter(filter); + int res = chooser.showSaveDialog(null); //自定义保存对话框 + if(res == JFileChooser.APPROVE_OPTION) { //点击保存按钮 + path = chooser.getSelectedFile().getAbsolutePath()+".txt"; //获得保存路径 + try { + OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path),"gbk"); + + writer.write(content); + writer.close(); + } + catch(IOException e2) { + e2.printStackTrace(); + } + } //保存 + System.exit(0); + } + } + else { + System.exit(0); + } } }); + editUndo.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + + } + + }); + editCut.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + + } + + }); + editCopy.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + + } + + }); + editPaste.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + + } + + }); + editClear.addActionListener(new ActionListener() { //清除匿名监视器 + + @Override + public void actionPerformed(ActionEvent e) { + textArea.setText(""); + } + + }); + + + + + /*--------------------------------菜单项相应的监视器--------------------------------*/ -- Gitee From 060b9c39f0740b6df593bcec158bbac512f207e8 Mon Sep 17 00:00:00 2001 From: Yin01 Date: Thu, 10 Jun 2021 10:55:13 +0800 Subject: [PATCH 7/9] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/MyFrame.java | 112 ++++++++++++++++---------------- 1 file changed, 57 insertions(+), 55 deletions(-) diff --git a/src/java2020spring/MyFrame.java b/src/java2020spring/MyFrame.java index 6a02e0e..bb863e8 100644 --- a/src/java2020spring/MyFrame.java +++ b/src/java2020spring/MyFrame.java @@ -6,7 +6,9 @@ import javax.swing.event.DocumentListener; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.undo.UndoManager; import java.awt.*; +import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.event.*; import java.io.*; @@ -32,6 +34,8 @@ private String codeStyle = "UTF-8"; // private JFileChooser chooser = null; //文件选择对象 private File fileChooser = null; //文件对象 private UndoManager manager= null; //撤销操作对象 +private Clipboard clipboard=null; +TextArea text1,text2; /*-------------------------文件菜单项--------------------------*/ private JMenuItem fileNew = null; //新建 private JMenuItem fileOpen = null; //打开 @@ -74,13 +78,12 @@ private JLabel labelCodeStyle = null; // setTitle(s); setSize(w,h); init(); - listener(); setVisible(true); setDefaultCloseOperation(DISPOSE_ON_CLOSE); //初始化相关组件的方法 } - //初始化相关组件 +//初始化相关组件 private void init() { /*------------多行文本框-----------------*/ textArea = new JTextArea(); //创建一个多行文本框 @@ -99,6 +102,13 @@ private JLabel labelCodeStyle = null; // Container cpan = this.getContentPane(); //获取当前框架的内容面板 cpan.add(pane,BorderLayout.CENTER); // 将多行文本框添加到面板中央 menuBar = new JMenuBar(); //创建菜单条 + manager = new UndoManager(); //创建一个撤销管理对象 + textArea.getDocument().addUndoableEditListener(manager); //给文本框添加可撤销监听 + clipboard=getToolkit().getSystemClipboard();//获取系统剪贴板 + text1 = new TextArea(20,20); + text2 = new TextArea(20,20); + add(text1); + add(text2); /*--------------文件菜单---------------*/ file = new JMenu("文件(F)"); //创建文件菜单 file.setMnemonic('F'); //助记符 @@ -199,32 +209,42 @@ private JLabel labelCodeStyle = null; // add(menuBar,BorderLayout.NORTH); //把菜单条放在布局的上面 add(pane,BorderLayout.CENTER); //把面板放在布局的中间 add(toolBar,BorderLayout.SOUTH); //把底部状态栏加到布局的底部 - } - //监视器 - private void listener() { - textArea.addMouseListener(new MouseAdapter() { //为文本框添加鼠标监视器 - public void mousePressed(MouseEvent e) { - //按下鼠标键触发的鼠标事件 - //补充 - } - public void mouseReleased(MouseEvent e) { - //释放鼠标键触发的鼠标事件 - //补充 - } - }); - textArea.getDocument().addDocumentListener(new DocumentListener() { - //监视文本区改变 - public void insertUpdate(DocumentEvent e) { - //补充 - - } - public void removeUpdate(DocumentEvent e) { - //补充 - } - public void changedUpdate(DocumentEvent e) { - //补充 + + + + this.addWindowListener(new WindowAdapter() { //为框架添加退出保存的监视器 + public void windowClosing(WindowEvent e) { + if(textArea.getText().equals("")) + System.exit(0); + else { + if(JOptionPane.showConfirmDialog(null,"需要保存吗?","提示!",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION) { + //保存 + String path; + String content = textArea.getText(); + JFileChooser chooser = new JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件(*.txt)","txt"); + chooser.setFileFilter(filter); + int res = chooser.showSaveDialog(null); //自定义保存对话框 + if(res == JFileChooser.APPROVE_OPTION) { //点击保存按钮 + path = chooser.getSelectedFile().getAbsolutePath()+".txt"; //获得保存路径 + try { + + OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path),"gbk"); + + writer.write(content); + writer.close(); + } + catch(IOException e2) { + e2.printStackTrace(); + } + } //保存 + System.exit(0); + }else + System.exit(0); + } } - }); + + }); /*--------------------------------菜单项相应的监视器--------------------------------*/ fileNew.addActionListener(new ActionListener() { //新建匿名监视器 @Override @@ -301,9 +321,9 @@ private JLabel labelCodeStyle = null; // try { OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path),"gbk"); - writer.write(content); writer.close(); + } catch(IOException e2) { e2.printStackTrace(); @@ -367,38 +387,18 @@ private JLabel labelCodeStyle = null; // } } }); - editUndo.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - - } - - }); - editCut.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - - } - - }); - editCopy.addActionListener(new ActionListener() { + editCut.addActionListener(e -> textArea.cut()); //实现剪切功能 + editCopy.addActionListener(e -> textArea.copy()); //实现复制功能 + editPaste.addActionListener(e -> textArea.paste()); //实现粘贴功能 + editUndo.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - + if(manager.canUndo()) { //撤销 + manager.undo(); + } } - }); - editPaste.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - - } - - }); editClear.addActionListener(new ActionListener() { //清除匿名监视器 @Override @@ -408,6 +408,8 @@ private JLabel labelCodeStyle = null; // }); + + -- Gitee From 3130c4abe0ee1628e415fbfcd7b4304b6100e7de Mon Sep 17 00:00:00 2001 From: Yin01 Date: Fri, 11 Jun 2021 13:42:06 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E8=AE=B0=E4=BA=8B=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E5=85=A8=E9=83=A8=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helpAbout.jpg | Bin 0 -> 9264 bytes src/java2020spring/AboutDialog.java | 45 +++++++ src/java2020spring/MyFrame.java | 190 +++++++++++++++++++--------- src/java2020spring/launcher.java | 7 + 4 files changed, 183 insertions(+), 59 deletions(-) create mode 100644 helpAbout.jpg create mode 100644 src/java2020spring/AboutDialog.java create mode 100644 src/java2020spring/launcher.java diff --git a/helpAbout.jpg b/helpAbout.jpg new file mode 100644 index 0000000000000000000000000000000000000000..8a30bea32ae22b25be13ec8dbcee8bcb33eea773 GIT binary patch literal 9264 zcmb7qRa9I}w{16#L*v060z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6Qp1AC?VSA ztgukr7)e)X)niY{uQPF4GZcZscyW*MM2XLnlXXsIZx}w1^J8Hjh4wb=1)yROV6Tl_ z^T}J%+CE(KfYP%W)Yae6NQSa+f3GbhL;R%mGv({<;QU9N?RmZB*~Lhq*l*kJ@u`ct zu75Bgw~`+MzPpk|&iH)eecOz1afVK3mc`jtU$6R26FX`i4kBLDfKedCa}f&&G|(pA z!!>?g$hIEWv|{y5)~ww-+r9wa?5v(^83rQb#$aLX7ueX~N_Q1#v!eL7D@&2n3uN7Z P(I#0w{dZ6BW#PX7Q#ziA literal 0 HcmV?d00001 diff --git a/src/java2020spring/AboutDialog.java b/src/java2020spring/AboutDialog.java new file mode 100644 index 0000000..7d19d3b --- /dev/null +++ b/src/java2020spring/AboutDialog.java @@ -0,0 +1,45 @@ +package java2020spring; +import javax.swing.*; +import java.awt.*; +/* + * * Created by Haiyin + */ +public class AboutDialog extends JDialog { + private static final int ABOUT_WIDTH=600; //窗口默认高度 + private static final int ABOUT_HEIGHT=400; //窗口默认高度 + private Icon about = new ImageIcon("helpAbout.jpg"); + private JLabel desc = null; //文本标签 + private JPanel panel = null; //内容面板 + private JButton btn = null; //按钮 + public AboutDialog(Frame frame,String title,boolean modal) { + super(frame,title,modal); //将对话框放进当前框架,设置标题和模态 + init(); + Listener(); + } + private void init() { + desc = new JLabel(); //新建文本标签 + desc.setText("欢迎来到记事本,希望你使用愉快!"); + desc.setHorizontalAlignment(JLabel.CENTER); //设置文本标签位置 + panel = new JPanel(); //新建内容面板 + btn = new JButton(); + btn.setIcon(about); + panel.add(btn); + this.add(desc); //文本默认在边界管理器中央 + this.add(panel,BorderLayout.SOUTH); //将面板放在便捷管理器下方 + this.setSize(ABOUT_WIDTH,ABOUT_HEIGHT); //设置对话框大小 + } + private void Listener() { + btn.addActionListener(e -> {AboutDialog.this.dispose();//退出对话框 + }); + Toolkit kit = Toolkit.getDefaultToolkit(); //获得工具包 + Dimension screenSize = kit.getScreenSize(); //获得尺寸 + int screenWidth=screenSize.width; //获得当前屏幕宽度 + int screenHeight=screenSize.height; //获得当前屏幕高度 + int windowsWidth=this.getWidth(); //获得当前窗口宽度 + int windowsHeight=this.getHeight(); //获得当前窗口高度 + this.setLocation((screenWidth-windowsWidth)/2,(screenHeight-windowsHeight)/2); + //设置窗口位置 + this.setVisible(true); //设置窗口可见 + + } +} diff --git a/src/java2020spring/MyFrame.java b/src/java2020spring/MyFrame.java index bb863e8..ab0d67f 100644 --- a/src/java2020spring/MyFrame.java +++ b/src/java2020spring/MyFrame.java @@ -7,18 +7,18 @@ import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.undo.UndoManager; import java.awt.*; import java.awt.datatransfer.Clipboard; -import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.StringSelection; -import java.awt.datatransfer.Transferable; import java.awt.event.*; import java.io.*; -import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; /* * *Created by HaiyinChen */ public class MyFrame extends JFrame{ +/** + *By HaiYinCheng + */ +private static final long serialVersionUID = 7609614339639993905L; private static final int DEFAULT_FONTSIZE = 20; //默认字体大小 private JTextArea textArea = null; //多行文本框 private JScrollPane pane = null; //带滚动条的面板 @@ -26,7 +26,6 @@ private JMenuBar menuBar = null; // private JMenu file = null; //文件菜单 private JMenu edit = null; //编辑菜单 private JMenu format = null; //格式菜单 -private JMenu check = null; //查看菜单 private JMenu help = null; //帮助菜单 private JPopupMenu popupMenu = null; //右键弹出式菜单 private JToolBar toolBar = null; //底部状态栏 @@ -52,12 +51,8 @@ private JMenuItem editClear = null; //删 /*------------------------编辑菜单项-----------------------------*/ /*------------------------格式菜单项-----------------------------*/ private JMenuItem formatWrap = null; //自动换行 -private JMenuItem formatChooseFont = null; //字体选择 private JMenuItem formatChooseFontColor = null; //字体颜色选择 /*------------------------格式菜单项--------------------------------*/ -/*------------------------查看菜单项----------------------------------*/ -private JMenuItem checkStatus = null; //状态栏 -/*------------------------查看菜单项------------------------------*/ /*------------------------帮助菜单项----------------------------*/ private JMenuItem helpLook = null; //查看帮助 private JMenuItem helpAbout = null; //关于记事本 @@ -71,6 +66,7 @@ private JMenuItem popPaste = null; //粘 private JLabel label = null; //当前字数 private JLabel labelTime = null; //日期 private JLabel labelCodeStyle = null; //编码 +private int screenSize; /*-------------------------状态栏------------------------------*/ public MyFrame(String s,int x,int y,int w,int h) { //MyFrame的构造方法 @@ -78,13 +74,18 @@ private JLabel labelCodeStyle = null; // setTitle(s); setSize(w,h); init(); + Listener(); setVisible(true); setDefaultCloseOperation(DISPOSE_ON_CLOSE); //初始化相关组件的方法 } + + + //初始化相关组件 private void init() { + /*------------多行文本框-----------------*/ textArea = new JTextArea(); //创建一个多行文本框 textArea.setFont(new Font("Courier",Font.PLAIN,16)); // 文本框字体风格,int风格,字体号 @@ -95,8 +96,11 @@ private JLabel labelCodeStyle = null; // textArea.setTabSize(4); //设置选项卡要扩大到的字符数 textArea.getDocument(); /*-----------多行文本框------------------*/ - pane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, - JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); //创建一个滚动条面板 + pane = new JScrollPane(textArea); + pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); + pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + + //创建一个滚动条面板 pane.setOpaque(false); //设置面板透明 pane.getViewport().setOpaque(false); Container cpan = this.getContentPane(); //获取当前框架的内容面板 @@ -154,20 +158,11 @@ private JLabel labelCodeStyle = null; // format = new JMenu("格式(M)"); //创建格式菜单 format.setMnemonic('M'); formatWrap = new JMenuItem("自动换行(W)"); //创建自动换行菜单项 - formatChooseFont = new JMenuItem("字体选择(T)"); formatChooseFontColor = new JMenuItem("字体颜色(R)"); //把菜单项加入到格式菜单中 format.add(formatWrap); - format.add(formatChooseFont); format.add(formatChooseFontColor); /*-----------格式菜单------------------------*/ - /*----------------查看菜单------------------*/ - check = new JMenu("查看(C)"); //创建查看菜单 - check.setMnemonic('C'); - checkStatus = new JMenuItem("状态栏(S)"); //创建状态栏菜单项 - //把菜单项加到查看菜单中 - check.add(checkStatus); - /*------------------查看菜单------------------*/ /*--------------帮助菜单---------------------*/ help = new JMenu("帮助(H)"); //创建帮助菜单 help.setMnemonic('H'); @@ -190,7 +185,6 @@ private JLabel labelCodeStyle = null; // menuBar.add(file); menuBar.add(edit); menuBar.add(format); - menuBar.add(check); menuBar.add(help); /*----------------把菜单加到菜单条----------------*/ /*-------------状态栏-------------------------*/ @@ -201,17 +195,29 @@ private JLabel labelCodeStyle = null; // labelTime = new JLabel("日期"+time.format(new Date())); labelCodeStyle = new JLabel("编码"+codeStyle); toolBar.add(label); - toolBar.addSeparator(new Dimension(220,5)); //添加分隔符 + toolBar.addSeparator(new Dimension(150,5)); //添加分隔符 toolBar.add(labelTime); - toolBar.addSeparator(new Dimension(220,5)); //添加分隔符 + toolBar.addSeparator(new Dimension(150,5)); //添加分隔符 toolBar.add(labelCodeStyle); /*-------------状态栏-------------------------*/ add(menuBar,BorderLayout.NORTH); //把菜单条放在布局的上面 add(pane,BorderLayout.CENTER); //把面板放在布局的中间 add(toolBar,BorderLayout.SOUTH); //把底部状态栏加到布局的底部 + Toolkit kit = Toolkit.getDefaultToolkit(); //获得工具包 + Dimension screenSize = kit.getScreenSize(); //获得尺寸 + int screenWidth=screenSize.width; //获得当前屏幕宽度 + int screenHeight=screenSize.height; //获得当前屏幕高度 + int windowsWidth=this.getWidth(); //获得当前窗口宽度 + int windowsHeight=this.getHeight(); //获得当前窗口高度 + this.setLocation((screenWidth-windowsWidth)/2,(screenHeight-windowsHeight)/2); + //设置窗口位置 + this.setVisible(true); //设置窗口可见 + } + - - + +private void Listener() { + this.addWindowListener(new WindowAdapter() { //为框架添加退出保存的监视器 public void windowClosing(WindowEvent e) { if(textArea.getText().equals("")) @@ -245,8 +251,41 @@ private JLabel labelCodeStyle = null; // } }); +/*------------------------------右键菜单相应的监视器-----------------------*/ + textArea.addMouseListener(new MouseAdapter() { //为文本框添加鼠标监视器 + public void mousePressed(MouseEvent e) { + //按下鼠标键触发的鼠标事件 + maybeShowPopup(e); + } + public void mouseReleased(MouseEvent e) { + //释放鼠标键触发的鼠标事件 + maybeShowPopup(e); + } + }); + textArea.getDocument().addDocumentListener(new DocumentListener() { + //监听文本改变 + public void insertUpdate(DocumentEvent e) { + isItemsAvalible(); //如果文本改变就设置其他按钮的可用性 + changeTextLengthStatus(); //实时显示文本字数 + } + public void removeUpdate(DocumentEvent e) { + isItemsAvalible(); //如果文本改变就设置其他按钮的可用性 + changeTextLengthStatus(); //实时显示文本字数 + } + public void changedUpdate(DocumentEvent e) { + changeTextLengthStatus(); //实时显示文本字数 + isItemsAvalible(); //如果文本改变就设置其他按钮的可用性 + } + }); + + + + popCut.addActionListener(e -> textArea.cut()); + popCopy.addActionListener(e -> textArea.copy()); + popPaste.addActionListener(e -> textArea.paste()); + helpAbout.addActionListener(e -> new AboutDialog(MyFrame.this,"About-NotePad",true)); /*--------------------------------菜单项相应的监视器--------------------------------*/ - fileNew.addActionListener(new ActionListener() { //新建匿名监视器 + fileNew.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(!textArea.getText().equals("")) { //文本框有内容 @@ -283,14 +322,14 @@ private JLabel labelCodeStyle = null; // file = null; } } - } + } //新建匿名监视器 private void setTitle(String string) {} //新建记事本标题的方法 }); - fileOpen.addActionListener(new ActionListener() { //打开文件匿名监视器 + fileOpen.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { - chooser = new JFileChooser("A:/javakeshe/savefile/"); //设置打开时默认目录 + chooser = new JFileChooser("D://"); //设置打开时默认目录 int result = chooser.showOpenDialog(null); //自定义打开对话框 if(result==JFileChooser.APPROVE_OPTION) { //点击打开按钮 fileChooser = chooser.getSelectedFile(); @@ -305,46 +344,44 @@ private JLabel labelCodeStyle = null; // }catch(Exception e1) { JOptionPane.showMessageDialog(null,e1); } - } + } //打开文件匿名监视器 }); - fileSave.addActionListener(new ActionListener() { //保存匿名监视器 + + fileSave.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - String path; + String path = null; String content = textArea.getText(); JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件(*.txt)","txt"); chooser.setFileFilter(filter); int res = chooser.showSaveDialog(null); //自定义保存对话框 if(res == JFileChooser.APPROVE_OPTION) { //点击保存按钮 - path = chooser.getSelectedFile().getAbsolutePath()+".txt"; //获得保存路径 try { - OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path),"gbk"); + path = chooser.getSelectedFile().getAbsolutePath()+".txt"; //获得保存路径 writer.write(content); writer.close(); - } catch(IOException e2) { e2.printStackTrace(); } } - } + }//保存匿名监视器 }); - fileElseSave.addActionListener(new ActionListener() { //另存为匿名监视器 + fileElseSave.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - String path; + String path = null; String content = textArea.getText(); JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件(*.txt)","txt"); chooser.setFileFilter(filter); - int res = chooser.showSaveDialog(null); //自定义保存对话框 + int res = chooser.showSaveDialog(null); //自定义另存为对话框 if(res == JFileChooser.APPROVE_OPTION) { //点击保存按钮 - path = chooser.getSelectedFile().getAbsolutePath()+".txt"; //获得保存路径 try { OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path),"gbk"); - + path = chooser.getSelectedFile().getAbsolutePath()+".txt"; //获得保存路径 writer.write(content); writer.close(); } @@ -352,9 +389,9 @@ private JLabel labelCodeStyle = null; // e2.printStackTrace(); } } - } + } //另存为匿名监视器 }); - fileExit.addActionListener(new ActionListener() { //退出匿名监视器 + fileExit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(!textArea.getText().equals("")) { @@ -385,41 +422,76 @@ private JLabel labelCodeStyle = null; // else { System.exit(0); } - } + }//退出匿名监视器 }); editCut.addActionListener(e -> textArea.cut()); //实现剪切功能 editCopy.addActionListener(e -> textArea.copy()); //实现复制功能 editPaste.addActionListener(e -> textArea.paste()); //实现粘贴功能 - editUndo.addActionListener(new ActionListener() { + editUndo.addActionListener(new ActionListener() { //撤销匿名监视器 @Override public void actionPerformed(ActionEvent e) { if(manager.canUndo()) { //撤销 manager.undo(); } - } + } //撤销匿名监视器 }); - editClear.addActionListener(new ActionListener() { //清除匿名监视器 + editClear.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textArea.setText(""); - } - - }); - + }//清除匿名监视器 + }); + formatWrap.addActionListener(new ActionListener() { - - - - - - /*--------------------------------菜单项相应的监视器--------------------------------*/ + @Override + public void actionPerformed(ActionEvent e) { + textArea.setLineWrap(true); //激活自动换行功能 + textArea.setWrapStyleWord(true); //激活断行不断字功能 + } //自动换行匿名监视器 + }); + formatChooseFontColor.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + Color color=JColorChooser.showDialog(null, "请选择你喜欢的颜色", Color.WHITE); + textArea.setForeground(color); + } //字体颜色选择 + }); +/*--------------------------------菜单项相应的监视器--------------------------------*/ + } +private void maybeShowPopup(MouseEvent e) { //监听鼠标 + if(e.isPopupTrigger()) { //判断是否触发了弹出菜单这一动作 + popupMenu.show(e.getComponent(),e.getX(),e.getY()); //获得鼠标事件的对象,获得鼠标的位置 + } + +} +private void isItemsAvalible() { //监视文本区并设置各功能项是否可用 + String content = textArea.getText(); + if(content.equals("")) { + fileSave.setEnabled(false); + editCut.setEnabled(false); + editCopy.setEnabled(false); + editClear.setEnabled(false); + } + else { + fileSave.setEnabled(true); + editCut.setEnabled(true); + editCopy.setEnabled(true); + editClear.setEnabled(true); + } +} +private void changeTextLengthStatus() { //文本监听 + String content = textArea.getText().trim(); + label.setText("当前字数:"+content.length()); + +} +} + - } -} diff --git a/src/java2020spring/launcher.java b/src/java2020spring/launcher.java new file mode 100644 index 0000000..b15d4f3 --- /dev/null +++ b/src/java2020spring/launcher.java @@ -0,0 +1,7 @@ +package java2020spring; + +public class launcher { + public static void main(String[] args) { + MyFrame win = new MyFrame("记事本",40,50,700,450); + } +} -- Gitee From 1d0b86b51209acc04d6f46299b57d18237d84a67 Mon Sep 17 00:00:00 2001 From: Yin01 Date: Fri, 11 Jun 2021 19:47:58 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E8=AE=B0=E4=BA=8B=E6=9C=AC=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E5=95=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/AboutDialog.java | 10 +++++----- src/java2020spring/MyFrame.java | 4 ---- src/java2020spring/launcher.java | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/java2020spring/AboutDialog.java b/src/java2020spring/AboutDialog.java index 7d19d3b..cbfbb52 100644 --- a/src/java2020spring/AboutDialog.java +++ b/src/java2020spring/AboutDialog.java @@ -10,7 +10,7 @@ public class AboutDialog extends JDialog { private Icon about = new ImageIcon("helpAbout.jpg"); private JLabel desc = null; //文本标签 private JPanel panel = null; //内容面板 - private JButton btn = null; //按钮 + private JButton bu = null; //按钮 public AboutDialog(Frame frame,String title,boolean modal) { super(frame,title,modal); //将对话框放进当前框架,设置标题和模态 init(); @@ -21,15 +21,15 @@ public class AboutDialog extends JDialog { desc.setText("欢迎来到记事本,希望你使用愉快!"); desc.setHorizontalAlignment(JLabel.CENTER); //设置文本标签位置 panel = new JPanel(); //新建内容面板 - btn = new JButton(); - btn.setIcon(about); - panel.add(btn); + bu = new JButton(); + bu.setIcon(about); + panel.add(bu); this.add(desc); //文本默认在边界管理器中央 this.add(panel,BorderLayout.SOUTH); //将面板放在便捷管理器下方 this.setSize(ABOUT_WIDTH,ABOUT_HEIGHT); //设置对话框大小 } private void Listener() { - btn.addActionListener(e -> {AboutDialog.this.dispose();//退出对话框 + bu.addActionListener(e -> {AboutDialog.this.dispose();//退出对话框 }); Toolkit kit = Toolkit.getDefaultToolkit(); //获得工具包 Dimension screenSize = kit.getScreenSize(); //获得尺寸 diff --git a/src/java2020spring/MyFrame.java b/src/java2020spring/MyFrame.java index ab0d67f..5d19f62 100644 --- a/src/java2020spring/MyFrame.java +++ b/src/java2020spring/MyFrame.java @@ -109,10 +109,6 @@ private int screenSize; manager = new UndoManager(); //创建一个撤销管理对象 textArea.getDocument().addUndoableEditListener(manager); //给文本框添加可撤销监听 clipboard=getToolkit().getSystemClipboard();//获取系统剪贴板 - text1 = new TextArea(20,20); - text2 = new TextArea(20,20); - add(text1); - add(text2); /*--------------文件菜单---------------*/ file = new JMenu("文件(F)"); //创建文件菜单 file.setMnemonic('F'); //助记符 diff --git a/src/java2020spring/launcher.java b/src/java2020spring/launcher.java index b15d4f3..55c924d 100644 --- a/src/java2020spring/launcher.java +++ b/src/java2020spring/launcher.java @@ -2,6 +2,6 @@ package java2020spring; public class launcher { public static void main(String[] args) { - MyFrame win = new MyFrame("记事本",40,50,700,450); + MyFrame win = new MyFrame("记事本",40,50,700,450); //记事本 } } -- Gitee