From d39675a72bef1e9087776ae07f1a5386dabc708c Mon Sep 17 00:00:00 2001 From: wu Date: Tue, 11 May 2021 22:48:44 +0800 Subject: [PATCH 01/19] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E8=AE=B0=E4=BA=8B=E6=9C=AC=E5=9F=BA=E6=9C=AC=E6=A1=86?= =?UTF-8?q?=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .project | 2 +- src/Notebook.java | 518 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 519 insertions(+), 1 deletion(-) create mode 100644 src/Notebook.java diff --git a/.project b/.project index 79a6e57..7216fba 100644 --- a/.project +++ b/.project @@ -1,6 +1,6 @@ - java2020spring + java2021spring diff --git a/src/Notebook.java b/src/Notebook.java new file mode 100644 index 0000000..3ac0e10 --- /dev/null +++ b/src/Notebook.java @@ -0,0 +1,518 @@ +import java.awt.BorderLayout; +import java.awt.EventQueue; +import java.awt.Font; +import java.awt.GraphicsEnvironment; + +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JPopupMenu; +import javax.swing.border.EmptyBorder; +import javax.swing.event.DocumentEvent; +import javax.swing.event.UndoableEditEvent; +import javax.swing.event.UndoableEditListener; +import javax.swing.undo.UndoManager; +import javax.swing.JMenuBar; +import javax.swing.JMenu; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.JTextArea; +import javax.swing.JToolBar; +import javax.swing.UIManager; +import javax.swing.WindowConstants; +import javax.swing.JCheckBoxMenuItem; +import javax.swing.JColorChooser; +import javax.swing.JComboBox; +import javax.swing.JDialog; +import javax.swing.JFileChooser; +import javax.swing.AbstractButton; +import javax.swing.Icon; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import java.awt.List; +import java.awt.Checkbox; +import javax.swing.JList; +import java.awt.Choice; +import java.awt.Color; +import java.awt.Component; +import java.awt.Container; +import java.awt.Dimension; +import java.awt.ScrollPane; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.sql.Date; +import java.text.SimpleDateFormat; + +import javax.swing.JScrollPane; +import javax.swing.JTabbedPane; +import javax.swing.KeyStroke; +import javax.swing.SwingConstants; + +import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.event.InputEvent; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.awt.GraphicsEnvironment; + +public class Notebook { + public static void main(String[] args) { + new TimeNote(""); + /*Notebook TimeNote=new Notebook(); + JFrame window=new JFrame ("时间记事本"); //设置窗口名称 + Container con=window.getContentPane(); + con.setBackground(doushalv); //设置记事本的背景颜色为护眼豆沙绿 + window.setBounds(100, 100, 800, 500); //设置窗口在屏幕上的位置和大小 + window.setVisible(true); + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出程序*/ + } + } + class TimeNote extends JFrame implements UndoableEditListener{ + private Icon toolBar_Cut; + private Icon toolBar_Copy; + private Icon toolBar_Paste; + private Icon toolBar_Delete; + private Icon toolBar_Allselect; + private Icon statusBar_status; + private String codestyle; + private JTextArea textArea; + private File file; + private JFileChooser chooser; + private UndoManager manager; + private JPopupMenu popupMenu; + private JLabel label; + private JMenuItem menuItem_cut; + private JMenuItem menuItem_copy; + private JMenuItem menuItem_save; + private JMenuItem menuItem_undo; + private JMenuItem menuItem_redo; + + public TimeNote(String title) { + super(title); //窗体标题 + //window(); + registerListener(); + } + private void window() { + + JTextArea textArea = new JTextArea(); + UndoManager undomg=new UndoManager(); + JScrollPane scrollPane = new JScrollPane(textArea); //文本框里加上滚动条 + scrollPane.setBounds(100, 100, 100, 100); //设置文本框的大小 + + textArea.setFont(new Font("Courier Prime",Font.PLAIN,12)); + textArea.setCaretColor(Color.black); //光标颜色 + textArea.setSelectedTextColor(Color.white); //选中字体颜色 + textArea.setSelectionColor(Color.blue); //选中字体的背景颜色 + textArea.setLineWrap(true); //自动换行 + textArea.setTabSize(4); + textArea.getDocument().addUndoableEditListener(this);//设置文本框可监听 + textArea.getDocument().addUndoableEditListener(undomg); + + JScrollPane pane=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);//带滚动条面板 + + JMenuBar menuBar = new JMenuBar();//初始化菜单 + + Container conpane=this.getContentPane(); // 用Container对象获取当前框架的内容面板 + conpane.add(pane,BorderLayout.CENTER);//将多行文本框加入面板中心 + pane.setBounds(100, 100, 800, 500); //设置窗口在屏幕上的位置和大小 + pane.setVisible(true); + + /*文件菜单*/ + JMenu menu = new JMenu("文件"); + menuBar.add(menu); + + JMenuItem menuItem_save = new JMenuItem("保存"); + menu.add(menuItem_save); + menuItem_save.addActionListener(e->saveFile()); + + JMenuItem menuItem_saveas = new JMenuItem("另存为"); + menu.add(menuItem_saveas); + menuItem_saveas.addActionListener(e->saveFile()); + + JMenuItem menuItem_new = new JMenuItem("新建"); + menu.add(menuItem_new); + menuItem_new.addActionListener(e->newFile()); + + JMenuItem menuItem_open = new JMenuItem("打开"); + menu.add(menuItem_open); + menuItem_open.addActionListener(e->openFile()); + + JMenuItem menuItem_exit = new JMenuItem("退出"); + menu.add(menuItem_exit); + menuItem_exit.addActionListener(e->exit()); + + /*编辑菜单*/ + JMenu menu_edit = new JMenu("编辑"); + menuBar.add(menu_edit); + + JMenuItem menuItem_undo = new JMenuItem("撤销"); + menuItem_undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK)); + menu_edit.add(menuItem_undo); + //undomg.undo(); + + JMenuItem menuItem_cut = new JMenuItem("剪切"); + menuItem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); + menu_edit.add(menuItem_cut); + menuItem_cut.addActionListener(e->textArea.cut()); + + JMenuItem menuItem_paste = new JMenuItem("粘贴"); + menuItem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK)); + menu_edit.add(menuItem_paste); + menuItem_paste.addActionListener(e->textArea.paste()); + + JMenuItem menuItem_copy = new JMenuItem("复制"); + menuItem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); + menu_edit.add(menuItem_copy); + menuItem_copy.addActionListener(e->textArea.copy()); + + JMenuItem menuItem_redo = new JMenuItem("重做"); + menuItem_redo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK)); + menu_edit.add(menuItem_redo); + undomg.redo(); + + JMenuItem menuItem_find = new JMenuItem("查找"); + menuItem_find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK)); + menu_edit.add(menuItem_find); + //menuItem_find.addActionListener(e -> checkBox_setFont()); + + JMenuItem menuItem_excharge = new JMenuItem("替换"); + menuItem_excharge.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK)); + menu_edit.add(menuItem_excharge); + //menuItem_excharge.addActionListener(e -> checkBox_setFont()); + + JMenuItem menuItem_selectall = new JMenuItem("全选"); + menuItem_selectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); + menu_edit.add(menuItem_selectall); + menuItem_selectall.addActionListener(e -> textArea.selectAll()); + + JMenuItem menuItem_time = new JMenuItem("时间"); + menuItem_time.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0)); + menu_edit.add(menuItem_time); + //menuItem_time.addActionListener(this); + + /*格式菜单*/ + JMenu menu_2 = new JMenu("格式"); + menuBar.add(menu_2); + + JMenuItem menuItem_font = new JMenuItem("字体"); + menu_2.add(menuItem_font); + //menuItem_font.addActionListener(this); + + + + JMenuItem menuItem_15 = new JMenuItem("自动换行"); + menu_2.add(menuItem_15); + + JMenu menu_3 = new JMenu("帮助"); + menuBar.add(menu_3); + + JMenuItem menuItem_16 = new JMenuItem("状态栏"); + menu_3.add(menuItem_16); + + /*帮助菜单*/ + JMenu menu_4 = new JMenu("帮助"); + menuBar.add(menu_4); + + JMenuItem menuItem_17 = new JMenuItem("查看帮助"); + menu_4.add(menuItem_17); + + JMenuItem menuItem_18 = new JMenuItem("关于清单记事本"); + menu_4.add(menuItem_18); + + /*右键菜单*/ + JPopupMenu popupMenu=new JPopupMenu(); + popupMenu = new JPopupMenu(); + + JMenuItem popCut = new JMenuItem("剪切",toolBar_Cut); + popupMenu.add(popCut); + + JMenuItem popCopy = new JMenuItem("复制",toolBar_Copy); + popupMenu.add(popCopy); + + JMenuItem popPaste = new JMenuItem("粘贴",toolBar_Paste); + popupMenu.add(popPaste); + + JMenuItem popDelete=new JMenuItem("删除",toolBar_Delete); + popupMenu.add(popDelete); + + JMenuItem popAllselect=new JMenuItem("全选",toolBar_Allselect); + popupMenu.add(popAllselect); + + /*状态栏*/ + JToolBar toolBar=new JToolBar(); + toolBar.setFloatable(false); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + + JLabel label = new JLabel("当前字数:0",statusBar_status,SwingConstants.CENTER); + JLabel labelTime = new JLabel("日期: "+sdf.format(new Date(0, 0, 0))); + JLabel labelCodeStyle = new JLabel("编码: "+codestyle); + toolBar.add(label); + toolBar.addSeparator(new Dimension(180,5)); + toolBar.add(labelTime); + toolBar.addSeparator(new Dimension(180,5)); + toolBar.add(labelCodeStyle); + + this.add(toolBar,BorderLayout.NORTH); // 将工具栏添加到框架中 + this.setJMenuBar(pane); // 将菜单栏添加到框架中 + + this.addWindowListener(new WindowAdapter() { //为框架添加退出保存的监视器 + + public void windowClosing(WindowEvent e) { + String exitMessage = "需要保存输入的文本吗?"; + + if(textArea.getText().equals("")) + + System.exit(0); + + else{ + if(JOptionPane.showConfirmDialog(null, exitMessage,"提示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION){ + saveFile(); + System.exit(0); + }else + System.exit(0); + }}}); + + } + + + private void addWindowListener(WindowAdapter windowAdapter) { + // TODO 自动生成的方法存根 + + } + + + private void newFile(){ // 新建文件触发函数 + if(!(textArea.getText().equals(""))){ + int res = JOptionPane.showConfirmDialog + (null,"当前内容未保存,需要保存吗?", + "info",JOptionPane.YES_NO_OPTION); // 储存选择结果 + if(res==JOptionPane.YES_OPTION){ + saveFile(); + textArea.setText(""); + this.setTitle("时间记事本"); + file = null; + } + else{ + textArea.setText(""); // 取消则清空页面 + this.setTitle("时间记事本"); + file = null; + } + } + } + + + + private void clearAll(){ // 清空当前页面触发函数 + textArea.setText(""); + } // 清空页面触发函数 + + private void openFile(){ // 打开文件触发函数 + try { + chooser = new JFileChooser("/Users/1kasshole/Desktop/");// 设置打开时的默认目录 + int result = chooser.showOpenDialog(null); + if (result == JFileChooser.APPROVE_OPTION) { // 点击打开按钮 + file = chooser.getSelectedFile(); + int length = (int) file.length(); + FileReader reader = new FileReader(file); + char[] ch = new char[length]; + reader.read(ch); // 将文件读进char数组 + reader.close(); + textArea.setText(new String(ch).trim()); // 删除字符串的头尾空白符 + setTitle(file.getName()); // 框架标题设置为文件名 + } + } catch (Exception e) { + JOptionPane.showMessageDialog(null, e); + } + } + + private void saveFile(){ // 文件保存触发函数 + if (file == null) + try { + chooser = new JFileChooser("/Users/1kasshole/Desktop/"); + int result = chooser.showSaveDialog(null); + if (result == JFileChooser.APPROVE_OPTION) { + File selectfile =chooser.getSelectedFile(); // 获得文件 + String end = chooser.getFileFilter().getDescription();// 获得被选中的过滤器中的文件扩展名 + File newFile; + if (selectfile.getAbsolutePath().toUpperCase().endsWith(end.toUpperCase())) {// 如果文件是以选定扩展名结束的,则使用原名 + newFile = selectfile; + } + else { // 否则加上选定的扩展名 + newFile = new File(selectfile.getAbsolutePath()+ end); + } + + try { + if (!newFile.exists()) { + newFile.createNewFile(); + } + FileWriter writer = new FileWriter(newFile); + char[] arry = textArea.getText().toCharArray(); + writer.write(arry); + writer.flush(); + writer.close(); + setTitle(newFile.getName()); + file = newFile; + } catch (IOException e) { + e.printStackTrace(); + }}} + catch (Exception e) { + JOptionPane.showMessageDialog(null, e); + } + + else + try { + FileWriter writer = new FileWriter(file); + char[] arry = textArea.getText().toCharArray(); + writer.write(arry); + writer.flush(); + writer.close(); + } catch (Exception e) { + JOptionPane.showMessageDialog(null, e); + } + } + + private void undo(){ // 撤销触发函数 + if (manager.canUndo()){ + manager.undo(); + } + } + + private void exit(){ // 退出程序触发函数 + newFile(); + System.exit(0); + } + + private void setTextFont(){ // 选择字体触发函数 + try{ + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();// 获取系统字体 + JList fontNames = new JList<>(ge.getAvailableFontFamilyNames()); + int response = JOptionPane.showConfirmDialog(null,new JScrollPane(fontNames), "请选择字体 ( 默认: Courier Prime )",JOptionPane.OK_CANCEL_OPTION); + Object selectedFont = fontNames.getSelectedValue(); + if (response == JOptionPane.OK_OPTION && selectedFont != null) + textArea.setFont(new Font((String) fontNames.getSelectedValue(), Font.PLAIN,20)); + } + catch (Exception e){ + e.printStackTrace(); + } + } + + private void setTextColor(){ // 字体颜色选择触发函数 + 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 changeTextLengthStatus(){ // 文本监听 + String content = textArea.getText().trim(); + label.setText("当前字数:"+content.length()); + } + + private void isItemsAvalible(){ // 监视文本区并设置各功能项是否可用 + String content = textArea.getText(); + if(content.equals("")){ + menuItem_cut.setEnabled(false); + menuItem_copy.setEnabled(false); + menuItem_save.setEnabled(false); + } + else{ + menuItem_save.setEnabled(true); + menuItem_cut.setEnabled(true); + menuItem_copy.setEnabled(true); + menuItem_undo.setEnabled(true); + menuItem_redo.setEnabled(true); + } + } + + + + + + + + private Object checkBox_setFont() { + // TODO 自动生成的方法存根 + return null; + } + + + + + + + + + + private void registerListener() { + textArea.addMouseListener(new MouseAdapter() { //加入鼠标监听器 + public void mousePressed(MouseEvent e) { + maybeShowPopup(e); + } + public void mouseReleased(MouseEvent e) { + maybeShowPopup(e); + } + }); + } + + + + + private void setJMenuBar(JScrollPane pane) { + // TODO 自动生成的方法存根 + + } + + + + private void add(JToolBar toolBar, String north) { + // TODO 自动生成的方法存根 + + } + + + + /*private void setDefaultCloseOperation(int exitOnClose) { + // TODO 自动生成的方法存根 + + } + + + + private Container getContentPane() { + // TODO 自动生成的方法存根 + return null; + }*/ + + + + private void setContentPane(JPanel contentPane) { + + } + public final Color doushalv = new Color(199,237,204);//新建颜色为护眼豆沙绿 + + @Override + public void undoableEditHappened(UndoableEditEvent e) { + // TODO 自动生成的方法存根 + + } + + } + + + + + -- Gitee From 5a2f678a045dfb099c6732a113ef62f1df4f2fc5 Mon Sep 17 00:00:00 2001 From: lenovo Date: Mon, 7 Jun 2021 23:02:46 +0800 Subject: [PATCH 02/19] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=EF=BC=88?= =?UTF-8?q?=E8=AE=B0=E4=BA=8B=E6=9C=AC=E7=9A=84=E5=9F=BA=E6=9C=AC=E6=A1=86?= =?UTF-8?q?=E6=9E=B6=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Notebook.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Notebook.java b/src/Notebook.java index 3ac0e10..30df110 100644 --- a/src/Notebook.java +++ b/src/Notebook.java @@ -99,7 +99,7 @@ public class Notebook { public TimeNote(String title) { super(title); //窗体标题 //window(); - registerListener(); + //registerListener(); } private void window() { -- Gitee From 1ff69141358158bc2c5285f0a0d49fa0fe9068bd Mon Sep 17 00:00:00 2001 From: lenovo Date: Wed, 9 Jun 2021 20:21:25 +0800 Subject: [PATCH 03/19] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/TimeNoteBook/Notebook.java | 792 +++++++++++++++++++++++++++++++++ 1 file changed, 792 insertions(+) create mode 100644 src/TimeNoteBook/Notebook.java diff --git a/src/TimeNoteBook/Notebook.java b/src/TimeNoteBook/Notebook.java new file mode 100644 index 0000000..55f27e2 --- /dev/null +++ b/src/TimeNoteBook/Notebook.java @@ -0,0 +1,792 @@ +package TimeNoteBook; +import java.awt.*; +import javax.swing.*; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.event.UndoableEditEvent; +import javax.swing.event.UndoableEditListener; +import javax.swing.undo.UndoManager; +import java.io.*; +import java.sql.Date; +import java.text.SimpleDateFormat; +import javax.swing.*; +import java.awt.event.*; +import java.beans.EventHandler; + + +public class Notebook extends JFrame { + public static void main(String[] args) { + new Notebook(); + } + public final static Color doushalv = new Color(199,237,204);//新建颜色为护眼豆沙绿 + private JFrame notebook; + +public Notebook() { + JTextArea textArea = new JTextArea(); //创建一个多行文本框 +// UndoManager undomg=new UndoManager(); //创建撤销的管理 +// JScrollPane scrollPane = new JScrollPane(textArea); //文本框里加上滚动条 +// scrollPane.setBounds(800, 800, 300, 300); //设置文本框的大小 +// textArea.setFont(new Font("Courier Prime",Font.PLAIN,12)); +// textArea.setCaretColor(Color.black); //光标颜色 +// textArea.setSelectedTextColor(Color.white); //选中字体颜色 +// textArea.setSelectionColor(Color.blue); //选中字体的背景颜色 +// textArea.setLineWrap(true); //自动换行 +// textArea.setTabSize(4); + textArea.setVisible(true); + this.add(textArea); + JScrollPane pane=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);//带滚动条面板 + this.setSize(800, 600); + this.setLocation(100,100); + this.setVisible(true); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setBackground(doushalv);//设置记事本的背景颜色为护眼豆沙绿 +}} +//// JMenuBar menuBar = new JMenuBar();//初始化菜单 +//// +//// /*文件菜单*/ +//// JMenu menu = new JMenu("文件"); +//// menuBar.add(menu); +//// +//// JMenuItem menuItem_save = new JMenuItem("保存"); //建立保存按钮 +//// menu.add(menuItem_save); +//// +//// +//// JMenuItem menuItem_saveas = new JMenuItem("另存为"); +//// menu.add(menuItem_saveas); +//// +//// +//// JMenuItem menuItem_new = new JMenuItem("新建"); +//// menu.add(menuItem_new); +//// +//// JMenuItem menuItem_open = new JMenuItem("打开"); +//// menu.add(menuItem_open); +//// +//// +//// JMenuItem menuItem_exit = new JMenuItem("退出"); +//// menu.add(menuItem_exit); +//// +//// /*编辑菜单*/ +//// JMenu menu_edit = new JMenu("编辑"); +//// menuBar.add(menu_edit); +//// +//// JMenuItem menuItem_undo = new JMenuItem("撤销"); +//// menuItem_undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_undo); +//// +//// +//// JMenuItem menuItem_cut = new JMenuItem("剪切"); +//// menuItem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_cut); +//// +//// +//// JMenuItem menuItem_paste = new JMenuItem("粘贴"); +//// menuItem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_paste); +//// +//// JMenuItem menuItem_copy = new JMenuItem("复制"); +//// menuItem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_copy); +//// +//// JMenuItem menuItem_redo = new JMenuItem("重做"); +//// menuItem_redo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_redo); +//// //undomg.redo(); +//// +//// JMenuItem menuItem_find = new JMenuItem("查找"); +//// menuItem_find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_find); +//// //menuItem_find.addActionListener(this); +//// +//// JMenuItem menuItem_excharge = new JMenuItem("替换"); +//// menuItem_excharge.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_excharge); +//// //menuItem_excharge.addActionListener(this); +//// +//// JMenuItem menuItem_selectall = new JMenuItem("全选"); +//// menuItem_selectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_selectall); +//// +//// JMenuItem menuItem_time = new JMenuItem("时间"); +//// menuItem_time.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0)); +//// menu_edit.add(menuItem_time); +//// +//// +//// /*格式菜单*/ +//// JMenu menu_2 = new JMenu("格式"); +//// menuBar.add(menu_2); +//// +//// JMenuItem menuItem_font = new JMenuItem("字体"); +//// menu_2.add(menuItem_font); +//// //menuItem_font.addActionListener(this); +//// +//// JMenuItem menuItem_linewrap = new JMenuItem("自动换行"); +//// menu_2.add(menuItem_linewrap); +//// menuItem_linewrap.setEnabled(true); +//// //menuItem_linewrap.addActionListener(this); +//// +//// +//// JMenu menu_3 = new JMenu("查看"); +//// menuBar.add(menu_3); +//// +//// JMenuItem menuItem_viewmenu = new JMenuItem("状态栏"); +//// menu_3.add(menuItem_viewmenu); +//// menuItem_viewmenu.setEnabled(true); +//// //menuItem_viewmenu.addActionListener(this); +//// +//// +//// /*帮助菜单*/ +//// JMenu menu_4 = new JMenu("帮助"); +//// menuBar.add(menu_4); +//// +//// JMenuItem menuItem_17 = new JMenuItem("查看帮助"); +//// menu_4.add(menuItem_17); +//// //menuItem_17.addActionListener(this); +//// +//// JMenuItem menuItem_18 = new JMenuItem("关于清单记事本"); +//// menu_4.add(menuItem_18); +//// //menuItem_18.addActionListener(this); +//// +//// /*右键菜单*/ +//// JPopupMenu popupMenu=new JPopupMenu(); +//// popupMenu = new JPopupMenu(); +//// +//// JMenuItem popCut = new JMenuItem("剪切"); +//// popupMenu.add(popCut); +//// +//// JMenuItem popCopy = new JMenuItem("复制"); +//// popupMenu.add(popCopy); +//// +//// JMenuItem popPaste = new JMenuItem("粘贴"); +//// popupMenu.add(popPaste); +//// +//// JMenuItem popDelete=new JMenuItem("删除"); +//// popupMenu.add(popDelete); +//// +//// JMenuItem popAllselect=new JMenuItem("全选"); +//// popupMenu.add(popAllselect); +//// +//// +//// /*状态栏*/ +//// JToolBar toolBar=new JToolBar(); +//// toolBar.setFloatable(false); +//// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); +//// +//// JLabel label = new JLabel("当前字数:0",statusBar_status,SwingConstants.CENTER); +//// JLabel labelTime = new JLabel("日期: "+sdf.format(new Date(0, 0, 0))); +//// JLabel labelCodeStyle = new JLabel("编码: "+codestyle); +//// toolBar.add(label); +//// toolBar.addSeparator(new Dimension(180,5)); +//// toolBar.add(labelTime); +//// toolBar.addSeparator(new Dimension(180,5)); +//// toolBar.add(labelCodeStyle); +//// +//// con.add(textArea); +//// con.add(menu); +//// con.add(toolBar); +//// con.add(menuBar); +//// con.add(label); +//// con.add(popupMenu); +// +// } +// +// +// private static Icon statusBar_status; +// private static String codestyle="ANSI"; +// private JTextArea textArea; +// private File file; +// private JFileChooser chooser; +// private UndoManager manager; +// private JPopupMenu popupMenu; +// private JLabel label; +// private JMenuItem menuItem_cut; +// private JMenuItem menuItem_copy; +// private JMenuItem menuItem_save; +// private JMenuItem menuItem_undo; +// private JMenuItem menuItem_redo; +// private JMenuItem menuItem_saveas; +// private JMenuItem menuItem_new; +// private JMenuItem menuItem_exit; +// private JMenuItem menuItem_open; +// private JMenuItem menuItem_paste; +// private JMenuItem menuItem_selectall; +// private JScrollPane pane; +// private JFileChooser menuItem_time; +// private Object menuItem_find; +// +// private void window() +// { +//// +//// JTextArea textArea = new JTextArea(); //创建一个多行文本框 +//// UndoManager undomg=new UndoManager(); //创建撤销的管理 +//// JScrollPane scrollPane = new JScrollPane(textArea); //文本框里加上滚动条 +//// scrollPane.setBounds(300, 200, 100, 100); //设置文本框的大小 +//// textArea.setFont(new Font("Courier Prime",Font.PLAIN,12)); +//// textArea.setCaretColor(Color.black); //光标颜色 +//// textArea.setSelectedTextColor(Color.white); //选中字体颜色 +//// textArea.setSelectionColor(Color.blue); //选中字体的背景颜色 +//// textArea.setLineWrap(true); //自动换行 +//// textArea.setTabSize(4); +//// textArea.getDocument().addUndoableEditListener(this);//设置文本框可监听 +//// textArea.getDocument().addUndoableEditListener(undomg);//设置文本撤销 +//// +//// JScrollPane pane=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);//带滚动条面板 +//// +//// JMenuBar menuBar = new JMenuBar();//初始化菜单 +//// +//// Container conpane=this.getContentPane(); // 用Container对象获取当前框架的内容面板 +//// conpane.add(pane,BorderLayout.CENTER); //将多行文本框加入面板中心 +//// pane.setBounds(100, 100, 100, 100); //设置窗口在屏幕上的位置和大小 +//// pane.setVisible(true); +//// +//// /*文件菜单*/ +//// JMenu menu = new JMenu("文件"); +//// menuBar.add(menu); +//// +//// JMenuItem menuItem_save = new JMenuItem("保存"); //建立保存按钮 +//// menu.add(menuItem_save); +//// +//// +//// JMenuItem menuItem_saveas = new JMenuItem("另存为"); +//// menu.add(menuItem_saveas); +//// +//// +//// JMenuItem menuItem_new = new JMenuItem("新建"); +//// menu.add(menuItem_new); +//// +//// JMenuItem menuItem_open = new JMenuItem("打开"); +//// menu.add(menuItem_open); +//// +//// +//// JMenuItem menuItem_exit = new JMenuItem("退出"); +//// menu.add(menuItem_exit); +//// +//// /*编辑菜单*/ +//// JMenu menu_edit = new JMenu("编辑"); +//// menuBar.add(menu_edit); +//// +//// JMenuItem menuItem_undo = new JMenuItem("撤销"); +//// menuItem_undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_undo); +//// +//// +//// JMenuItem menuItem_cut = new JMenuItem("剪切"); +//// menuItem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_cut); +//// +//// +//// JMenuItem menuItem_paste = new JMenuItem("粘贴"); +//// menuItem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_paste); +//// +//// JMenuItem menuItem_copy = new JMenuItem("复制"); +//// menuItem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_copy); +//// +//// JMenuItem menuItem_redo = new JMenuItem("重做"); +//// menuItem_redo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_redo); +//// //undomg.redo(); +//// +//// JMenuItem menuItem_find = new JMenuItem("查找"); +//// menuItem_find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_find); +//// menuItem_find.addActionListener(this); +//// +//// JMenuItem menuItem_excharge = new JMenuItem("替换"); +//// menuItem_excharge.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_excharge); +//// menuItem_excharge.addActionListener(this); +//// +//// JMenuItem menuItem_selectall = new JMenuItem("全选"); +//// menuItem_selectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); +//// menu_edit.add(menuItem_selectall); +//// +//// JMenuItem menuItem_time = new JMenuItem("时间"); +//// menuItem_time.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0)); +//// menu_edit.add(menuItem_time); +//// +//// +//// /*格式菜单*/ +//// JMenu menu_2 = new JMenu("格式"); +//// menuBar.add(menu_2); +//// +//// JMenuItem menuItem_font = new JMenuItem("字体"); +//// menu_2.add(menuItem_font); +//// menuItem_font.addActionListener(this); +//// +//// JMenuItem menuItem_linewrap = new JMenuItem("自动换行"); +//// menu_2.add(menuItem_linewrap); +//// menuItem_linewrap.setEnabled(true); +//// menuItem_linewrap.addActionListener(this); +//// +//// +//// JMenu menu_3 = new JMenu("查看"); +//// menuBar.add(menu_3); +//// +//// JMenuItem menuItem_viewmenu = new JMenuItem("状态栏"); +//// menu_3.add(menuItem_viewmenu); +//// menuItem_viewmenu.setEnabled(true); +//// menuItem_viewmenu.addActionListener(this); +//// +//// +//// /*帮助菜单*/ +//// JMenu menu_4 = new JMenu("帮助"); +//// menuBar.add(menu_4); +//// +//// JMenuItem menuItem_17 = new JMenuItem("查看帮助"); +//// menu_4.add(menuItem_17); +//// menuItem_17.addActionListener(this); +//// +//// JMenuItem menuItem_18 = new JMenuItem("关于清单记事本"); +//// menu_4.add(menuItem_18); +//// menuItem_18.addActionListener(this); +//// +//// /*右键菜单*/ +//// JPopupMenu popupMenu=new JPopupMenu(); +//// popupMenu = new JPopupMenu(); +//// +//// JMenuItem popCut = new JMenuItem("剪切"); +//// popupMenu.add(popCut); +//// +//// JMenuItem popCopy = new JMenuItem("复制"); +//// popupMenu.add(popCopy); +//// +//// JMenuItem popPaste = new JMenuItem("粘贴"); +//// popupMenu.add(popPaste); +//// +//// JMenuItem popDelete=new JMenuItem("删除"); +//// popupMenu.add(popDelete); +//// +//// JMenuItem popAllselect=new JMenuItem("全选"); +//// popupMenu.add(popAllselect); +//// +//// +//// /*状态栏*/ +//// JToolBar toolBar=new JToolBar(); +//// toolBar.setFloatable(false); +//// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); +//// +//// JLabel label = new JLabel("当前字数:0",statusBar_status,SwingConstants.CENTER); +//// JLabel labelTime = new JLabel("日期: "+sdf.format(new Date(0, 0, 0))); +//// JLabel labelCodeStyle = new JLabel("编码: "+codestyle); +//// toolBar.add(label); +//// toolBar.addSeparator(new Dimension(180,5)); +//// toolBar.add(labelTime); +//// toolBar.addSeparator(new Dimension(180,5)); +//// toolBar.add(labelCodeStyle); +// } +// +// +// +// +// private void registerListener() { //监听器注册 +// this.setJMenuBar(pane); // 将菜单栏添加到框架中 +// +// textArea.addMouseListener(new MouseAdapter() { //加入鼠标监听器 +// +// public void mousePressed(MouseEvent e) { +// if(e.isPopupTrigger()){//返回此鼠标事件是否为该平台的弹出菜单触发事件 +// popupMenu.show(e.getComponent(),e.getX(),e.getY());//在组件调用者的坐标空间中的位置 X、Y 显示弹出菜单 +// } +// checkMenuItemEnabled();//设置剪切,复制,粘帖,删除等功能的可用性 +// textArea.requestFocus();//编辑区获取焦点 +// } +// private void checkMenuItemEnabled() { +// // TODO 自动生成的方法存根 +// +// } +// public void mouseReleased(MouseEvent e) +// { if(e.isPopupTrigger())//返回此鼠标事件是否为该平台的弹出菜单触发事件 +// { popupMenu.show(e.getComponent(),e.getX(),e.getY());//在组件调用者的坐标空间中的位置 X、Y 显示弹出菜单 +// } +// checkMenuItemEnabled();//设置剪切,复制,粘帖,删除等功能的可用性 +// textArea.requestFocus();//编辑区获取焦点 +// } +// }); +// this.setLocation(100,100); +// this.setSize(650,550); +// this.setVisible(true); +// +// +// +// 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(); // 一旦文本有改变就设置各按钮的可用性 +// } +// }); +// menuItem_save.addActionListener(e->saveFile()); //注册保存文件的监听器 +// menuItem_saveas.addActionListener(e->saveFile()); +// menuItem_new.addActionListener(e->newFile()); +// menuItem_open.addActionListener(e->openFile()); +// menuItem_exit.addActionListener(e->exit()); +// menuItem_cut.addActionListener(e->textArea.cut()); +// menuItem_paste.addActionListener(e->textArea.paste()); +// menuItem_copy.addActionListener(e->textArea.copy()); +// menuItem_selectall.addActionListener(e -> textArea.selectAll()); +// menuItem_time.addActionListener(e->onDateClick(e)); +// +// this.addWindowListener(new WindowAdapter() { //为框架添加退出保存的监视器 +// public void windowClosing(WindowEvent e) { +// String exitMessage = "需要保存输入的文本吗?"; +// if(textArea.getText().equals("")) +// System.exit(0); +// else{ +// if(JOptionPane.showConfirmDialog(null, exitMessage,"提示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION){ +// saveFile(); +// System.exit(0); +// }else +// System.exit(0); +// }}}); +// } +// public void find() { +// final JDialog findDialog=new JDialog(this,"查找",false); +// Container con=findDialog.getContentPane();//返回此对话框的contentPane对象 +// con.setLayout(new FlowLayout(FlowLayout.LEFT)); +// JLabel findContentLabel=new JLabel("查找内容:"); +// final JTextField findText=new JTextField(15); +// JButton findNextButton=new JButton("查找下一个:"); +// final JCheckBox matchCheckBox=new JCheckBox("区分大小写"); +// ButtonGroup bGroup=new ButtonGroup(); +// final JRadioButton upButton=new JRadioButton("向上"); +// final JRadioButton downButton=new JRadioButton("向下"); +// downButton.setSelected(true); +// bGroup.add(upButton); +// bGroup.add(downButton); +// JButton cancel=new JButton("取消"); //取消按钮事件处理 +// cancel.addActionListener(new ActionListener() +// { public void actionPerformed(ActionEvent e) +// { findDialog.dispose(); +// } +// }); //"查找下一个"按钮监听 +// findNextButton.addActionListener(new ActionListener() +// { public void actionPerformed(ActionEvent e) +// { //"区分大小写(C)"的JCheckBox是否被选中 +// int k=0,m=0; +// final String str1,str2,str3,str4,strA,strB; +// str1=textArea.getText(); +// str2=findText.getText(); +// str3=str1.toUpperCase(); +// str4=str2.toUpperCase(); +// if(matchCheckBox.isSelected())//区分大小写 +// { strA=str1; +// strB=str2; +// } +// else//不区分大小写,此时把所选内容全部化成大写(或小写),以便于查找 +// { strA=str3; +// strB=str4; +// } +// if(upButton.isSelected()) +// { +// if(textArea.getSelectedText()==null) +// k=strA.lastIndexOf(strB,textArea.getCaretPosition()-1); +// else +// k=strA.lastIndexOf(strB, textArea.getCaretPosition()-findText.getText().length()-1); +// if(k>-1) +// { +// textArea.setCaretPosition(k); +// textArea.select(k,k+strB.length()); +// } +// else +// { JOptionPane.showMessageDialog(null,"找不到您查找的内容!请重新输入!","查找",JOptionPane.INFORMATION_MESSAGE); +// } +// } +// else if(downButton.isSelected()) +// { if(textArea.getSelectedText()==null) +// k=strA.indexOf(strB,textArea.getCaretPosition()+1); +// else +// k=strA.indexOf(strB, textArea.getCaretPosition()-findText.getText().length()+1); +// if(k>-1) +// { +// textArea.setCaretPosition(k); +// textArea.select(k,k+strB.length()); +// } +// else +// { JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE); +// } +// } +// } +// }); +// +// JPanel panel1=new JPanel();//创建"查找"对话框的界面 +// JPanel panel2=new JPanel(); +// JPanel panel3=new JPanel(); +// JPanel directionPanel=new JPanel(); +// directionPanel.setBorder(BorderFactory.createTitledBorder("方向"));//设置directionPanel组件的边框; +// directionPanel.add(upButton); +// directionPanel.add(downButton); +// panel1.setLayout(new GridLayout(2,1)); +// panel1.add(findNextButton); +// panel1.add(cancel); +// panel2.add(findContentLabel); +// panel2.add(findText); +// panel2.add(panel1); +// panel3.add(matchCheckBox); +// panel3.add(directionPanel); +// con.add(panel2); +// con.add(panel3); +// findDialog.setSize(410,180); +// findDialog.setResizable(false);//不可调整大小 +// findDialog.setLocation(230,280); +// findDialog.setVisible(true); +// } +// +// +// +// private void onDateClick(ActionEvent event) { // 格式化当前的日期 +// Date nowTime = new Date(0); +// this.textArea.requestFocus(); +// SimpleDateFormat currentDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); +// this.textArea.insert(currentDateTime.format(nowTime), this.textArea.getCaretPosition()); +// } +// +// +// private void newFile(){ // 新建文件触发函数 +// if(!(textArea.getText().equals(""))){ +// int res = JOptionPane.showConfirmDialog +// (null,"当前内容未保存,需要保存吗?", +// "info",JOptionPane.YES_NO_OPTION); // 储存选择结果 +// if(res==JOptionPane.YES_OPTION){ +// saveFile(); +// textArea.setText(""); +// this.setTitle("时间记事本"); +// file = null; +// } +// else{ +// textArea.setText(""); // 取消则清空页面 +// this.setTitle("时间记事本"); +// file = null; +// } +// } +// } +// +// +// +// private void clearAll(){ // 清空当前页面触发函数 +// textArea.setText(""); +// } // 清空页面触发函数 +// +// private void openFile(){ // 打开文件触发函数 +// try { +// chooser = new JFileChooser("/Users/1kasshole/Desktop/");// 设置打开时的默认目录 +// int result = chooser.showOpenDialog(null); +// if (result == JFileChooser.APPROVE_OPTION) { // 点击打开按钮 +// file = chooser.getSelectedFile(); +// int length = (int) file.length(); +// FileReader reader = new FileReader(file); +// char[] ch = new char[length]; +// reader.read(ch); // 将文件读进char数组 +// reader.close(); +// textArea.setText(new String(ch).trim()); // 删除字符串的头尾空白符 +// setTitle(file.getName()); // 框架标题设置为文件名 +// } +// } catch (Exception e) { +// JOptionPane.showMessageDialog(null, e); +// } +// } +// +// private void saveFile(){ // 文件保存触发函数 +// if (file == null) +// try { +// chooser = new JFileChooser("/Users/1kasshole/Desktop/"); +// int result = chooser.showSaveDialog(null); +// if (result == JFileChooser.APPROVE_OPTION) { +// File selectfile =chooser.getSelectedFile(); // 获得文件 +// String end = chooser.getFileFilter().getDescription();// 获得被选中的过滤器中的文件扩展名 +// File newFile; +// if (selectfile.getAbsolutePath().toUpperCase().endsWith(end.toUpperCase())) {// 如果文件是以选定扩展名结束的,则使用原名 +// newFile = selectfile; +// } +// else { // 否则加上选定的扩展名 +// newFile = new File(selectfile.getAbsolutePath()+ end); +// } +// +// try { +// if (!newFile.exists()) { +// newFile.createNewFile(); +// } +// FileWriter writer = new FileWriter(newFile); +// char[] arry = textArea.getText().toCharArray(); +// writer.write(arry); +// writer.flush(); +// writer.close(); +// setTitle(newFile.getName()); +// file = newFile; +// } catch (IOException e) { +// e.printStackTrace(); +// }}} +// catch (Exception e) { +// JOptionPane.showMessageDialog(null, e); +// } +// +// else +// try { +// FileWriter writer = new FileWriter(file); +// char[] arry = textArea.getText().toCharArray(); +// writer.write(arry); +// writer.flush(); +// writer.close(); +// } catch (Exception e) { +// JOptionPane.showMessageDialog(null, e); +// } +// } +// +// private void undo(){ // 撤销触发函数 +// if (manager.canUndo()){ +// manager.undo(); +// } +// } +// +// private void redo(){ // 重做触发函数 +// if (manager.canRedo()){ +// manager.redo(); +// } +// } +// +// private void exit(){ // 退出程序触发函数 +// newFile(); +// System.exit(0); +// } +// +// private void setTextFont(){ // 选择字体触发函数 +// try{ +// GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();// 获取系统字体 +// JList fontNames = new JList<>(ge.getAvailableFontFamilyNames()); +// int response = JOptionPane.showConfirmDialog(null,new JScrollPane(fontNames), "请选择字体 ( 默认: Courier Prime )",JOptionPane.OK_CANCEL_OPTION); +// Object selectedFont = fontNames.getSelectedValue(); +// if (response == JOptionPane.OK_OPTION && selectedFont != null) +// textArea.setFont(new Font((String) fontNames.getSelectedValue(), Font.PLAIN,20)); +// } +// catch (Exception e){ +// e.printStackTrace(); +// } +// } +// +// private void setTextColor(){ // 字体颜色选择触发函数 +// 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 changeTextLengthStatus(){ // 文本监听 +// String content = textArea.getText().trim(); +// label.setText("当前字数:"+content.length()); +// } +// +// private void isItemsAvalible(){ // 监视文本区并设置各功能项是否可用 +// String content = textArea.getText(); +// if(content.equals("")){ +// menuItem_cut.setEnabled(false); +// menuItem_copy.setEnabled(false); +// menuItem_save.setEnabled(false); +// menuItem_undo.setEnabled(false); +// menuItem_redo.setEnabled(false); +// menuItem_open.setEnabled(false); +// menuItem_paste.setEnabled(false); +// menuItem_selectall.setEnabled(false); +// menuItem_exit.setEnabled(false); +// menuItem_saveas.setEnabled(false); +// } +// else{ +// menuItem_save.setEnabled(true); +// menuItem_cut.setEnabled(true); +// menuItem_copy.setEnabled(true); +// menuItem_undo.setEnabled(true); +// menuItem_redo.setEnabled(true); +// menuItem_open.setEnabled(true); +// menuItem_paste.setEnabled(true); +// menuItem_selectall.setEnabled(true); +// menuItem_exit.setEnabled(true); +// menuItem_saveas.setEnabled(true); +// } +// } +// +// +// +// +// +// +// +// private void setJMenuBar(JScrollPane pane) { +// // TODO 自动生成的方法存根 +// +// } +// +// +// +// private void add(JToolBar toolBar, String north) { +// // TODO 自动生成的方法存根 +// +// } +// +// +// +// +// private void setContentPane(JPanel contentPane) { +// +// } +// +// +// +// @Override +// public void insertUpdate(DocumentEvent e) { +// // TODO 自动生成的方法存根 +// +// } +// +// @Override +// public void removeUpdate(DocumentEvent e) { +// // TODO 自动生成的方法存根 +// +// } +// +// @Override +// public void changedUpdate(DocumentEvent e) { +// // TODO 自动生成的方法存根 +// +// } +// +// @Override +// public void actionPerformed(ActionEvent e) { +// // TODO 自动生成的方法存根 +// +// } +// +// +// +// +// @Override +// public void undoableEditHappened(UndoableEditEvent e) { +// // TODO 自动生成的方法存根 +// +// } +// +// +// } +// +// +// +// + + -- Gitee From e0207aacc84eb99ee905d408d1ba5c814e24c5c1 Mon Sep 17 00:00:00 2001 From: lenovo Date: Thu, 10 Jun 2021 07:45:00 +0800 Subject: [PATCH 04/19] =?UTF-8?q?=E8=AE=B0=E4=BA=8B=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/TimeNoteBook/Notebook.java | 1178 +++++++++++--------------------- 1 file changed, 405 insertions(+), 773 deletions(-) diff --git a/src/TimeNoteBook/Notebook.java b/src/TimeNoteBook/Notebook.java index 55f27e2..4f0d3ba 100644 --- a/src/TimeNoteBook/Notebook.java +++ b/src/TimeNoteBook/Notebook.java @@ -1,792 +1,424 @@ package TimeNoteBook; + import java.awt.*; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.Transferable; + import javax.swing.*; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; +import javax.swing.event.MenuEvent; +import javax.swing.event.MenuListener; import javax.swing.event.UndoableEditEvent; import javax.swing.event.UndoableEditListener; import javax.swing.undo.UndoManager; import java.io.*; import java.sql.Date; import java.text.SimpleDateFormat; +import java.util.Enumeration; + import javax.swing.*; import java.awt.event.*; import java.beans.EventHandler; - -public class Notebook extends JFrame { - public static void main(String[] args) { - new Notebook(); +public class Notebook extends JFrame implements DocumentListener,ActionListener { + private static Icon statusBar_status; + private static String codestyle = "ANSI"; + /*菜单*/ + private JMenu menu_file; + private JMenu menu_edit; + private JMenu menu_format; + private JMenu menu_help; + private JMenu menu_view; + /*右键弹出菜单*/ + private JPopupMenu popupMenu; + private JMenuItem popupmenu_undo; + private JMenuItem popupmenu_cut; + private JMenuItem popupmenu_copy; + private JMenuItem popupmenu_paste; + private JMenuItem popupmenu_delete; + private JMenuItem popupmenu_selectall; + /*文件菜单*/ + private JMenuItem menuItem_new; + private JMenuItem menuItem_open; + private JMenuItem menuItem_save; + private JMenuItem menuItem_saveas; + private JMenuItem menuItem_exit; + /*编辑菜单*/ + private JMenuItem menuItem_undo; + private JMenuItem menuItem_time; + private JMenuItem menuItem_find; + private JMenuItem menuItem_replace; + private JMenuItem menuItem_paste; + private JMenuItem menuItem_selectall; + private JMenuItem menuItem_cut; + private JMenuItem menuItem_copy; + private JMenuItem menuItem_delete; + /*格式菜单*/ + private JCheckBoxMenuItem menuItem_linewrap; + private JMenuItem menuItem_font; + /*查看的菜单*/ + private JCheckBoxMenuItem menuItem_status; + /*帮助的菜单*/ + private JMenuItem menuItem_HelpTopics; + private JMenuItem menuItem_AboutNotepad; + /*文本编辑区域*/ + private JTextArea textArea; + /*状态栏*/ + private JToolBar statusLabel; + + private JMenuBar menuBar; + + Toolkit toolkit=Toolkit.getDefaultToolkit(); + Clipboard clipBoard=toolkit.getSystemClipboard();//创建撤销操作管理器(与撤销操作有关) + protected UndoManager undo=new UndoManager(); + protected UndoableEditListener undoHandler=new UndoHandler();//其他变量 + private String oldValue;//存放编辑区原来的内容,用于比较文本是否有改动 + boolean isNewFile=true;//是否新文件(未保存过的) + File currentFile; + //当前文件名 + + public Notebook(){ + super("时间记事本"); + Font font=new Font("Courier Prime", Font.PLAIN, 12); //改变系统默认字体 + Enumeration keys = UIManager.getDefaults().keys(); + while (keys.hasMoreElements()) { + Object key = keys.nextElement(); + Object value = UIManager.get(key); + if (value instanceof javax.swing.plaf.FontUIResource) { + UIManager.put(key,font); + } } - public final static Color doushalv = new Color(199,237,204);//新建颜色为护眼豆沙绿 - private JFrame notebook; -public Notebook() { - JTextArea textArea = new JTextArea(); //创建一个多行文本框 -// UndoManager undomg=new UndoManager(); //创建撤销的管理 -// JScrollPane scrollPane = new JScrollPane(textArea); //文本框里加上滚动条 -// scrollPane.setBounds(800, 800, 300, 300); //设置文本框的大小 -// textArea.setFont(new Font("Courier Prime",Font.PLAIN,12)); -// textArea.setCaretColor(Color.black); //光标颜色 -// textArea.setSelectedTextColor(Color.white); //选中字体颜色 -// textArea.setSelectionColor(Color.blue); //选中字体的背景颜色 -// textArea.setLineWrap(true); //自动换行 -// textArea.setTabSize(4); - textArea.setVisible(true); - this.add(textArea); - JScrollPane pane=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);//带滚动条面板 - this.setSize(800, 600); - this.setLocation(100,100); - this.setVisible(true); - this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - this.setBackground(doushalv);//设置记事本的背景颜色为护眼豆沙绿 -}} -//// JMenuBar menuBar = new JMenuBar();//初始化菜单 -//// -//// /*文件菜单*/ -//// JMenu menu = new JMenu("文件"); -//// menuBar.add(menu); -//// -//// JMenuItem menuItem_save = new JMenuItem("保存"); //建立保存按钮 -//// menu.add(menuItem_save); -//// -//// -//// JMenuItem menuItem_saveas = new JMenuItem("另存为"); -//// menu.add(menuItem_saveas); -//// -//// -//// JMenuItem menuItem_new = new JMenuItem("新建"); -//// menu.add(menuItem_new); -//// -//// JMenuItem menuItem_open = new JMenuItem("打开"); -//// menu.add(menuItem_open); -//// -//// -//// JMenuItem menuItem_exit = new JMenuItem("退出"); -//// menu.add(menuItem_exit); -//// -//// /*编辑菜单*/ -//// JMenu menu_edit = new JMenu("编辑"); -//// menuBar.add(menu_edit); -//// -//// JMenuItem menuItem_undo = new JMenuItem("撤销"); -//// menuItem_undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_undo); -//// -//// -//// JMenuItem menuItem_cut = new JMenuItem("剪切"); -//// menuItem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_cut); -//// -//// -//// JMenuItem menuItem_paste = new JMenuItem("粘贴"); -//// menuItem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_paste); -//// -//// JMenuItem menuItem_copy = new JMenuItem("复制"); -//// menuItem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_copy); -//// -//// JMenuItem menuItem_redo = new JMenuItem("重做"); -//// menuItem_redo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_redo); -//// //undomg.redo(); -//// -//// JMenuItem menuItem_find = new JMenuItem("查找"); -//// menuItem_find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_find); -//// //menuItem_find.addActionListener(this); -//// -//// JMenuItem menuItem_excharge = new JMenuItem("替换"); -//// menuItem_excharge.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_excharge); -//// //menuItem_excharge.addActionListener(this); -//// -//// JMenuItem menuItem_selectall = new JMenuItem("全选"); -//// menuItem_selectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_selectall); -//// -//// JMenuItem menuItem_time = new JMenuItem("时间"); -//// menuItem_time.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0)); -//// menu_edit.add(menuItem_time); -//// -//// -//// /*格式菜单*/ -//// JMenu menu_2 = new JMenu("格式"); -//// menuBar.add(menu_2); -//// -//// JMenuItem menuItem_font = new JMenuItem("字体"); -//// menu_2.add(menuItem_font); -//// //menuItem_font.addActionListener(this); -//// -//// JMenuItem menuItem_linewrap = new JMenuItem("自动换行"); -//// menu_2.add(menuItem_linewrap); -//// menuItem_linewrap.setEnabled(true); -//// //menuItem_linewrap.addActionListener(this); -//// -//// -//// JMenu menu_3 = new JMenu("查看"); -//// menuBar.add(menu_3); -//// -//// JMenuItem menuItem_viewmenu = new JMenuItem("状态栏"); -//// menu_3.add(menuItem_viewmenu); -//// menuItem_viewmenu.setEnabled(true); -//// //menuItem_viewmenu.addActionListener(this); -//// -//// -//// /*帮助菜单*/ -//// JMenu menu_4 = new JMenu("帮助"); -//// menuBar.add(menu_4); -//// -//// JMenuItem menuItem_17 = new JMenuItem("查看帮助"); -//// menu_4.add(menuItem_17); -//// //menuItem_17.addActionListener(this); -//// -//// JMenuItem menuItem_18 = new JMenuItem("关于清单记事本"); -//// menu_4.add(menuItem_18); -//// //menuItem_18.addActionListener(this); -//// -//// /*右键菜单*/ -//// JPopupMenu popupMenu=new JPopupMenu(); -//// popupMenu = new JPopupMenu(); -//// -//// JMenuItem popCut = new JMenuItem("剪切"); -//// popupMenu.add(popCut); -//// -//// JMenuItem popCopy = new JMenuItem("复制"); -//// popupMenu.add(popCopy); -//// -//// JMenuItem popPaste = new JMenuItem("粘贴"); -//// popupMenu.add(popPaste); -//// -//// JMenuItem popDelete=new JMenuItem("删除"); -//// popupMenu.add(popDelete); -//// -//// JMenuItem popAllselect=new JMenuItem("全选"); -//// popupMenu.add(popAllselect); -//// -//// -//// /*状态栏*/ -//// JToolBar toolBar=new JToolBar(); -//// toolBar.setFloatable(false); -//// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); -//// -//// JLabel label = new JLabel("当前字数:0",statusBar_status,SwingConstants.CENTER); -//// JLabel labelTime = new JLabel("日期: "+sdf.format(new Date(0, 0, 0))); -//// JLabel labelCodeStyle = new JLabel("编码: "+codestyle); -//// toolBar.add(label); -//// toolBar.addSeparator(new Dimension(180,5)); -//// toolBar.add(labelTime); -//// toolBar.addSeparator(new Dimension(180,5)); -//// toolBar.add(labelCodeStyle); -//// -//// con.add(textArea); -//// con.add(menu); -//// con.add(toolBar); -//// con.add(menuBar); -//// con.add(label); -//// con.add(popupMenu); -// -// } -// -// -// private static Icon statusBar_status; -// private static String codestyle="ANSI"; -// private JTextArea textArea; -// private File file; -// private JFileChooser chooser; -// private UndoManager manager; -// private JPopupMenu popupMenu; -// private JLabel label; -// private JMenuItem menuItem_cut; -// private JMenuItem menuItem_copy; -// private JMenuItem menuItem_save; -// private JMenuItem menuItem_undo; -// private JMenuItem menuItem_redo; -// private JMenuItem menuItem_saveas; -// private JMenuItem menuItem_new; -// private JMenuItem menuItem_exit; -// private JMenuItem menuItem_open; -// private JMenuItem menuItem_paste; -// private JMenuItem menuItem_selectall; -// private JScrollPane pane; -// private JFileChooser menuItem_time; -// private Object menuItem_find; -// -// private void window() -// { -//// -//// JTextArea textArea = new JTextArea(); //创建一个多行文本框 -//// UndoManager undomg=new UndoManager(); //创建撤销的管理 -//// JScrollPane scrollPane = new JScrollPane(textArea); //文本框里加上滚动条 -//// scrollPane.setBounds(300, 200, 100, 100); //设置文本框的大小 -//// textArea.setFont(new Font("Courier Prime",Font.PLAIN,12)); -//// textArea.setCaretColor(Color.black); //光标颜色 -//// textArea.setSelectedTextColor(Color.white); //选中字体颜色 -//// textArea.setSelectionColor(Color.blue); //选中字体的背景颜色 -//// textArea.setLineWrap(true); //自动换行 -//// textArea.setTabSize(4); -//// textArea.getDocument().addUndoableEditListener(this);//设置文本框可监听 -//// textArea.getDocument().addUndoableEditListener(undomg);//设置文本撤销 -//// -//// JScrollPane pane=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);//带滚动条面板 -//// -//// JMenuBar menuBar = new JMenuBar();//初始化菜单 -//// -//// Container conpane=this.getContentPane(); // 用Container对象获取当前框架的内容面板 -//// conpane.add(pane,BorderLayout.CENTER); //将多行文本框加入面板中心 -//// pane.setBounds(100, 100, 100, 100); //设置窗口在屏幕上的位置和大小 -//// pane.setVisible(true); -//// -//// /*文件菜单*/ -//// JMenu menu = new JMenu("文件"); -//// menuBar.add(menu); -//// -//// JMenuItem menuItem_save = new JMenuItem("保存"); //建立保存按钮 -//// menu.add(menuItem_save); -//// -//// -//// JMenuItem menuItem_saveas = new JMenuItem("另存为"); -//// menu.add(menuItem_saveas); -//// -//// -//// JMenuItem menuItem_new = new JMenuItem("新建"); -//// menu.add(menuItem_new); -//// -//// JMenuItem menuItem_open = new JMenuItem("打开"); -//// menu.add(menuItem_open); -//// -//// -//// JMenuItem menuItem_exit = new JMenuItem("退出"); -//// menu.add(menuItem_exit); -//// -//// /*编辑菜单*/ -//// JMenu menu_edit = new JMenu("编辑"); -//// menuBar.add(menu_edit); -//// -//// JMenuItem menuItem_undo = new JMenuItem("撤销"); -//// menuItem_undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_undo); -//// -//// -//// JMenuItem menuItem_cut = new JMenuItem("剪切"); -//// menuItem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_cut); -//// -//// -//// JMenuItem menuItem_paste = new JMenuItem("粘贴"); -//// menuItem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_paste); -//// -//// JMenuItem menuItem_copy = new JMenuItem("复制"); -//// menuItem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_copy); -//// -//// JMenuItem menuItem_redo = new JMenuItem("重做"); -//// menuItem_redo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_redo); -//// //undomg.redo(); -//// -//// JMenuItem menuItem_find = new JMenuItem("查找"); -//// menuItem_find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_find); -//// menuItem_find.addActionListener(this); -//// -//// JMenuItem menuItem_excharge = new JMenuItem("替换"); -//// menuItem_excharge.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_excharge); -//// menuItem_excharge.addActionListener(this); -//// -//// JMenuItem menuItem_selectall = new JMenuItem("全选"); -//// menuItem_selectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); -//// menu_edit.add(menuItem_selectall); -//// -//// JMenuItem menuItem_time = new JMenuItem("时间"); -//// menuItem_time.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0)); -//// menu_edit.add(menuItem_time); -//// -//// -//// /*格式菜单*/ -//// JMenu menu_2 = new JMenu("格式"); -//// menuBar.add(menu_2); -//// -//// JMenuItem menuItem_font = new JMenuItem("字体"); -//// menu_2.add(menuItem_font); -//// menuItem_font.addActionListener(this); -//// -//// JMenuItem menuItem_linewrap = new JMenuItem("自动换行"); -//// menu_2.add(menuItem_linewrap); -//// menuItem_linewrap.setEnabled(true); -//// menuItem_linewrap.addActionListener(this); -//// -//// -//// JMenu menu_3 = new JMenu("查看"); -//// menuBar.add(menu_3); -//// -//// JMenuItem menuItem_viewmenu = new JMenuItem("状态栏"); -//// menu_3.add(menuItem_viewmenu); -//// menuItem_viewmenu.setEnabled(true); -//// menuItem_viewmenu.addActionListener(this); -//// -//// -//// /*帮助菜单*/ -//// JMenu menu_4 = new JMenu("帮助"); -//// menuBar.add(menu_4); -//// -//// JMenuItem menuItem_17 = new JMenuItem("查看帮助"); -//// menu_4.add(menuItem_17); -//// menuItem_17.addActionListener(this); -//// -//// JMenuItem menuItem_18 = new JMenuItem("关于清单记事本"); -//// menu_4.add(menuItem_18); -//// menuItem_18.addActionListener(this); -//// -//// /*右键菜单*/ -//// JPopupMenu popupMenu=new JPopupMenu(); -//// popupMenu = new JPopupMenu(); -//// -//// JMenuItem popCut = new JMenuItem("剪切"); -//// popupMenu.add(popCut); -//// -//// JMenuItem popCopy = new JMenuItem("复制"); -//// popupMenu.add(popCopy); -//// -//// JMenuItem popPaste = new JMenuItem("粘贴"); -//// popupMenu.add(popPaste); -//// -//// JMenuItem popDelete=new JMenuItem("删除"); -//// popupMenu.add(popDelete); -//// -//// JMenuItem popAllselect=new JMenuItem("全选"); -//// popupMenu.add(popAllselect); -//// -//// -//// /*状态栏*/ -//// JToolBar toolBar=new JToolBar(); -//// toolBar.setFloatable(false); -//// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); -//// -//// JLabel label = new JLabel("当前字数:0",statusBar_status,SwingConstants.CENTER); -//// JLabel labelTime = new JLabel("日期: "+sdf.format(new Date(0, 0, 0))); -//// JLabel labelCodeStyle = new JLabel("编码: "+codestyle); -//// toolBar.add(label); -//// toolBar.addSeparator(new Dimension(180,5)); -//// toolBar.add(labelTime); -//// toolBar.addSeparator(new Dimension(180,5)); -//// toolBar.add(labelCodeStyle); -// } -// -// -// -// -// private void registerListener() { //监听器注册 -// this.setJMenuBar(pane); // 将菜单栏添加到框架中 -// -// textArea.addMouseListener(new MouseAdapter() { //加入鼠标监听器 -// -// public void mousePressed(MouseEvent e) { -// if(e.isPopupTrigger()){//返回此鼠标事件是否为该平台的弹出菜单触发事件 -// popupMenu.show(e.getComponent(),e.getX(),e.getY());//在组件调用者的坐标空间中的位置 X、Y 显示弹出菜单 -// } -// checkMenuItemEnabled();//设置剪切,复制,粘帖,删除等功能的可用性 -// textArea.requestFocus();//编辑区获取焦点 -// } -// private void checkMenuItemEnabled() { -// // TODO 自动生成的方法存根 -// -// } -// public void mouseReleased(MouseEvent e) -// { if(e.isPopupTrigger())//返回此鼠标事件是否为该平台的弹出菜单触发事件 -// { popupMenu.show(e.getComponent(),e.getX(),e.getY());//在组件调用者的坐标空间中的位置 X、Y 显示弹出菜单 -// } -// checkMenuItemEnabled();//设置剪切,复制,粘帖,删除等功能的可用性 -// textArea.requestFocus();//编辑区获取焦点 -// } -// }); -// this.setLocation(100,100); -// this.setSize(650,550); -// this.setVisible(true); -// -// -// -// 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(); // 一旦文本有改变就设置各按钮的可用性 -// } -// }); -// menuItem_save.addActionListener(e->saveFile()); //注册保存文件的监听器 -// menuItem_saveas.addActionListener(e->saveFile()); -// menuItem_new.addActionListener(e->newFile()); -// menuItem_open.addActionListener(e->openFile()); -// menuItem_exit.addActionListener(e->exit()); -// menuItem_cut.addActionListener(e->textArea.cut()); -// menuItem_paste.addActionListener(e->textArea.paste()); -// menuItem_copy.addActionListener(e->textArea.copy()); -// menuItem_selectall.addActionListener(e -> textArea.selectAll()); -// menuItem_time.addActionListener(e->onDateClick(e)); -// -// this.addWindowListener(new WindowAdapter() { //为框架添加退出保存的监视器 -// public void windowClosing(WindowEvent e) { -// String exitMessage = "需要保存输入的文本吗?"; -// if(textArea.getText().equals("")) -// System.exit(0); -// else{ -// if(JOptionPane.showConfirmDialog(null, exitMessage,"提示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION){ -// saveFile(); -// System.exit(0); -// }else -// System.exit(0); -// }}}); -// } -// public void find() { -// final JDialog findDialog=new JDialog(this,"查找",false); -// Container con=findDialog.getContentPane();//返回此对话框的contentPane对象 -// con.setLayout(new FlowLayout(FlowLayout.LEFT)); -// JLabel findContentLabel=new JLabel("查找内容:"); -// final JTextField findText=new JTextField(15); -// JButton findNextButton=new JButton("查找下一个:"); -// final JCheckBox matchCheckBox=new JCheckBox("区分大小写"); -// ButtonGroup bGroup=new ButtonGroup(); -// final JRadioButton upButton=new JRadioButton("向上"); -// final JRadioButton downButton=new JRadioButton("向下"); -// downButton.setSelected(true); -// bGroup.add(upButton); -// bGroup.add(downButton); -// JButton cancel=new JButton("取消"); //取消按钮事件处理 -// cancel.addActionListener(new ActionListener() -// { public void actionPerformed(ActionEvent e) -// { findDialog.dispose(); -// } -// }); //"查找下一个"按钮监听 -// findNextButton.addActionListener(new ActionListener() -// { public void actionPerformed(ActionEvent e) -// { //"区分大小写(C)"的JCheckBox是否被选中 -// int k=0,m=0; -// final String str1,str2,str3,str4,strA,strB; -// str1=textArea.getText(); -// str2=findText.getText(); -// str3=str1.toUpperCase(); -// str4=str2.toUpperCase(); -// if(matchCheckBox.isSelected())//区分大小写 -// { strA=str1; -// strB=str2; -// } -// else//不区分大小写,此时把所选内容全部化成大写(或小写),以便于查找 -// { strA=str3; -// strB=str4; -// } -// if(upButton.isSelected()) -// { -// if(textArea.getSelectedText()==null) -// k=strA.lastIndexOf(strB,textArea.getCaretPosition()-1); -// else -// k=strA.lastIndexOf(strB, textArea.getCaretPosition()-findText.getText().length()-1); -// if(k>-1) -// { -// textArea.setCaretPosition(k); -// textArea.select(k,k+strB.length()); -// } -// else -// { JOptionPane.showMessageDialog(null,"找不到您查找的内容!请重新输入!","查找",JOptionPane.INFORMATION_MESSAGE); -// } -// } -// else if(downButton.isSelected()) -// { if(textArea.getSelectedText()==null) -// k=strA.indexOf(strB,textArea.getCaretPosition()+1); -// else -// k=strA.indexOf(strB, textArea.getCaretPosition()-findText.getText().length()+1); -// if(k>-1) -// { -// textArea.setCaretPosition(k); -// textArea.select(k,k+strB.length()); -// } -// else -// { JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE); -// } -// } -// } -// }); -// -// JPanel panel1=new JPanel();//创建"查找"对话框的界面 -// JPanel panel2=new JPanel(); -// JPanel panel3=new JPanel(); -// JPanel directionPanel=new JPanel(); -// directionPanel.setBorder(BorderFactory.createTitledBorder("方向"));//设置directionPanel组件的边框; -// directionPanel.add(upButton); -// directionPanel.add(downButton); -// panel1.setLayout(new GridLayout(2,1)); -// panel1.add(findNextButton); -// panel1.add(cancel); -// panel2.add(findContentLabel); -// panel2.add(findText); -// panel2.add(panel1); -// panel3.add(matchCheckBox); -// panel3.add(directionPanel); -// con.add(panel2); -// con.add(panel3); -// findDialog.setSize(410,180); -// findDialog.setResizable(false);//不可调整大小 -// findDialog.setLocation(230,280); -// findDialog.setVisible(true); -// } -// -// -// -// private void onDateClick(ActionEvent event) { // 格式化当前的日期 -// Date nowTime = new Date(0); -// this.textArea.requestFocus(); -// SimpleDateFormat currentDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); -// this.textArea.insert(currentDateTime.format(nowTime), this.textArea.getCaretPosition()); -// } -// -// -// private void newFile(){ // 新建文件触发函数 -// if(!(textArea.getText().equals(""))){ -// int res = JOptionPane.showConfirmDialog -// (null,"当前内容未保存,需要保存吗?", -// "info",JOptionPane.YES_NO_OPTION); // 储存选择结果 -// if(res==JOptionPane.YES_OPTION){ -// saveFile(); -// textArea.setText(""); -// this.setTitle("时间记事本"); -// file = null; -// } -// else{ -// textArea.setText(""); // 取消则清空页面 -// this.setTitle("时间记事本"); -// file = null; -// } -// } -// } -// -// -// -// private void clearAll(){ // 清空当前页面触发函数 -// textArea.setText(""); -// } // 清空页面触发函数 -// -// private void openFile(){ // 打开文件触发函数 -// try { -// chooser = new JFileChooser("/Users/1kasshole/Desktop/");// 设置打开时的默认目录 -// int result = chooser.showOpenDialog(null); -// if (result == JFileChooser.APPROVE_OPTION) { // 点击打开按钮 -// file = chooser.getSelectedFile(); -// int length = (int) file.length(); -// FileReader reader = new FileReader(file); -// char[] ch = new char[length]; -// reader.read(ch); // 将文件读进char数组 -// reader.close(); -// textArea.setText(new String(ch).trim()); // 删除字符串的头尾空白符 -// setTitle(file.getName()); // 框架标题设置为文件名 -// } -// } catch (Exception e) { -// JOptionPane.showMessageDialog(null, e); -// } -// } -// -// private void saveFile(){ // 文件保存触发函数 -// if (file == null) -// try { -// chooser = new JFileChooser("/Users/1kasshole/Desktop/"); -// int result = chooser.showSaveDialog(null); -// if (result == JFileChooser.APPROVE_OPTION) { -// File selectfile =chooser.getSelectedFile(); // 获得文件 -// String end = chooser.getFileFilter().getDescription();// 获得被选中的过滤器中的文件扩展名 -// File newFile; -// if (selectfile.getAbsolutePath().toUpperCase().endsWith(end.toUpperCase())) {// 如果文件是以选定扩展名结束的,则使用原名 -// newFile = selectfile; -// } -// else { // 否则加上选定的扩展名 -// newFile = new File(selectfile.getAbsolutePath()+ end); -// } -// -// try { -// if (!newFile.exists()) { -// newFile.createNewFile(); -// } -// FileWriter writer = new FileWriter(newFile); -// char[] arry = textArea.getText().toCharArray(); -// writer.write(arry); -// writer.flush(); -// writer.close(); -// setTitle(newFile.getName()); -// file = newFile; -// } catch (IOException e) { -// e.printStackTrace(); -// }}} -// catch (Exception e) { -// JOptionPane.showMessageDialog(null, e); -// } -// -// else -// try { -// FileWriter writer = new FileWriter(file); -// char[] arry = textArea.getText().toCharArray(); -// writer.write(arry); -// writer.flush(); -// writer.close(); -// } catch (Exception e) { -// JOptionPane.showMessageDialog(null, e); -// } -// } -// -// private void undo(){ // 撤销触发函数 -// if (manager.canUndo()){ -// manager.undo(); -// } -// } -// -// private void redo(){ // 重做触发函数 -// if (manager.canRedo()){ -// manager.redo(); -// } -// } -// -// private void exit(){ // 退出程序触发函数 -// newFile(); -// System.exit(0); -// } -// -// private void setTextFont(){ // 选择字体触发函数 -// try{ -// GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();// 获取系统字体 -// JList fontNames = new JList<>(ge.getAvailableFontFamilyNames()); -// int response = JOptionPane.showConfirmDialog(null,new JScrollPane(fontNames), "请选择字体 ( 默认: Courier Prime )",JOptionPane.OK_CANCEL_OPTION); -// Object selectedFont = fontNames.getSelectedValue(); -// if (response == JOptionPane.OK_OPTION && selectedFont != null) -// textArea.setFont(new Font((String) fontNames.getSelectedValue(), Font.PLAIN,20)); -// } -// catch (Exception e){ -// e.printStackTrace(); -// } -// } -// -// private void setTextColor(){ // 字体颜色选择触发函数 -// 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 changeTextLengthStatus(){ // 文本监听 -// String content = textArea.getText().trim(); -// label.setText("当前字数:"+content.length()); -// } -// -// private void isItemsAvalible(){ // 监视文本区并设置各功能项是否可用 -// String content = textArea.getText(); -// if(content.equals("")){ -// menuItem_cut.setEnabled(false); -// menuItem_copy.setEnabled(false); -// menuItem_save.setEnabled(false); -// menuItem_undo.setEnabled(false); -// menuItem_redo.setEnabled(false); -// menuItem_open.setEnabled(false); -// menuItem_paste.setEnabled(false); -// menuItem_selectall.setEnabled(false); -// menuItem_exit.setEnabled(false); -// menuItem_saveas.setEnabled(false); -// } -// else{ -// menuItem_save.setEnabled(true); -// menuItem_cut.setEnabled(true); -// menuItem_copy.setEnabled(true); -// menuItem_undo.setEnabled(true); -// menuItem_redo.setEnabled(true); -// menuItem_open.setEnabled(true); -// menuItem_paste.setEnabled(true); -// menuItem_selectall.setEnabled(true); -// menuItem_exit.setEnabled(true); -// menuItem_saveas.setEnabled(true); -// } -// } -// -// -// -// -// -// -// -// private void setJMenuBar(JScrollPane pane) { -// // TODO 自动生成的方法存根 -// -// } -// -// -// -// private void add(JToolBar toolBar, String north) { -// // TODO 自动生成的方法存根 -// -// } -// -// -// -// -// private void setContentPane(JPanel contentPane) { -// -// } -// -// -// -// @Override -// public void insertUpdate(DocumentEvent e) { -// // TODO 自动生成的方法存根 -// -// } -// -// @Override -// public void removeUpdate(DocumentEvent e) { -// // TODO 自动生成的方法存根 -// -// } -// -// @Override -// public void changedUpdate(DocumentEvent e) { -// // TODO 自动生成的方法存根 -// -// } -// -// @Override -// public void actionPerformed(ActionEvent e) { -// // TODO 自动生成的方法存根 -// -// } -// -// -// -// -// @Override -// public void undoableEditHappened(UndoableEditEvent e) { -// // TODO 自动生成的方法存根 -// -// } -// -// -// } -// -// -// -// + JMenuBar menuBar=new JMenuBar(); //创建菜单条 + /*创建文件菜单及菜单项并注册事件监听*/ + menu_file=new JMenu("文件(F)"); + + menuItem_new=new JMenuItem("新建(N)"); + menuItem_new.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); + menuItem_new.addActionListener(this); + + menuItem_open=new JMenuItem("打开(O)..."); + menuItem_open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); + menuItem_open.addActionListener(this); + + menuItem_save=new JMenuItem("保存(S)"); + menuItem_save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); + menuItem_save.addActionListener(this); + + menuItem_saveas=new JMenuItem("另存为(A)..."); + menuItem_saveas.addActionListener(this); + + menuItem_exit=new JMenuItem("退出(X)"); + menuItem_exit.addActionListener(this); + + /*创建编辑菜单及菜单项并注册事件监听*/ + menu_edit=new JMenu("编辑(E)"); + menu_edit.setMnemonic('E');//设置快捷键ALT+E + + //当选择编辑菜单时,设置剪切、复制、粘贴、删除等功能的可用性 + + menu_edit.addMenuListener(new MenuListener() + { public void menuCanceled(MenuEvent e)//取消菜单时调用 + { checkMenuItemEnabled();//设置剪切、复制、粘贴、删除等功能的可用性 + } + public void menuDeselected(MenuEvent e)//取消选择某个菜单时调用 + { checkMenuItemEnabled();//设置剪切、复制、粘贴、删除等功能的可用性 + } + public void menuSelected(MenuEvent e)//选择某个菜单时调用 + { checkMenuItemEnabled();//设置剪切、复制、粘贴、删除等功能的可用性 + } + }); + + menuItem_undo=new JMenuItem("撤销(U)"); + menuItem_undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK)); + menuItem_undo.addActionListener(this); + menuItem_undo.setEnabled(false); + + menuItem_cut=new JMenuItem("剪切(T)"); + menuItem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK)); + menuItem_cut.addActionListener(this); + + menuItem_copy=new JMenuItem("复制(C)"); + menuItem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); + menuItem_copy.addActionListener(this); + + menuItem_paste=new JMenuItem("粘贴(P)"); + menuItem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); + menuItem_paste.addActionListener(this); + + menuItem_delete=new JMenuItem("删除(D)"); + menuItem_delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0)); + menuItem_delete.addActionListener(this); + + menuItem_find=new JMenuItem("查找(F)..."); + menuItem_find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK)); + menuItem_find.addActionListener(this); + + menuItem_replace = new JMenuItem("替换(R)...",'R'); + menuItem_replace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK)); + menuItem_replace.addActionListener(this); + + menuItem_selectall = new JMenuItem("全选",'A'); + menuItem_selectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); + menuItem_selectall.addActionListener(this); + + menuItem_time = new JMenuItem("时间/日期(D)",'D'); + menuItem_time.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0)); + menuItem_time.addActionListener(this); + + /*创建格式菜单及菜单项并注册事件监听*/ + menu_format=new JMenu("格式(O)"); + menu_format.setMnemonic('O');//设置快捷键ALT+O + + menuItem_linewrap=new JCheckBoxMenuItem("自动换行(W)"); + menuItem_linewrap.setMnemonic('W');//设置快捷键ALT+W + menuItem_linewrap.setState(true); + menuItem_linewrap.addActionListener(this); + + menuItem_font=new JMenuItem("字体(F)..."); + menuItem_font.addActionListener(this); + + //创建查看菜单及菜单项并注册事件监听 + menu_view=new JMenu("查看(V)"); + menu_view.setMnemonic('V');//设置快捷键ALT+V + + menuItem_status=new JCheckBoxMenuItem("状态栏(S)"); + menuItem_status.setMnemonic('S');//设置快捷键ALT+S + menuItem_status.setState(true); + menuItem_status.addActionListener(this); + + //创建帮助菜单及菜单项并注册事件监听 + menu_help = new JMenu("帮助(H)"); + menu_help.setMnemonic('H');//设置快捷键ALT+H + + menuItem_HelpTopics = new JMenuItem("帮助主题(H)"); + menuItem_HelpTopics.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0)); + menuItem_HelpTopics.addActionListener(this); + + menuItem_AboutNotepad = new JMenuItem("关于记事本(A)"); + menuItem_AboutNotepad.addActionListener(this); + + //向菜单条添加"文件"菜单及菜单项 + menuBar.add(menu_file); + menu_file.add(menuItem_new); + menu_file.add(menuItem_open); + menu_file.add(menuItem_save); + menu_file.add(menuItem_saveas); + menu_file.add(menuItem_exit); + + //向菜单条添加"编辑"菜单及菜单项 + menuBar.add(menu_edit); + menu_edit.add(menuItem_undo); + menu_edit.add(menuItem_cut); + menu_edit.add(menuItem_copy); + menu_edit.add(menuItem_paste); + menu_edit.add(menuItem_delete); + menu_edit.add(menuItem_find); + menu_edit.add(menuItem_replace); + menu_edit.add(menuItem_selectall); + menu_edit.add(menuItem_time); + + //向菜单条添加"格式"菜单及菜单项 + menuBar.add(menu_format); + menu_format.add(menuItem_linewrap); + menu_format.add(menuItem_font); + + //向菜单条添加"查看"菜单及菜单项 + menuBar.add(menu_view); + menu_view.add(menuItem_status); + + //向菜单条添加"帮助"菜单及菜单项 + menuBar.add(menu_help); + menu_help.add(menuItem_HelpTopics); + menu_help.addSeparator(); + menu_help.add(menuItem_AboutNotepad); + + //向窗口添加菜单条 + this.setJMenuBar(menuBar); + + textArea=new JTextArea(100,100); + JScrollPane scroller=new JScrollPane(textArea); + + + //创建右键弹出菜单 + popupMenu=new JPopupMenu(); + popupmenu_undo=new JMenuItem("撤销"); + popupmenu_cut=new JMenuItem("剪切"); + popupmenu_copy=new JMenuItem("复制"); + popupmenu_paste=new JMenuItem("粘帖"); + popupmenu_delete=new JMenuItem("删除"); + popupmenu_selectall=new JMenuItem("全选"); + popupmenu_undo.setEnabled(false); + + //向右键菜单添加菜单项和分隔符 + popupMenu.add(popupmenu_undo); + popupMenu.addSeparator(); + popupMenu.add(popupmenu_cut); + popupMenu.add(popupmenu_copy); + popupMenu.add(popupmenu_paste); + popupMenu.add(popupmenu_delete); + popupMenu.addSeparator(); + popupMenu.add(popupmenu_selectall); + + //文本编辑区注册右键菜单事件 + popupmenu_undo.addActionListener(this); + popupmenu_cut.addActionListener(this); + popupmenu_copy.addActionListener(this); + popupmenu_paste.addActionListener(this); + popupmenu_delete.addActionListener(this); + popupmenu_selectall.addActionListener(this); + + //文本编辑区注册右键菜单事件 + textArea.addMouseListener(new MouseAdapter() + { public void mousePressed(MouseEvent e) + { if(e.isPopupTrigger())//返回此鼠标事件是否为该平台的弹出菜单触发事件 + { popupMenu.show(e.getComponent(),e.getX(),e.getY());//在组件调用者的坐标空间中的位置 X、Y 显示弹出菜单 + } + checkMenuItemEnabled();//设置剪切,复制,粘帖,删除等功能的可用性 + textArea.requestFocus();//编辑区获取焦点 + } + public void mouseReleased(MouseEvent e) + { if(e.isPopupTrigger())//返回此鼠标事件是否为该平台的弹出菜单触发事件 + { popupMenu.show(e.getComponent(),e.getX(),e.getY());//在组件调用者的坐标空间中的位置 X、Y 显示弹出菜单 + } + checkMenuItemEnabled();//设置剪切,复制,粘帖,删除等功能的可用性 + textArea.requestFocus();//编辑区获取焦点 + } + });//文本编辑区注册右键菜单事件结束 + + //创建和添加状态栏 + JToolBar statusLabel=new JToolBar(); + statusLabel.setFloatable(false); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + JLabel label = new JLabel("当前字数:0",statusBar_status,SwingConstants.CENTER); + JLabel labelTime = new JLabel("日期: "+sdf.format(new Date(0, 0, 0))); + JLabel labelCodeStyle = new JLabel("编码: "+codestyle); + statusLabel.add(label); + statusLabel.addSeparator(new Dimension(180,5)); + statusLabel.add(labelTime); + statusLabel.addSeparator(new Dimension(180,5)); + statusLabel.add(labelCodeStyle); + this.add(statusLabel,BorderLayout.SOUTH);//向窗口添加状态栏标签 + this.setLocation(100,100);//设置窗口在屏幕上的位置 + this.setSize(650,550);//设置窗口在屏幕上的大小 + this.setVisible(true);//设置窗口在屏幕上的可见性 + + /*添加窗口监听器*/ + addWindowListener(new WindowAdapter() + { public void windowClosing(WindowEvent e) + { exitWindowChoose(); + } + }); + checkMenuItemEnabled(); + textArea.requestFocus(); + } + + //设置菜单项的可用性:剪切,复制,粘帖,删除功能 + public void checkMenuItemEnabled() + { String selectText=textArea.getSelectedText(); + if(selectText==null) + { menuItem_cut.setEnabled(false); + popupmenu_cut.setEnabled(false); + menuItem_copy.setEnabled(false); + popupmenu_copy.setEnabled(false); + menuItem_delete.setEnabled(false); + popupmenu_delete.setEnabled(false); + } + else + { menuItem_cut.setEnabled(true); + popupmenu_cut.setEnabled(true); + menuItem_copy.setEnabled(true); + popupmenu_copy.setEnabled(true); + menuItem_delete.setEnabled(true); + popupmenu_delete.setEnabled(true); + } + /*粘帖功能可用性判断*/ + Transferable contents=clipBoard.getContents(this); + if(contents==null) + { menuItem_paste.setEnabled(false); + popupmenu_paste.setEnabled(false); + } + else + { menuItem_paste.setEnabled(true); + popupmenu_paste.setEnabled(true); + } + } + + /*关闭窗口时调用*/ + public void exitWindowChoose() + { textArea.requestFocus(); + String currentValue=textArea.getText(); + if(currentValue.equals(oldValue)==true) + { System.exit(0); + } + else + { int exitChoose=JOptionPane.showConfirmDialog(this,"你的文件还没保存哦:)是否保存?","退出!!",JOptionPane.YES_NO_CANCEL_OPTION); + if(exitChoose==JOptionPane.YES_OPTION) + { + if(isNewFile) + { + String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setApproveButtonText("!!!确定!!!"); + fileChooser.setDialogTitle("另存为:"); + + int result=fileChooser.showSaveDialog(this); + + + File saveFileName=fileChooser.getSelectedFile(); + + if(saveFileName==null||saveFileName.getName().equals("")) + { JOptionPane.showMessageDialog(this,"不合法的文件名!","不合法的文件名!",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileWriter fw=new FileWriter(saveFileName); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush(); + fw.close(); + + isNewFile=false; + currentFile=saveFileName; + oldValue=textArea.getText(); + + this.setTitle(saveFileName.getName()+" de记事本"); + //isSave=true; + } + catch(IOException ioException){ + } + } + } + else + { + try + { FileWriter fw=new FileWriter(currentFile); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush(); + fw.close(); + + } + catch(IOException ioException){ + } + } + System.exit(0); + + } + else if(exitChoose==JOptionPane.NO_OPTION) + { System.exit(0); + } + else + { return; + } + } + } + + + + + }} -- Gitee From 27cccd09169578f88581a226e262fb8f4226301a Mon Sep 17 00:00:00 2001 From: lenovo Date: Thu, 10 Jun 2021 07:45:17 +0800 Subject: [PATCH 05/19] =?UTF-8?q?=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Notebook.java | 518 ---------------------------------------------- 1 file changed, 518 deletions(-) delete mode 100644 src/Notebook.java diff --git a/src/Notebook.java b/src/Notebook.java deleted file mode 100644 index 30df110..0000000 --- a/src/Notebook.java +++ /dev/null @@ -1,518 +0,0 @@ -import java.awt.BorderLayout; -import java.awt.EventQueue; -import java.awt.Font; -import java.awt.GraphicsEnvironment; - -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.JPopupMenu; -import javax.swing.border.EmptyBorder; -import javax.swing.event.DocumentEvent; -import javax.swing.event.UndoableEditEvent; -import javax.swing.event.UndoableEditListener; -import javax.swing.undo.UndoManager; -import javax.swing.JMenuBar; -import javax.swing.JMenu; -import javax.swing.JMenuItem; -import javax.swing.JOptionPane; -import javax.swing.JTextArea; -import javax.swing.JToolBar; -import javax.swing.UIManager; -import javax.swing.WindowConstants; -import javax.swing.JCheckBoxMenuItem; -import javax.swing.JColorChooser; -import javax.swing.JComboBox; -import javax.swing.JDialog; -import javax.swing.JFileChooser; -import javax.swing.AbstractButton; -import javax.swing.Icon; -import javax.swing.JButton; -import javax.swing.JCheckBox; -import java.awt.List; -import java.awt.Checkbox; -import javax.swing.JList; -import java.awt.Choice; -import java.awt.Color; -import java.awt.Component; -import java.awt.Container; -import java.awt.Dimension; -import java.awt.ScrollPane; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.sql.Date; -import java.text.SimpleDateFormat; - -import javax.swing.JScrollPane; -import javax.swing.JTabbedPane; -import javax.swing.KeyStroke; -import javax.swing.SwingConstants; - -import java.awt.event.KeyEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.awt.event.InputEvent; -import java.awt.event.ItemEvent; -import java.awt.event.ItemListener; -import java.awt.GraphicsEnvironment; - -public class Notebook { - public static void main(String[] args) { - new TimeNote(""); - /*Notebook TimeNote=new Notebook(); - JFrame window=new JFrame ("时间记事本"); //设置窗口名称 - Container con=window.getContentPane(); - con.setBackground(doushalv); //设置记事本的背景颜色为护眼豆沙绿 - window.setBounds(100, 100, 800, 500); //设置窗口在屏幕上的位置和大小 - window.setVisible(true); - window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出程序*/ - } - } - class TimeNote extends JFrame implements UndoableEditListener{ - private Icon toolBar_Cut; - private Icon toolBar_Copy; - private Icon toolBar_Paste; - private Icon toolBar_Delete; - private Icon toolBar_Allselect; - private Icon statusBar_status; - private String codestyle; - private JTextArea textArea; - private File file; - private JFileChooser chooser; - private UndoManager manager; - private JPopupMenu popupMenu; - private JLabel label; - private JMenuItem menuItem_cut; - private JMenuItem menuItem_copy; - private JMenuItem menuItem_save; - private JMenuItem menuItem_undo; - private JMenuItem menuItem_redo; - - public TimeNote(String title) { - super(title); //窗体标题 - //window(); - //registerListener(); - } - private void window() { - - JTextArea textArea = new JTextArea(); - UndoManager undomg=new UndoManager(); - JScrollPane scrollPane = new JScrollPane(textArea); //文本框里加上滚动条 - scrollPane.setBounds(100, 100, 100, 100); //设置文本框的大小 - - textArea.setFont(new Font("Courier Prime",Font.PLAIN,12)); - textArea.setCaretColor(Color.black); //光标颜色 - textArea.setSelectedTextColor(Color.white); //选中字体颜色 - textArea.setSelectionColor(Color.blue); //选中字体的背景颜色 - textArea.setLineWrap(true); //自动换行 - textArea.setTabSize(4); - textArea.getDocument().addUndoableEditListener(this);//设置文本框可监听 - textArea.getDocument().addUndoableEditListener(undomg); - - JScrollPane pane=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);//带滚动条面板 - - JMenuBar menuBar = new JMenuBar();//初始化菜单 - - Container conpane=this.getContentPane(); // 用Container对象获取当前框架的内容面板 - conpane.add(pane,BorderLayout.CENTER);//将多行文本框加入面板中心 - pane.setBounds(100, 100, 800, 500); //设置窗口在屏幕上的位置和大小 - pane.setVisible(true); - - /*文件菜单*/ - JMenu menu = new JMenu("文件"); - menuBar.add(menu); - - JMenuItem menuItem_save = new JMenuItem("保存"); - menu.add(menuItem_save); - menuItem_save.addActionListener(e->saveFile()); - - JMenuItem menuItem_saveas = new JMenuItem("另存为"); - menu.add(menuItem_saveas); - menuItem_saveas.addActionListener(e->saveFile()); - - JMenuItem menuItem_new = new JMenuItem("新建"); - menu.add(menuItem_new); - menuItem_new.addActionListener(e->newFile()); - - JMenuItem menuItem_open = new JMenuItem("打开"); - menu.add(menuItem_open); - menuItem_open.addActionListener(e->openFile()); - - JMenuItem menuItem_exit = new JMenuItem("退出"); - menu.add(menuItem_exit); - menuItem_exit.addActionListener(e->exit()); - - /*编辑菜单*/ - JMenu menu_edit = new JMenu("编辑"); - menuBar.add(menu_edit); - - JMenuItem menuItem_undo = new JMenuItem("撤销"); - menuItem_undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK)); - menu_edit.add(menuItem_undo); - //undomg.undo(); - - JMenuItem menuItem_cut = new JMenuItem("剪切"); - menuItem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); - menu_edit.add(menuItem_cut); - menuItem_cut.addActionListener(e->textArea.cut()); - - JMenuItem menuItem_paste = new JMenuItem("粘贴"); - menuItem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK)); - menu_edit.add(menuItem_paste); - menuItem_paste.addActionListener(e->textArea.paste()); - - JMenuItem menuItem_copy = new JMenuItem("复制"); - menuItem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); - menu_edit.add(menuItem_copy); - menuItem_copy.addActionListener(e->textArea.copy()); - - JMenuItem menuItem_redo = new JMenuItem("重做"); - menuItem_redo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK)); - menu_edit.add(menuItem_redo); - undomg.redo(); - - JMenuItem menuItem_find = new JMenuItem("查找"); - menuItem_find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK)); - menu_edit.add(menuItem_find); - //menuItem_find.addActionListener(e -> checkBox_setFont()); - - JMenuItem menuItem_excharge = new JMenuItem("替换"); - menuItem_excharge.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK)); - menu_edit.add(menuItem_excharge); - //menuItem_excharge.addActionListener(e -> checkBox_setFont()); - - JMenuItem menuItem_selectall = new JMenuItem("全选"); - menuItem_selectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); - menu_edit.add(menuItem_selectall); - menuItem_selectall.addActionListener(e -> textArea.selectAll()); - - JMenuItem menuItem_time = new JMenuItem("时间"); - menuItem_time.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0)); - menu_edit.add(menuItem_time); - //menuItem_time.addActionListener(this); - - /*格式菜单*/ - JMenu menu_2 = new JMenu("格式"); - menuBar.add(menu_2); - - JMenuItem menuItem_font = new JMenuItem("字体"); - menu_2.add(menuItem_font); - //menuItem_font.addActionListener(this); - - - - JMenuItem menuItem_15 = new JMenuItem("自动换行"); - menu_2.add(menuItem_15); - - JMenu menu_3 = new JMenu("帮助"); - menuBar.add(menu_3); - - JMenuItem menuItem_16 = new JMenuItem("状态栏"); - menu_3.add(menuItem_16); - - /*帮助菜单*/ - JMenu menu_4 = new JMenu("帮助"); - menuBar.add(menu_4); - - JMenuItem menuItem_17 = new JMenuItem("查看帮助"); - menu_4.add(menuItem_17); - - JMenuItem menuItem_18 = new JMenuItem("关于清单记事本"); - menu_4.add(menuItem_18); - - /*右键菜单*/ - JPopupMenu popupMenu=new JPopupMenu(); - popupMenu = new JPopupMenu(); - - JMenuItem popCut = new JMenuItem("剪切",toolBar_Cut); - popupMenu.add(popCut); - - JMenuItem popCopy = new JMenuItem("复制",toolBar_Copy); - popupMenu.add(popCopy); - - JMenuItem popPaste = new JMenuItem("粘贴",toolBar_Paste); - popupMenu.add(popPaste); - - JMenuItem popDelete=new JMenuItem("删除",toolBar_Delete); - popupMenu.add(popDelete); - - JMenuItem popAllselect=new JMenuItem("全选",toolBar_Allselect); - popupMenu.add(popAllselect); - - /*状态栏*/ - JToolBar toolBar=new JToolBar(); - toolBar.setFloatable(false); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); - - JLabel label = new JLabel("当前字数:0",statusBar_status,SwingConstants.CENTER); - JLabel labelTime = new JLabel("日期: "+sdf.format(new Date(0, 0, 0))); - JLabel labelCodeStyle = new JLabel("编码: "+codestyle); - toolBar.add(label); - toolBar.addSeparator(new Dimension(180,5)); - toolBar.add(labelTime); - toolBar.addSeparator(new Dimension(180,5)); - toolBar.add(labelCodeStyle); - - this.add(toolBar,BorderLayout.NORTH); // 将工具栏添加到框架中 - this.setJMenuBar(pane); // 将菜单栏添加到框架中 - - this.addWindowListener(new WindowAdapter() { //为框架添加退出保存的监视器 - - public void windowClosing(WindowEvent e) { - String exitMessage = "需要保存输入的文本吗?"; - - if(textArea.getText().equals("")) - - System.exit(0); - - else{ - if(JOptionPane.showConfirmDialog(null, exitMessage,"提示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION){ - saveFile(); - System.exit(0); - }else - System.exit(0); - }}}); - - } - - - private void addWindowListener(WindowAdapter windowAdapter) { - // TODO 自动生成的方法存根 - - } - - - private void newFile(){ // 新建文件触发函数 - if(!(textArea.getText().equals(""))){ - int res = JOptionPane.showConfirmDialog - (null,"当前内容未保存,需要保存吗?", - "info",JOptionPane.YES_NO_OPTION); // 储存选择结果 - if(res==JOptionPane.YES_OPTION){ - saveFile(); - textArea.setText(""); - this.setTitle("时间记事本"); - file = null; - } - else{ - textArea.setText(""); // 取消则清空页面 - this.setTitle("时间记事本"); - file = null; - } - } - } - - - - private void clearAll(){ // 清空当前页面触发函数 - textArea.setText(""); - } // 清空页面触发函数 - - private void openFile(){ // 打开文件触发函数 - try { - chooser = new JFileChooser("/Users/1kasshole/Desktop/");// 设置打开时的默认目录 - int result = chooser.showOpenDialog(null); - if (result == JFileChooser.APPROVE_OPTION) { // 点击打开按钮 - file = chooser.getSelectedFile(); - int length = (int) file.length(); - FileReader reader = new FileReader(file); - char[] ch = new char[length]; - reader.read(ch); // 将文件读进char数组 - reader.close(); - textArea.setText(new String(ch).trim()); // 删除字符串的头尾空白符 - setTitle(file.getName()); // 框架标题设置为文件名 - } - } catch (Exception e) { - JOptionPane.showMessageDialog(null, e); - } - } - - private void saveFile(){ // 文件保存触发函数 - if (file == null) - try { - chooser = new JFileChooser("/Users/1kasshole/Desktop/"); - int result = chooser.showSaveDialog(null); - if (result == JFileChooser.APPROVE_OPTION) { - File selectfile =chooser.getSelectedFile(); // 获得文件 - String end = chooser.getFileFilter().getDescription();// 获得被选中的过滤器中的文件扩展名 - File newFile; - if (selectfile.getAbsolutePath().toUpperCase().endsWith(end.toUpperCase())) {// 如果文件是以选定扩展名结束的,则使用原名 - newFile = selectfile; - } - else { // 否则加上选定的扩展名 - newFile = new File(selectfile.getAbsolutePath()+ end); - } - - try { - if (!newFile.exists()) { - newFile.createNewFile(); - } - FileWriter writer = new FileWriter(newFile); - char[] arry = textArea.getText().toCharArray(); - writer.write(arry); - writer.flush(); - writer.close(); - setTitle(newFile.getName()); - file = newFile; - } catch (IOException e) { - e.printStackTrace(); - }}} - catch (Exception e) { - JOptionPane.showMessageDialog(null, e); - } - - else - try { - FileWriter writer = new FileWriter(file); - char[] arry = textArea.getText().toCharArray(); - writer.write(arry); - writer.flush(); - writer.close(); - } catch (Exception e) { - JOptionPane.showMessageDialog(null, e); - } - } - - private void undo(){ // 撤销触发函数 - if (manager.canUndo()){ - manager.undo(); - } - } - - private void exit(){ // 退出程序触发函数 - newFile(); - System.exit(0); - } - - private void setTextFont(){ // 选择字体触发函数 - try{ - GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();// 获取系统字体 - JList fontNames = new JList<>(ge.getAvailableFontFamilyNames()); - int response = JOptionPane.showConfirmDialog(null,new JScrollPane(fontNames), "请选择字体 ( 默认: Courier Prime )",JOptionPane.OK_CANCEL_OPTION); - Object selectedFont = fontNames.getSelectedValue(); - if (response == JOptionPane.OK_OPTION && selectedFont != null) - textArea.setFont(new Font((String) fontNames.getSelectedValue(), Font.PLAIN,20)); - } - catch (Exception e){ - e.printStackTrace(); - } - } - - private void setTextColor(){ // 字体颜色选择触发函数 - 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 changeTextLengthStatus(){ // 文本监听 - String content = textArea.getText().trim(); - label.setText("当前字数:"+content.length()); - } - - private void isItemsAvalible(){ // 监视文本区并设置各功能项是否可用 - String content = textArea.getText(); - if(content.equals("")){ - menuItem_cut.setEnabled(false); - menuItem_copy.setEnabled(false); - menuItem_save.setEnabled(false); - } - else{ - menuItem_save.setEnabled(true); - menuItem_cut.setEnabled(true); - menuItem_copy.setEnabled(true); - menuItem_undo.setEnabled(true); - menuItem_redo.setEnabled(true); - } - } - - - - - - - - private Object checkBox_setFont() { - // TODO 自动生成的方法存根 - return null; - } - - - - - - - - - - private void registerListener() { - textArea.addMouseListener(new MouseAdapter() { //加入鼠标监听器 - public void mousePressed(MouseEvent e) { - maybeShowPopup(e); - } - public void mouseReleased(MouseEvent e) { - maybeShowPopup(e); - } - }); - } - - - - - private void setJMenuBar(JScrollPane pane) { - // TODO 自动生成的方法存根 - - } - - - - private void add(JToolBar toolBar, String north) { - // TODO 自动生成的方法存根 - - } - - - - /*private void setDefaultCloseOperation(int exitOnClose) { - // TODO 自动生成的方法存根 - - } - - - - private Container getContentPane() { - // TODO 自动生成的方法存根 - return null; - }*/ - - - - private void setContentPane(JPanel contentPane) { - - } - public final Color doushalv = new Color(199,237,204);//新建颜色为护眼豆沙绿 - - @Override - public void undoableEditHappened(UndoableEditEvent e) { - // TODO 自动生成的方法存根 - - } - - } - - - - - -- Gitee From 586f375f7800bc5827888a1ef3e5485b0168bcff Mon Sep 17 00:00:00 2001 From: lenovo Date: Thu, 10 Jun 2021 12:05:46 +0800 Subject: [PATCH 06/19] =?UTF-8?q?=E4=B8=B0=E5=AF=8C=E5=90=84=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/TimeNoteBook/Notebook.java | 1122 ++++++++++++++++++++++++++++---- 1 file changed, 994 insertions(+), 128 deletions(-) diff --git a/src/TimeNoteBook/Notebook.java b/src/TimeNoteBook/Notebook.java index 4f0d3ba..bae6d28 100644 --- a/src/TimeNoteBook/Notebook.java +++ b/src/TimeNoteBook/Notebook.java @@ -2,19 +2,25 @@ package TimeNoteBook; import java.awt.*; import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import javax.swing.*; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; import javax.swing.event.MenuEvent; import javax.swing.event.MenuListener; import javax.swing.event.UndoableEditEvent; import javax.swing.event.UndoableEditListener; +import javax.swing.undo.CannotUndoException; import javax.swing.undo.UndoManager; import java.io.*; import java.sql.Date; import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Enumeration; import javax.swing.*; @@ -23,14 +29,17 @@ import java.beans.EventHandler; public class Notebook extends JFrame implements DocumentListener,ActionListener { private static Icon statusBar_status; - private static String codestyle = "ANSI"; - /*菜单*/ + private static String codestyle = "UTF-8"; + public static final Color doushalv = new Color(199,237,204);//鏂板缓棰滆壊涓烘姢鐪艰眴娌欑豢 + + /*鑿滃崟*/ private JMenu menu_file; private JMenu menu_edit; private JMenu menu_format; private JMenu menu_help; private JMenu menu_view; - /*右键弹出菜单*/ + + /*鍙抽敭寮瑰嚭鑿滃崟*/ private JPopupMenu popupMenu; private JMenuItem popupmenu_undo; private JMenuItem popupmenu_cut; @@ -38,13 +47,15 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener private JMenuItem popupmenu_paste; private JMenuItem popupmenu_delete; private JMenuItem popupmenu_selectall; - /*文件菜单*/ + + /*鏂囦欢鑿滃崟*/ private JMenuItem menuItem_new; private JMenuItem menuItem_open; private JMenuItem menuItem_save; private JMenuItem menuItem_saveas; private JMenuItem menuItem_exit; - /*编辑菜单*/ + + /*缂栬緫鑿滃崟*/ private JMenuItem menuItem_undo; private JMenuItem menuItem_time; private JMenuItem menuItem_find; @@ -54,33 +65,41 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener private JMenuItem menuItem_cut; private JMenuItem menuItem_copy; private JMenuItem menuItem_delete; - /*格式菜单*/ + + /*鏍煎紡鑿滃崟*/ private JCheckBoxMenuItem menuItem_linewrap; private JMenuItem menuItem_font; - /*查看的菜单*/ + + /*鏌ョ湅鐨勮彍鍗*/ private JCheckBoxMenuItem menuItem_status; - /*帮助的菜单*/ + + /*甯姪鐨勮彍鍗*/ private JMenuItem menuItem_HelpTopics; - private JMenuItem menuItem_AboutNotepad; - /*文本编辑区域*/ - private JTextArea textArea; - /*状态栏*/ - private JToolBar statusLabel; + private JMenuItem menuItem_AboutNoteBook; + /*鏂囨湰缂栬緫鍖哄煙*/ + private JTextArea textArea; + /*鐘舵佹爮*/ + private JLabel statusLabel; private JMenuBar menuBar; + private JLabel labelTime; Toolkit toolkit=Toolkit.getDefaultToolkit(); - Clipboard clipBoard=toolkit.getSystemClipboard();//创建撤销操作管理器(与撤销操作有关) + Clipboard clipBoard=toolkit.getSystemClipboard(); + + /*鍒涘缓鎾ら攢鎿嶄綔绠$悊鍣*/ protected UndoManager undo=new UndoManager(); - protected UndoableEditListener undoHandler=new UndoHandler();//其他变量 - private String oldValue;//存放编辑区原来的内容,用于比较文本是否有改动 - boolean isNewFile=true;//是否新文件(未保存过的) + protected UndoableEditListener undoHandler=new UndoHandler();//鍏朵粬鍙橀噺 + private String oldValue;//瀛樻斁缂栬緫鍖哄師鏉ョ殑鍐呭锛岀敤浜庢瘮杈冩枃鏈槸鍚︽湁鏀瑰姩 + boolean isNewFile=true;//鏄惁鏂版枃浠(鏈繚瀛樿繃鐨) File currentFile; - //当前文件名 + private JLabel label; + + //褰撳墠鏂囦欢鍚 public Notebook(){ - super("时间记事本"); - Font font=new Font("Courier Prime", Font.PLAIN, 12); //改变系统默认字体 + super("鏃堕棿璁颁簨鏈"); + Font font=new Font("Courier Prime", Font.PLAIN, 12); //鏀瑰彉绯荤粺榛樿瀛椾綋 Enumeration keys = UIManager.getDefaults().keys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); @@ -89,117 +108,119 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener UIManager.put(key,font); } } - - JMenuBar menuBar=new JMenuBar(); //创建菜单条 - /*创建文件菜单及菜单项并注册事件监听*/ - menu_file=new JMenu("文件(F)"); + + + JMenuBar menuBar=new JMenuBar(); //鍒涘缓鑿滃崟鏉 + + /*鍒涘缓鏂囦欢鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚*/ + menu_file=new JMenu("鏂囦欢"); - menuItem_new=new JMenuItem("新建(N)"); + menuItem_new=new JMenuItem("鏂板缓"); menuItem_new.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); menuItem_new.addActionListener(this); - menuItem_open=new JMenuItem("打开(O)..."); + menuItem_open=new JMenuItem("鎵撳紑"); menuItem_open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); menuItem_open.addActionListener(this); - menuItem_save=new JMenuItem("保存(S)"); + menuItem_save=new JMenuItem("淇濆瓨"); menuItem_save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); menuItem_save.addActionListener(this); - menuItem_saveas=new JMenuItem("另存为(A)..."); + menuItem_saveas=new JMenuItem("鍙﹀瓨涓"); menuItem_saveas.addActionListener(this); - menuItem_exit=new JMenuItem("退出(X)"); + menuItem_exit=new JMenuItem("閫鍑"); menuItem_exit.addActionListener(this); - /*创建编辑菜单及菜单项并注册事件监听*/ - menu_edit=new JMenu("编辑(E)"); - menu_edit.setMnemonic('E');//设置快捷键ALT+E - - //当选择编辑菜单时,设置剪切、复制、粘贴、删除等功能的可用性 + /*鍒涘缓缂栬緫鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚*/ + menu_edit=new JMenu("缂栬緫"); + menu_edit.setMnemonic('E');//璁剧疆蹇嵎閿瓵LT+E + + /*褰撻夋嫨缂栬緫鑿滃崟鏃讹紝璁剧疆鍓垏銆佸鍒躲佺矘璐淬佸垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ*/ menu_edit.addMenuListener(new MenuListener() - { public void menuCanceled(MenuEvent e)//取消菜单时调用 - { checkMenuItemEnabled();//设置剪切、复制、粘贴、删除等功能的可用性 + { public void menuCanceled(MenuEvent e)//鍙栨秷鑿滃崟鏃惰皟鐢 + { checkMenuItemEnabled();//璁剧疆鍓垏銆佸鍒躲佺矘璐淬佸垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ } - public void menuDeselected(MenuEvent e)//取消选择某个菜单时调用 - { checkMenuItemEnabled();//设置剪切、复制、粘贴、删除等功能的可用性 + public void menuDeselected(MenuEvent e)//鍙栨秷閫夋嫨鏌愪釜鑿滃崟鏃惰皟鐢 + { checkMenuItemEnabled();//璁剧疆鍓垏銆佸鍒躲佺矘璐淬佸垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ } - public void menuSelected(MenuEvent e)//选择某个菜单时调用 - { checkMenuItemEnabled();//设置剪切、复制、粘贴、删除等功能的可用性 + public void menuSelected(MenuEvent e)//閫夋嫨鏌愪釜鑿滃崟鏃惰皟鐢 + { checkMenuItemEnabled();//璁剧疆鍓垏銆佸鍒躲佺矘璐淬佸垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ } }); - menuItem_undo=new JMenuItem("撤销(U)"); + menuItem_undo=new JMenuItem("鎾ら攢"); menuItem_undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK)); menuItem_undo.addActionListener(this); menuItem_undo.setEnabled(false); - menuItem_cut=new JMenuItem("剪切(T)"); + menuItem_cut=new JMenuItem("鍓垏"); menuItem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK)); menuItem_cut.addActionListener(this); - menuItem_copy=new JMenuItem("复制(C)"); + menuItem_copy=new JMenuItem("澶嶅埗"); menuItem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); menuItem_copy.addActionListener(this); - menuItem_paste=new JMenuItem("粘贴(P)"); + menuItem_paste=new JMenuItem("绮樿创"); menuItem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); menuItem_paste.addActionListener(this); - menuItem_delete=new JMenuItem("删除(D)"); + menuItem_delete=new JMenuItem("鍒犻櫎"); menuItem_delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0)); menuItem_delete.addActionListener(this); - menuItem_find=new JMenuItem("查找(F)..."); + menuItem_find=new JMenuItem("鏌ユ壘"); menuItem_find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK)); menuItem_find.addActionListener(this); - menuItem_replace = new JMenuItem("替换(R)...",'R'); + menuItem_replace = new JMenuItem("鏇挎崲"); menuItem_replace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK)); menuItem_replace.addActionListener(this); - menuItem_selectall = new JMenuItem("全选",'A'); + menuItem_selectall = new JMenuItem("鍏ㄩ"); menuItem_selectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); menuItem_selectall.addActionListener(this); - menuItem_time = new JMenuItem("时间/日期(D)",'D'); + menuItem_time = new JMenuItem("鏃堕棿/鏃ユ湡"); menuItem_time.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0)); menuItem_time.addActionListener(this); - /*创建格式菜单及菜单项并注册事件监听*/ - menu_format=new JMenu("格式(O)"); - menu_format.setMnemonic('O');//设置快捷键ALT+O + /*鍒涘缓鏍煎紡鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚*/ + menu_format=new JMenu("鏍煎紡"); + menu_format.setMnemonic('O');//璁剧疆蹇嵎閿瓵LT+O - menuItem_linewrap=new JCheckBoxMenuItem("自动换行(W)"); - menuItem_linewrap.setMnemonic('W');//设置快捷键ALT+W + menuItem_linewrap=new JCheckBoxMenuItem("鑷姩鎹㈣"); + menuItem_linewrap.setMnemonic('W');//璁剧疆蹇嵎閿瓵LT+W menuItem_linewrap.setState(true); menuItem_linewrap.addActionListener(this); - menuItem_font=new JMenuItem("字体(F)..."); + menuItem_font=new JMenuItem("瀛椾綋"); menuItem_font.addActionListener(this); - //创建查看菜单及菜单项并注册事件监听 - menu_view=new JMenu("查看(V)"); - menu_view.setMnemonic('V');//设置快捷键ALT+V + //鍒涘缓鏌ョ湅鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚 + menu_view=new JMenu("鏌ョ湅"); + menu_view.setMnemonic('V');//璁剧疆蹇嵎閿瓵LT+V - menuItem_status=new JCheckBoxMenuItem("状态栏(S)"); - menuItem_status.setMnemonic('S');//设置快捷键ALT+S + menuItem_status=new JCheckBoxMenuItem("鐘舵佹爮"); + menuItem_status.setMnemonic('S');//璁剧疆蹇嵎閿瓵LT+S menuItem_status.setState(true); menuItem_status.addActionListener(this); - //创建帮助菜单及菜单项并注册事件监听 - menu_help = new JMenu("帮助(H)"); - menu_help.setMnemonic('H');//设置快捷键ALT+H + //鍒涘缓甯姪鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚 + menu_help = new JMenu("甯姪"); + menu_help.setMnemonic('H');//璁剧疆蹇嵎閿瓵LT+H - menuItem_HelpTopics = new JMenuItem("帮助主题(H)"); + menuItem_HelpTopics = new JMenuItem("甯姪涓婚"); menuItem_HelpTopics.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0)); menuItem_HelpTopics.addActionListener(this); - menuItem_AboutNotepad = new JMenuItem("关于记事本(A)"); - menuItem_AboutNotepad.addActionListener(this); + menuItem_AboutNoteBook = new JMenuItem("鍏充簬璁颁簨鏈"); + menuItem_AboutNoteBook.addActionListener(this); - //向菜单条添加"文件"菜单及菜单项 + /*鍚戣彍鍗曟潯娣诲姞"鏂囦欢"鑿滃崟鍙婅彍鍗曢」*/ menuBar.add(menu_file); menu_file.add(menuItem_new); menu_file.add(menuItem_open); @@ -207,7 +228,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener menu_file.add(menuItem_saveas); menu_file.add(menuItem_exit); - //向菜单条添加"编辑"菜单及菜单项 + /*鍚戣彍鍗曟潯娣诲姞"缂栬緫"鑿滃崟鍙婅彍鍗曢」 */ menuBar.add(menu_edit); menu_edit.add(menuItem_undo); menu_edit.add(menuItem_cut); @@ -219,39 +240,51 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener menu_edit.add(menuItem_selectall); menu_edit.add(menuItem_time); - //向菜单条添加"格式"菜单及菜单项 + /*鍚戣彍鍗曟潯娣诲姞"鏍煎紡"鑿滃崟鍙婅彍鍗曢」*/ menuBar.add(menu_format); menu_format.add(menuItem_linewrap); menu_format.add(menuItem_font); - //向菜单条添加"查看"菜单及菜单项 + /*鍚戣彍鍗曟潯娣诲姞"鏌ョ湅"鑿滃崟鍙婅彍鍗曢」 */ menuBar.add(menu_view); menu_view.add(menuItem_status); - //向菜单条添加"帮助"菜单及菜单项 + /*鍚戣彍鍗曟潯娣诲姞"甯姪"鑿滃崟鍙婅彍鍗曢」*/ menuBar.add(menu_help); menu_help.add(menuItem_HelpTopics); menu_help.addSeparator(); - menu_help.add(menuItem_AboutNotepad); + menu_help.add(menuItem_AboutNoteBook); - //向窗口添加菜单条 + /*鍚戠獥鍙f坊鍔犺彍鍗曟潯*/ this.setJMenuBar(menuBar); - - textArea=new JTextArea(100,100); + + //鍒涘缓鏂囨湰缂栬緫鍖哄苟娣诲姞婊氬姩鏉 + textArea=new JTextArea(20,50); JScrollPane scroller=new JScrollPane(textArea); - + scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + this.add(scroller,BorderLayout.CENTER);//鍚戠獥鍙f坊鍔犳枃鏈紪杈戝尯 + textArea.setBackground(doushalv); + textArea.setWrapStyleWord(true);//璁剧疆鍗曡瘝鍦ㄤ竴琛屼笉瓒冲绾虫椂鎹㈣ + textArea.setLineWrap(true);//璁剧疆鏂囨湰缂栬緫鍖鸿嚜鍔ㄦ崲琛岄粯璁や负true,鍗充細"鑷姩鎹㈣" + //this.add(editArea,BorderLayout.CENTER);//鍚戠獥鍙f坊鍔犳枃鏈紪杈戝尯 + oldValue=textArea.getText();//鑾峰彇鍘熸枃鏈紪杈戝尯鐨勫唴瀹 + + //缂栬緫鍖烘敞鍐屼簨浠剁洃鍚(涓庢挙閿鎿嶄綔鏈夊叧) + textArea.getDocument().addUndoableEditListener(undoHandler); + textArea.getDocument().addDocumentListener(this); + - //创建右键弹出菜单 + //鍒涘缓鍙抽敭寮瑰嚭鑿滃崟 popupMenu=new JPopupMenu(); - popupmenu_undo=new JMenuItem("撤销"); - popupmenu_cut=new JMenuItem("剪切"); - popupmenu_copy=new JMenuItem("复制"); - popupmenu_paste=new JMenuItem("粘帖"); - popupmenu_delete=new JMenuItem("删除"); - popupmenu_selectall=new JMenuItem("全选"); + popupmenu_undo=new JMenuItem("鎾ら攢"); + popupmenu_cut=new JMenuItem("鍓垏"); + popupmenu_copy=new JMenuItem("澶嶅埗"); + popupmenu_paste=new JMenuItem("绮樺笘"); + popupmenu_delete=new JMenuItem("鍒犻櫎"); + popupmenu_selectall=new JMenuItem("鍏ㄩ"); popupmenu_undo.setEnabled(false); - //向右键菜单添加菜单项和分隔符 + //鍚戝彸閿彍鍗曟坊鍔犺彍鍗曢」鍜屽垎闅旂 popupMenu.add(popupmenu_undo); popupMenu.addSeparator(); popupMenu.add(popupmenu_cut); @@ -261,7 +294,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener popupMenu.addSeparator(); popupMenu.add(popupmenu_selectall); - //文本编辑区注册右键菜单事件 + //鏂囨湰缂栬緫鍖烘敞鍐屽彸閿彍鍗曚簨浠 popupmenu_undo.addActionListener(this); popupmenu_cut.addActionListener(this); popupmenu_copy.addActionListener(this); @@ -269,53 +302,103 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener popupmenu_delete.addActionListener(this); popupmenu_selectall.addActionListener(this); - //文本编辑区注册右键菜单事件 + //鏂囨湰缂栬緫鍖烘敞鍐屽彸閿彍鍗曚簨浠 textArea.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) - { if(e.isPopupTrigger())//返回此鼠标事件是否为该平台的弹出菜单触发事件 - { popupMenu.show(e.getComponent(),e.getX(),e.getY());//在组件调用者的坐标空间中的位置 X、Y 显示弹出菜单 + { if(e.isPopupTrigger())//杩斿洖姝ら紶鏍囦簨浠舵槸鍚︿负璇ュ钩鍙扮殑寮瑰嚭鑿滃崟瑙﹀彂浜嬩欢 + { popupMenu.show(e.getComponent(),e.getX(),e.getY());//鍦ㄧ粍浠惰皟鐢ㄨ呯殑鍧愭爣绌洪棿涓殑浣嶇疆 X銆乊 鏄剧ず寮瑰嚭鑿滃崟 } - checkMenuItemEnabled();//设置剪切,复制,粘帖,删除等功能的可用性 - textArea.requestFocus();//编辑区获取焦点 + checkMenuItemEnabled();//璁剧疆鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ + textArea.requestFocus();//缂栬緫鍖鸿幏鍙栫劍鐐 } public void mouseReleased(MouseEvent e) - { if(e.isPopupTrigger())//返回此鼠标事件是否为该平台的弹出菜单触发事件 - { popupMenu.show(e.getComponent(),e.getX(),e.getY());//在组件调用者的坐标空间中的位置 X、Y 显示弹出菜单 + { if(e.isPopupTrigger())//杩斿洖姝ら紶鏍囦簨浠舵槸鍚︿负璇ュ钩鍙扮殑寮瑰嚭鑿滃崟瑙﹀彂浜嬩欢 + { popupMenu.show(e.getComponent(),e.getX(),e.getY());//鍦ㄧ粍浠惰皟鐢ㄨ呯殑鍧愭爣绌洪棿涓殑浣嶇疆 X銆乊 鏄剧ず寮瑰嚭鑿滃崟 } - checkMenuItemEnabled();//设置剪切,复制,粘帖,删除等功能的可用性 - textArea.requestFocus();//编辑区获取焦点 + checkMenuItemEnabled();//璁剧疆鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ + textArea.requestFocus();//缂栬緫鍖鸿幏鍙栫劍鐐 } - });//文本编辑区注册右键菜单事件结束 - - //创建和添加状态栏 + }); + + + + //鍒涘缓鍜屾坊鍔犵姸鎬佹爮 JToolBar statusLabel=new JToolBar(); statusLabel.setFloatable(false); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); - JLabel label = new JLabel("当前字数:0",statusBar_status,SwingConstants.CENTER); - JLabel labelTime = new JLabel("日期: "+sdf.format(new Date(0, 0, 0))); - JLabel labelCodeStyle = new JLabel("编码: "+codestyle); - statusLabel.add(label); + // Calendar calendar=Calendar.getInstance(); + //SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD"); + //JLabel label = new JLabel("褰撳墠瀛楁暟:",statusBar_status,SwingConstants.CENTER); + //labelTime = new JLabel("鏃ユ湡: "+sdf.format(new Date())); + JLabel labelCodeStyle = new JLabel("缂栫爜: "+codestyle); + //statusLabel.add(label); statusLabel.addSeparator(new Dimension(180,5)); - statusLabel.add(labelTime); + //statusLabel.add(labelTime); statusLabel.addSeparator(new Dimension(180,5)); statusLabel.add(labelCodeStyle); + this.add(statusLabel,BorderLayout.SOUTH);//鍚戠獥鍙f坊鍔犵姸鎬佹爮鏍囩 + this.setLocation(100,100);//璁剧疆绐楀彛鍦ㄥ睆骞曚笂鐨勪綅缃 + this.setSize(650,550);//璁剧疆绐楀彛鍦ㄥ睆骞曚笂鐨勫ぇ灏 + this.setVisible(true);//璁剧疆绐楀彛鍦ㄥ睆骞曚笂鐨勫彲瑙佹 +// +// 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(); // 涓鏃︽枃鏈湁鏀瑰彉灏辫缃悇鎸夐挳鐨勫彲鐢ㄦ +// } +// +// });} +// public void changeTextLengthStatus(){ // 鏂囨湰鐩戝惉 +// String content = textArea.getText().trim(); +// label.setText("褰撳墠瀛楁暟:"+content.length()); +// } +// +// private void isItemsAvalible(){ // 鐩戣鏂囨湰鍖哄苟璁剧疆鍚勫姛鑳介」鏄惁鍙敤 +// String content = textArea.getText(); +// if(content.equals("")){ +// menuItem_cut.setEnabled(false); +// menuItem_delete.setEnabled(false); +// menuItem_copy.setEnabled(false); +// menuItem_save.setEnabled(false); +// menuItem_saveas.setEnabled(false); +// popupmenu_copy.setEnabled(false); +// popupmenu_cut.setEnabled(false); +// }else{ +// menuItem_save.setEnabled(true); +// menuItem_cut.setEnabled(true); +// menuItem_delete.setEnabled(true); +// menuItem_copy.setEnabled(true); +// popupmenu_copy.setEnabled(true); +// popupmenu_cut.setEnabled(true); +// +// } +// +// - this.add(statusLabel,BorderLayout.SOUTH);//向窗口添加状态栏标签 - this.setLocation(100,100);//设置窗口在屏幕上的位置 - this.setSize(650,550);//设置窗口在屏幕上的大小 - this.setVisible(true);//设置窗口在屏幕上的可见性 - - /*添加窗口监听器*/ + /*娣诲姞绐楀彛鐩戝惉鍣*/ addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { exitWindowChoose(); } }); checkMenuItemEnabled(); - textArea.requestFocus(); - } - - //设置菜单项的可用性:剪切,复制,粘帖,删除功能 + textArea.requestFocus();} + + + + + + + //璁剧疆鑿滃崟椤圭殑鍙敤鎬э細鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ゅ姛鑳 public void checkMenuItemEnabled() { String selectText=textArea.getSelectedText(); if(selectText==null) @@ -334,7 +417,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener menuItem_delete.setEnabled(true); popupmenu_delete.setEnabled(true); } - /*粘帖功能可用性判断*/ + /*绮樺笘鍔熻兘鍙敤鎬у垽鏂*/ Transferable contents=clipBoard.getContents(this); if(contents==null) { menuItem_paste.setEnabled(false); @@ -345,8 +428,8 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener popupmenu_paste.setEnabled(true); } } - - /*关闭窗口时调用*/ + + /*鍏抽棴绐楀彛鏃惰皟鐢*/ public void exitWindowChoose() { textArea.requestFocus(); String currentValue=textArea.getText(); @@ -354,24 +437,28 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener { System.exit(0); } else - { int exitChoose=JOptionPane.showConfirmDialog(this,"你的文件还没保存哦:)是否保存?","退出!!",JOptionPane.YES_NO_CANCEL_OPTION); + { int exitChoose=JOptionPane.showConfirmDialog(this,"浣犵殑鏂囦欢杩樻病淇濆瓨鍝:)","閫鍑!!",JOptionPane.YES_NO_CANCEL_OPTION); if(exitChoose==JOptionPane.YES_OPTION) - { + { boolean isSave=false; if(isNewFile) { String str=null; JFileChooser fileChooser=new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - fileChooser.setApproveButtonText("!!!确定!!!"); - fileChooser.setDialogTitle("另存为:"); + fileChooser.setApproveButtonText("!!!纭畾!!!"); + fileChooser.setDialogTitle("鍙﹀瓨涓:"); - int result=fileChooser.showSaveDialog(this); - + int result=fileChooser.showSaveDialog(this); File saveFileName=fileChooser.getSelectedFile(); + if(result==JFileChooser.CANCEL_OPTION) + { statusLabel.setText("銆鎮ㄦ病鏈変繚瀛樻枃浠"); + return; + } + if(saveFileName==null||saveFileName.getName().equals("")) - { JOptionPane.showMessageDialog(this,"不合法的文件名!","不合法的文件名!",JOptionPane.ERROR_MESSAGE); + { JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚!","涓嶅悎娉曠殑鏂囦欢鍚!",JOptionPane.ERROR_MESSAGE); } else { try @@ -385,8 +472,9 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener currentFile=saveFileName; oldValue=textArea.getText(); - this.setTitle(saveFileName.getName()+" de记事本"); - //isSave=true; + this.setTitle(saveFileName.getName()+" de璁颁簨鏈"); + statusLabel.setText("銆褰撳墠鎵撳紑鏂囦欢:"+saveFileName.getAbsoluteFile()); + isSave=true; } catch(IOException ioException){ } @@ -400,13 +488,14 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener bfw.write(textArea.getText(),0,textArea.getText().length()); bfw.flush(); fw.close(); - + isSave=true; } catch(IOException ioException){ } } System.exit(0); - + if(isSave)System.exit(0); + else return; } else if(exitChoose==JOptionPane.NO_OPTION) { System.exit(0); @@ -417,8 +506,785 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener } } + /*鏌ユ壘鏂规硶*/ + public void find() + { final JDialog findDialog=new JDialog(this,"鏌ユ壘",false);//false鏃跺厑璁稿叾浠栫獥鍙e悓鏃跺浜庢縺娲荤姸鎬(鍗虫棤妯″紡) + Container con=findDialog.getContentPane();//杩斿洖姝ゅ璇濇鐨刢ontentPane瀵硅薄 + con.setLayout(new FlowLayout(FlowLayout.LEFT)); + JLabel findContentLabel=new JLabel("鏌ユ壘鍐呭锛"); + final JTextField findText=new JTextField(15); + JButton findNextButton=new JButton("鏌ユ壘涓嬩竴涓細"); + final JCheckBox matchCheckBox=new JCheckBox("鍖哄垎澶у皬鍐"); + ButtonGroup bGroup=new ButtonGroup(); + final JRadioButton upButton=new JRadioButton("寰涓"); + final JRadioButton downButton=new JRadioButton("寰涓"); + downButton.setSelected(true); + bGroup.add(upButton); + bGroup.add(downButton); + JButton cancel=new JButton("鍙栨秷");//鍙栨秷鎸夐挳浜嬩欢 + cancel.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { findDialog.dispose(); + } + }); + + //"鏌ユ壘涓嬩竴涓"鎸夐挳鐩戝惉 + findNextButton.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { //"鍖哄垎澶у皬鍐"鐨凧CheckBox鏄惁琚変腑 + int k=0,m=0; + final String str1,str2,str3,str4,strA,strB; + str1=textArea.getText(); + str2=findText.getText(); + str3=str1.toUpperCase(); + str4=str2.toUpperCase(); + if(matchCheckBox.isSelected())//鍖哄垎澶у皬鍐 + { strA=str1; + strB=str2; + } + else//涓嶅尯鍒嗗ぇ灏忓啓,姝ゆ椂鎶婃墍閫夊唴瀹瑰叏閮ㄥ寲鎴愬ぇ鍐(鎴栧皬鍐)锛屼互渚夸簬鏌ユ壘 + { strA=str3; + strB=str4; + } + if(upButton.isSelected()) + { + if(textArea.getSelectedText()==null) + k=strA.lastIndexOf(strB,textArea.getCaretPosition()-1); + else + k=strA.lastIndexOf(strB, textArea.getCaretPosition()-findText.getText().length()-1); + if(k>-1) + { + textArea.setCaretPosition(k); + textArea.select(k,k+strB.length()); + } + else + { JOptionPane.showMessageDialog(null,"鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹癸紒","鏌ユ壘",JOptionPane.INFORMATION_MESSAGE); + } + } + else if(downButton.isSelected()) + { if(textArea.getSelectedText()==null) + k=strA.indexOf(strB,textArea.getCaretPosition()+1); + else + k=strA.indexOf(strB, textArea.getCaretPosition()-findText.getText().length()+1); + if(k>-1) + { + textArea.setCaretPosition(k); + textArea.select(k,k+strB.length()); + } + else + { JOptionPane.showMessageDialog(null,"鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹癸紒","鏌ユ壘",JOptionPane.INFORMATION_MESSAGE); + } + } + } + }); + + /*鍒涘缓"鏌ユ壘"瀵硅瘽妗嗙殑鐣岄潰*/ + JPanel panel1=new JPanel(); + JPanel panel2=new JPanel(); + JPanel panel3=new JPanel(); + JPanel directionPanel=new JPanel(); + directionPanel.setBorder(BorderFactory.createTitledBorder("浣嶇疆")); //璁剧疆directionPanel缁勪欢鐨勮竟妗; + directionPanel.add(upButton); + directionPanel.add(downButton); + panel1.setLayout(new GridLayout(2,1)); + panel1.add(findNextButton); + panel1.add(cancel); + panel2.add(findContentLabel); + panel2.add(findText); + panel2.add(panel1); + panel3.add(matchCheckBox); + panel3.add(directionPanel); + con.add(panel2); + con.add(panel3); + findDialog.setSize(410,180); + findDialog.setResizable(false);//涓嶅彲璋冩暣澶у皬 + findDialog.setLocation(230,280); + findDialog.setVisible(true); + } + + /*鏇挎崲鏂规硶*/ + public void replace() + { final JDialog replaceDialog=new JDialog(this,"鏇挎崲",false);//false鏃跺厑璁稿叾浠栫獥鍙e悓鏃跺浜庢縺娲荤姸鎬(鍗虫棤妯″紡) + Container con=replaceDialog.getContentPane();//杩斿洖姝ゅ璇濇鐨刢ontentPane瀵硅薄 + con.setLayout(new FlowLayout(FlowLayout.CENTER)); + JLabel findContentLabel=new JLabel("鏌ユ壘鍐呭锛"); + final JTextField findText=new JTextField(15); + JButton findNextButton=new JButton("鏌ユ壘涓嬩竴涓:"); + JLabel replaceLabel=new JLabel("鏇挎崲涓猴細"); + final JTextField replaceText=new JTextField(15); + JButton replaceButton=new JButton("鏇挎崲"); + JButton replaceAllButton=new JButton("鍏ㄩ儴鏇挎崲"); + JButton cancel=new JButton("鍙栨秷"); + cancel.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { replaceDialog.dispose(); + } + }); + final JCheckBox matchCheckBox=new JCheckBox("鍖哄垎澶у皬鍐"); + ButtonGroup bGroup=new ButtonGroup(); + final JRadioButton upButton=new JRadioButton("寰涓"); + final JRadioButton downButton=new JRadioButton("寰涓"); + downButton.setSelected(true); + bGroup.add(upButton); + bGroup.add(downButton); + + + + /*"鏇挎崲"鎸夐挳鐩戝惉*/ + replaceButton.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { if(replaceText.getText().length()==0 && textArea.getSelectedText()!=null) + textArea.replaceSelection(""); + if(replaceText.getText().length()>0 && textArea.getSelectedText()!=null) + textArea.replaceSelection(replaceText.getText()); + } + });//"鏇挎崲"鎸夐挳鐩戝惉缁撴潫 + + /*"鍏ㄩ儴鏇挎崲"鎸夐挳鐩戝惉*/ + replaceAllButton.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { textArea.setCaretPosition(0); //灏嗗厜鏍囨斁鍒扮紪杈戝尯寮澶 + int k=0,m=0,replaceCount=0; + if(findText.getText().length()==0) + { JOptionPane.showMessageDialog(replaceDialog,"璇峰~鍐欐煡鎵惧唴瀹!","鎻愮ず",JOptionPane.WARNING_MESSAGE); + findText.requestFocus(true); + return; + } + while(k>-1)//褰撴枃鏈腑鏈夊唴瀹硅閫変腑鏃(k>-1琚変腑)杩涜鏇挎崲锛屽惁鍒欎笉杩涜while寰幆 + { + final String str1,str2,str3,str4,strA,strB; + str1=textArea.getText(); + str2=findText.getText(); + str3=str1.toUpperCase(); + str4=str2.toUpperCase(); + if(matchCheckBox.isSelected())//鍖哄垎澶у皬鍐 + { strA=str1; + strB=str2; + } + else//涓嶅尯鍒嗗ぇ灏忓啓,姝ゆ椂鎶婃墍閫夊唴瀹瑰叏閮ㄥ寲鎴愬ぇ鍐(鎴栧皬鍐)锛屼互渚夸簬鏌ユ壘 + { strA=str3; + strB=str4; + } + if(upButton.isSelected()) + { + if(textArea.getSelectedText()==null) + k=strA.lastIndexOf(strB,textArea.getCaretPosition()-1); + else + k=strA.lastIndexOf(strB, textArea.getCaretPosition()-findText.getText().length()-1); + if(k>-1) + { + textArea.setCaretPosition(k); + textArea.select(k,k+strB.length()); + } + else + { if(replaceCount==0) + { JOptionPane.showMessageDialog(replaceDialog, "鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹!", "璁颁簨鏈",JOptionPane.INFORMATION_MESSAGE); + } + else + { JOptionPane.showMessageDialog(replaceDialog,"鎴愬姛鏇挎崲"+replaceCount+"涓尮閰嶅唴瀹!","鏇挎崲鎴愬姛",JOptionPane.INFORMATION_MESSAGE); + } + } + } + else if(downButton.isSelected()) + { if(textArea.getSelectedText()==null) + k=strA.indexOf(strB,textArea.getCaretPosition()+1); + else + k=strA.indexOf(strB, textArea.getCaretPosition()-findText.getText().length()+1); + if(k>-1) + { + textArea.setCaretPosition(k); + textArea.select(k,k+strB.length()); + } + else + { if(replaceCount==0) + { JOptionPane.showMessageDialog(replaceDialog, "鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹!", "璁颁簨鏈",JOptionPane.INFORMATION_MESSAGE); + } + else + { JOptionPane.showMessageDialog(replaceDialog,"鎴愬姛鏇挎崲(鈮р垏鈮)"+replaceCount+"涓尮閰嶅唴瀹!","鏇挎崲鎴愬姛",JOptionPane.INFORMATION_MESSAGE); + } + } + } + if(replaceText.getText().length()==0 && textArea.getSelectedText()!= null) + { textArea.replaceSelection(""); + replaceCount++; + } + + if(replaceText.getText().length()>0 && textArea.getSelectedText()!= null) + { textArea.replaceSelection(replaceText.getText()); + replaceCount++; + } + } + } + }); + + /*鍒涘缓"鏇挎崲"瀵硅瘽妗嗙殑鐣岄潰*/ + JPanel directionPanel=new JPanel(); + directionPanel.setBorder(BorderFactory.createTitledBorder("浣嶇疆"));//璁剧疆directionPanel缁勪欢鐨勮竟妗; + directionPanel.add(upButton); + directionPanel.add(downButton); + JPanel panel1=new JPanel(); + JPanel panel2=new JPanel(); + JPanel panel3=new JPanel(); + JPanel panel4=new JPanel(); + panel4.setLayout(new GridLayout(2,1)); + panel1.add(findContentLabel); + panel1.add(findText); + panel1.add(findNextButton); + panel4.add(replaceButton); + panel4.add(replaceAllButton); + panel2.add(replaceLabel); + panel2.add(replaceText); + panel2.add(panel4); + panel3.add(matchCheckBox); + panel3.add(directionPanel); + panel3.add(cancel); + con.add(panel1); + con.add(panel2); + con.add(panel3); + replaceDialog.setSize(420,220); + replaceDialog.setResizable(false);//涓嶅彲璋冩暣澶у皬 + replaceDialog.setLocation(230,280); + replaceDialog.setVisible(true); + } + + /*"瀛椾綋"鏂规硶*/ + + public void font() + { final JDialog fontDialog=new JDialog(this,"瀛椾綋璁剧疆",false); + Container con=fontDialog.getContentPane(); + con.setLayout(new FlowLayout(FlowLayout.LEFT)); + JLabel fontLabel=new JLabel("瀛椾綋锛"); + fontLabel.setPreferredSize(new Dimension(100,20));//鏋勯犱竴涓狣imension锛屽苟灏嗗叾鍒濆鍖栦负鎸囧畾瀹藉害鍜岄珮搴 + JLabel styleLabel=new JLabel("瀛楀舰锛"); + styleLabel.setPreferredSize(new Dimension(100,20)); + JLabel sizeLabel=new JLabel("澶у皬锛"); + sizeLabel.setPreferredSize(new Dimension(100,20)); + final JLabel sample=new JLabel("鏄綘鎯宠鐨勫瓧浣撳悧锛(*^锛燸*)"); + sample.setHorizontalAlignment(SwingConstants.CENTER); + final JTextField fontText=new JTextField(9); + fontText.setPreferredSize(new Dimension(200,20)); + final JTextField styleText=new JTextField(8); + styleText.setPreferredSize(new Dimension(200,20)); + final int style[]={Font.PLAIN,Font.BOLD,Font.ITALIC,Font.BOLD+Font.ITALIC}; + final JTextField sizeText=new JTextField(5); + sizeText.setPreferredSize(new Dimension(200,20)); + JButton okButton=new JButton("纭畾"); + JButton cancel=new JButton("鍙栨秷"); + cancel.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { fontDialog.dispose(); + } + }); + Font currentFont=textArea.getFont(); + fontText.setText(currentFont.getFontName()); + fontText.selectAll(); + if(currentFont.getStyle()==Font.PLAIN) + styleText.setText("甯歌"); + else if(currentFont.getStyle()==Font.BOLD) + styleText.setText("绮椾綋"); + else if(currentFont.getStyle()==Font.ITALIC) + styleText.setText("鏂滀綋"); + else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC)) + styleText.setText("绮楁枩浣"); + styleText.selectAll(); + String str=String.valueOf(currentFont.getSize()); + sizeText.setText(str); + sizeText.selectAll(); + final JList fontList,styleList,sizeList; + GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); + final String fontName[]=ge.getAvailableFontFamilyNames(); + fontList=new JList(fontName); + fontList.setFixedCellWidth(86); + fontList.setFixedCellHeight(20); + fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + final String fontStyle[]={"甯歌","绮椾綋","鏂滀綋","绮楁枩浣"}; + styleList=new JList(fontStyle); + styleList.setFixedCellWidth(86); + styleList.setFixedCellHeight(20); + styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + + if(currentFont.getStyle()==Font.PLAIN) + styleList.setSelectedIndex(0); + else if(currentFont.getStyle()==Font.BOLD) + styleList.setSelectedIndex(1); + else if(currentFont.getStyle()==Font.ITALIC) + styleList.setSelectedIndex(2); + else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC)) + styleList.setSelectedIndex(3); + final String fontSize[]={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"}; + sizeList=new JList(fontSize); + sizeList.setFixedCellWidth(43); + sizeList.setFixedCellHeight(20); + sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + + fontList.addListSelectionListener(new ListSelectionListener() + { public void valueChanged(ListSelectionEvent event) + { fontText.setText(fontName[fontList.getSelectedIndex()]); + fontText.selectAll(); + Font sampleFont1=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); + sample.setFont(sampleFont1); + } + }); + + styleList.addListSelectionListener(new ListSelectionListener() + { public void valueChanged(ListSelectionEvent event) + { int s=style[styleList.getSelectedIndex()]; + styleText.setText(fontStyle[s]); + styleText.selectAll(); + Font sampleFont2=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); + sample.setFont(sampleFont2); + } + }); + + sizeList.addListSelectionListener(new ListSelectionListener() + { public void valueChanged(ListSelectionEvent event) + { sizeText.setText(fontSize[sizeList.getSelectedIndex()]); + sizeText.requestFocus(); + sizeText.selectAll(); + Font sampleFont3=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); + sample.setFont(sampleFont3); + } + }); + okButton.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { Font okFont=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); + textArea.setFont(okFont); + fontDialog.dispose(); + } + }); + JPanel samplePanel=new JPanel(); + samplePanel.setBorder(BorderFactory.createTitledBorder("绀轰緥")); + samplePanel.setLayout(new FlowLayout(FlowLayout.CENTER)); + samplePanel.add(sample); + JPanel panel1=new JPanel(); + JPanel panel2=new JPanel(); + JPanel panel3=new JPanel(); + panel2.add(fontText); + panel2.add(styleText); + panel2.add(sizeText); + panel2.add(okButton); + panel3.add(new JScrollPane(fontList));//JList涓嶆敮鎸佺洿鎺ユ粴鍔紝鎵浠ヨ璁㎎List浣滀负JScrollPane鐨勮鍙h鍥 + panel3.add(new JScrollPane(styleList)); + panel3.add(new JScrollPane(sizeList)); + panel3.add(cancel); + con.add(panel1); + con.add(panel2); + con.add(panel3); + con.add(samplePanel); + fontDialog.setSize(350,340); + fontDialog.setLocation(200,200); + fontDialog.setResizable(false); + fontDialog.setVisible(true); + } + + public void actionPerformed(ActionEvent e) + { //鏂板缓 + if(e.getSource()==menuItem_new) + { textArea.requestFocus(); + String currentValue=textArea.getText(); + boolean isTextChange=(currentValue.equals(oldValue))?false:true; + if(isTextChange) + { int saveChoose=JOptionPane.showConfirmDialog(this,"鎮ㄧ殑鏂囦欢灏氭湭淇濆瓨锛屾槸鍚︿繚瀛橈紵","鎻愮ず",JOptionPane.YES_NO_CANCEL_OPTION); + if(saveChoose==JOptionPane.YES_OPTION) + { String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setDialogTitle("鍙﹀瓨涓"); + int result=fileChooser.showSaveDialog(this); + File saveFileName=fileChooser.getSelectedFile(); + if(saveFileName==null || saveFileName.getName().equals("")) + { JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileWriter fw=new FileWriter(saveFileName); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush(); + bfw.close(); + isNewFile=false; + currentFile=saveFileName; + oldValue=textArea.getText(); + this.setTitle(saveFileName.getName()+" de 璁颁簨鏈"); + statusLabel.setText("褰撳墠鎵撳紑鏂囦欢锛"+saveFileName.getAbsoluteFile()); + } + catch (IOException ioException) + { + } + } + } + else if(saveChoose==JOptionPane.NO_OPTION) + { textArea.replaceRange("",0,textArea.getText().length()); + statusLabel.setText(" 鏂板缓鏂囦欢"); + this.setTitle("鏃犳爣棰 de璁颁簨鏈"); + isNewFile=true; + undo.discardAllEdits(); //鎾ゆ秷鎵鏈夌殑"鎾ゆ秷"鎿嶄綔 + menuItem_undo.setEnabled(false); + oldValue=textArea.getText(); + } + else if(saveChoose==JOptionPane.CANCEL_OPTION) + { return; + } + } + else + { textArea.replaceRange("",0,textArea.getText().length()); + statusLabel.setText(" 鏂板缓鏂囦欢"); + this.setTitle("鏃犳爣棰 - 璁颁簨鏈"); + isNewFile=true; + undo.discardAllEdits();//鎾ゆ秷鎵鏈夌殑"鎾ゆ秷"鎿嶄綔 + menuItem_undo.setEnabled(false); + oldValue=textArea.getText(); + } + }//鏂板缓缁撴潫 + //鎵撳紑 + else if(e.getSource()==menuItem_open) + { textArea.requestFocus(); + String currentValue=textArea.getText(); + boolean isTextChange=(currentValue.equals(oldValue))?false:true; + if(isTextChange) + { int saveChoose=JOptionPane.showConfirmDialog(this,"鎮ㄧ殑鏂囦欢灏氭湭淇濆瓨:(","鎻愮ず",JOptionPane.YES_NO_CANCEL_OPTION); + if(saveChoose==JOptionPane.YES_OPTION) + { String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setDialogTitle("鍙﹀瓨涓"); + int result=fileChooser.showSaveDialog(this); + File saveFileName=fileChooser.getSelectedFile(); + if(saveFileName==null || saveFileName.getName().equals("")) + { + statusLabel.setText("鎮ㄦ病鏈夐夋嫨浠讳綍鏂囦欢"); + JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileWriter fw=new FileWriter(saveFileName); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush();//鍒锋柊璇ユ祦鐨勭紦鍐 + bfw.close(); + isNewFile=false; + currentFile=saveFileName; + oldValue=textArea.getText(); + this.setTitle(saveFileName.getName()+" de璁颁簨鏈"); + statusLabel.setText("褰撳墠鎵撳紑鏂囦欢锛"+saveFileName.getAbsoluteFile()); + } + catch (IOException ioException) + { + } + } + } + else if(saveChoose==JOptionPane.NO_OPTION) + { String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setDialogTitle("鎵撳紑鏂囦欢"); + int result=fileChooser.showOpenDialog(this); + File fileName=fileChooser.getSelectedFile(); + if(fileName==null || fileName.getName().equals("")){ + statusLabel.setText("鎮ㄦ病鏈夐夋嫨浠讳綍鏂囦欢"); + JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileReader fr=new FileReader(fileName); + BufferedReader bfr=new BufferedReader(fr); + textArea.setText(""); + while((str=bfr.readLine())!=null) + { textArea.append(str); + } + this.setTitle(fileName.getName()+" de 璁颁簨鏈"); + statusLabel.setText(" 褰撳墠鎵撳紑鏂囦欢锛"+fileName.getAbsoluteFile()); + fr.close(); + isNewFile=false; + currentFile=fileName; + oldValue=textArea.getText(); + } + catch (IOException ioException) + { + } + } + } + else + { return; + } + } + else + { String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setDialogTitle("鎵撳紑鏂囦欢"); + int result=fileChooser.showOpenDialog(this); + File fileName=fileChooser.getSelectedFile(); + if(fileName==null || fileName.getName().equals("")) + { JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileReader fr=new FileReader(fileName); + BufferedReader bfr=new BufferedReader(fr); + textArea.setText(""); + while((str=bfr.readLine())!=null) + { textArea.append(str); + } + this.setTitle(fileName.getName()+" de 璁颁簨鏈"); + statusLabel.setText(" 褰撳墠鎵撳紑鏂囦欢锛"+fileName.getAbsoluteFile()); + fr.close(); + isNewFile=false; + currentFile=fileName; + oldValue=textArea.getText(); + } + catch (IOException ioException) + { + } + } + } + } + + /*淇濆瓨*/ + else if(e.getSource()==menuItem_save) + { textArea.requestFocus(); + if(isNewFile) + { String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + + fileChooser.setDialogTitle("淇濆瓨"); + int result=fileChooser.showSaveDialog(this); + if(result==JFileChooser.CANCEL_OPTION) + { statusLabel.setText("鎮ㄦ病鏈夐夋嫨浠讳綍鏂囦欢"); + return; + } + + File saveFileName=fileChooser.getSelectedFile(); + if(saveFileName==null || saveFileName.getName().equals("")) + { JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileWriter fw=new FileWriter(saveFileName); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush();//鍒锋柊璇ユ祦鐨勭紦鍐 + bfw.close(); + isNewFile=false; + currentFile=saveFileName; + oldValue=textArea.getText(); + this.setTitle(saveFileName.getName()+" - 璁颁簨鏈"); + statusLabel.setText("褰撳墠鎵撳紑鏂囦欢锛"+saveFileName.getAbsoluteFile()); + } + catch (IOException ioException) + { + } + } + } + else + { try + { FileWriter fw=new FileWriter(currentFile); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush(); + fw.close(); + } + catch(IOException ioException) + { + } + } + } + + /*鍙﹀瓨涓*/ + else if(e.getSource()==menuItem_saveas) + { textArea.requestFocus(); + String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + + fileChooser.setDialogTitle("鍙﹀瓨涓"); + int result=fileChooser.showSaveDialog(this); + if(result==JFileChooser.CANCEL_OPTION) + { statusLabel.setText("銆鎮ㄦ病鏈夐夋嫨浠讳綍鏂囦欢"); + return; + } + File saveFileName=fileChooser.getSelectedFile(); + if(saveFileName==null||saveFileName.getName().equals("")) + { JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileWriter fw=new FileWriter(saveFileName); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush(); + fw.close(); + oldValue=textArea.getText(); + this.setTitle(saveFileName.getName()+" de璁颁簨鏈"); + statusLabel.setText("銆褰撳墠鎵撳紑鏂囦欢:"+saveFileName.getAbsoluteFile()); + + } + catch(IOException ioException) + { + } + } + } + + /*閫鍑*/ + else if(e.getSource()==menuItem_exit) + { int exitChoose=JOptionPane.showConfirmDialog(this,"浣犵湡鐨勭‘瀹氳閫鍑哄悧?淇濆瓨浜嗗悧","閫鍑烘彁绀",JOptionPane.OK_CANCEL_OPTION); + if(exitChoose==JOptionPane.OK_OPTION) + { System.exit(0); + } + else + { return; + } + } + /*鎾ら攢*/ + else if(e.getSource()==menuItem_undo || e.getSource()==popupmenu_undo) + { textArea.requestFocus(); + if(undo.canUndo()) + { try + { undo.undo(); + } + catch (CannotUndoException ex) + { System.out.println("Unable to undo:" + ex); + + } + } + if(!undo.canUndo()) + { menuItem_undo.setEnabled(false); + } + } + + /*鍓垏*/ + else if(e.getSource()==menuItem_cut || e.getSource()==popupmenu_cut) + { textArea.requestFocus(); + String text=textArea.getSelectedText(); + StringSelection selection=new StringSelection(text); + clipBoard.setContents(selection,null); + textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd()); + checkMenuItemEnabled();//璁剧疆鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ゅ姛鑳界殑鍙敤鎬 + } + + /*澶嶅埗*/ + else if(e.getSource()==menuItem_copy || e.getSource()==popupmenu_copy) + { textArea.requestFocus(); + String text=textArea.getSelectedText(); + StringSelection selection=new StringSelection(text); + clipBoard.setContents(selection,null); + checkMenuItemEnabled();//璁剧疆鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ゅ姛鑳界殑鍙敤鎬 + } + + /*绮樺笘*/ + else if(e.getSource()==menuItem_paste || e.getSource()==popupmenu_paste) + { textArea.requestFocus(); + Transferable contents=clipBoard.getContents(this); + if(contents==null)return; + String text=""; + try + { text=(String)contents.getTransferData(DataFlavor.stringFlavor); + } + catch (Exception exception) + { + } + textArea.replaceRange(text,textArea.getSelectionStart(),textArea.getSelectionEnd()); + checkMenuItemEnabled(); + } + + /*鍒犻櫎*/ + else if(e.getSource()==menuItem_delete || e.getSource()==popupmenu_delete) + { textArea.requestFocus(); + textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd()); + checkMenuItemEnabled(); //璁剧疆鍓垏銆佸鍒躲佺矘璐淬佸垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ + } + + /*鏌ユ壘*/ + else if(e.getSource()==menuItem_find) + { textArea.requestFocus(); + find(); + } + + /*鏇挎崲*/ + else if(e.getSource()==menuItem_replace) + { textArea.requestFocus(); + replace(); + }//鏇挎崲缁撴潫 + + /*鏃堕棿鏃ユ湡*/ + else if(e.getSource()==menuItem_time) + { textArea.requestFocus(); +// SimpleDateFormat currentDateTime=new SimpleDateFormat(" yyyy/MM/dd HH:mm:ss "); +// textArea.insert(currentDateTime.format(new Date()),textArea.getCaretPosition()); + Calendar rightNow=Calendar.getInstance(); + Date date=(Date) rightNow.getTime(); + textArea.insert(date.toString(),textArea.getCaretPosition()); + + } + + /*鍏ㄩ*/ + else if(e.getSource()==menuItem_selectall || e.getSource()==popupmenu_selectall) + { textArea.selectAll(); + } + + /*鑷姩鎹㈣*/ + else if(e.getSource()==menuItem_linewrap) + { if(menuItem_linewrap.getState()) + textArea.setLineWrap(true); + else + textArea.setLineWrap(false); + + } + + /*瀛椾綋璁剧疆*/ + else if(e.getSource()==menuItem_font) + { textArea.requestFocus(); + font(); + } + + /*甯姪*/ + else if(e.getSource()==menuItem_HelpTopics) + { textArea.requestFocus(); + JOptionPane.showMessageDialog(this,"鍔姏锛侊紒","甯姪涓婚",JOptionPane.INFORMATION_MESSAGE); + } + + /*鍏充簬*/ + else if(e.getSource()==menuItem_AboutNoteBook) + { textArea.requestFocus(); + JOptionPane.showMessageDialog(this, + "鍔犳补锛侊紒", + "璁颁簨鏈",JOptionPane.INFORMATION_MESSAGE); + } + } + + /*瀹炵幇DocumentListener鎺ュ彛涓殑鏂规硶*/ + public void removeUpdate(DocumentEvent e) + { menuItem_undo.setEnabled(true); + } + public void insertUpdate(DocumentEvent e) + { menuItem_undo.setEnabled(true); + } + public void changedUpdate(DocumentEvent e) + { menuItem_undo.setEnabled(true); + } + + + /*瀹炵幇鎺ュ彛UndoableEditListener鐨勭被UndoHandler*/ + class UndoHandler implements UndoableEditListener + { public void undoableEditHappened(UndoableEditEvent uee) + { undo.addEdit(uee.getEdit()); + } + } + + + + public static void main(String args[]) + { Notebook notebook=new Notebook(); + notebook.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//浣跨敤 System exit 鏂规硶閫鍑哄簲鐢ㄧ▼搴 + } + } + + + + + - }} -- Gitee From 7eb82a18d8f024a27668dac52ac91dc377c0ecbe Mon Sep 17 00:00:00 2001 From: lenovo Date: Thu, 10 Jun 2021 12:06:23 +0800 Subject: [PATCH 07/19] =?UTF-8?q?=E4=B8=B0=E5=AF=8C=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/TimeNoteBook/Notebook.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/TimeNoteBook/Notebook.java b/src/TimeNoteBook/Notebook.java index bae6d28..3848f5b 100644 --- a/src/TimeNoteBook/Notebook.java +++ b/src/TimeNoteBook/Notebook.java @@ -1243,15 +1243,15 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener /*甯姪*/ else if(e.getSource()==menuItem_HelpTopics) { textArea.requestFocus(); - JOptionPane.showMessageDialog(this,"鍔姏锛侊紒","甯姪涓婚",JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(this,"鍔姏锛侊紒","鏌ョ湅甯姪",JOptionPane.INFORMATION_MESSAGE); } /*鍏充簬*/ else if(e.getSource()==menuItem_AboutNoteBook) { textArea.requestFocus(); JOptionPane.showMessageDialog(this, - "鍔犳补锛侊紒", - "璁颁簨鏈",JOptionPane.INFORMATION_MESSAGE); + "鏈浜嬫湰鍙互*******", + "鍏充簬",JOptionPane.INFORMATION_MESSAGE); } } -- Gitee From 83ff982f5b674c9ef6a12f11cb6a79e61622dcd9 Mon Sep 17 00:00:00 2001 From: lenovo Date: Thu, 10 Jun 2021 15:11:14 +0800 Subject: [PATCH 08/19] =?UTF-8?q?=E4=B8=B0=E5=AF=8C=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/TimeNoteBook/Notebook.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/TimeNoteBook/Notebook.java b/src/TimeNoteBook/Notebook.java index 3848f5b..85da80f 100644 --- a/src/TimeNoteBook/Notebook.java +++ b/src/TimeNoteBook/Notebook.java @@ -1215,9 +1215,8 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener // SimpleDateFormat currentDateTime=new SimpleDateFormat(" yyyy/MM/dd HH:mm:ss "); // textArea.insert(currentDateTime.format(new Date()),textArea.getCaretPosition()); Calendar rightNow=Calendar.getInstance(); - Date date=(Date) rightNow.getTime(); + java.util.Date date=rightNow.getTime(); textArea.insert(date.toString(),textArea.getCaretPosition()); - } /*鍏ㄩ*/ -- Gitee From 4647d6a35d22e59a81fd8e5b84babb76194d475f Mon Sep 17 00:00:00 2001 From: lenovo Date: Thu, 10 Jun 2021 15:18:07 +0800 Subject: [PATCH 09/19] =?UTF-8?q?=E4=B8=B0=E5=AF=8C=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/TimeNoteBook/Notebook.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/TimeNoteBook/Notebook.java b/src/TimeNoteBook/Notebook.java index 85da80f..7c97b13 100644 --- a/src/TimeNoteBook/Notebook.java +++ b/src/TimeNoteBook/Notebook.java @@ -446,7 +446,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener JFileChooser fileChooser=new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setApproveButtonText("!!!纭畾!!!"); - fileChooser.setDialogTitle("鍙﹀瓨涓:"); + fileChooser.setDialogTitle("鍙﹀瓨涓"); int result=fileChooser.showSaveDialog(this); @@ -513,7 +513,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener con.setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel findContentLabel=new JLabel("鏌ユ壘鍐呭锛"); final JTextField findText=new JTextField(15); - JButton findNextButton=new JButton("鏌ユ壘涓嬩竴涓細"); + JButton findNextButton=new JButton("鏌ユ壘"); final JCheckBox matchCheckBox=new JCheckBox("鍖哄垎澶у皬鍐"); ButtonGroup bGroup=new ButtonGroup(); final JRadioButton upButton=new JRadioButton("寰涓"); @@ -558,7 +558,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener textArea.select(k,k+strB.length()); } else - { JOptionPane.showMessageDialog(null,"鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹癸紒","鏌ユ壘",JOptionPane.INFORMATION_MESSAGE); + { JOptionPane.showMessageDialog(null,"鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹癸紒:(","鏌ユ壘",JOptionPane.INFORMATION_MESSAGE); } } else if(downButton.isSelected()) @@ -572,7 +572,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener textArea.select(k,k+strB.length()); } else - { JOptionPane.showMessageDialog(null,"鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹癸紒","鏌ユ壘",JOptionPane.INFORMATION_MESSAGE); + { JOptionPane.showMessageDialog(null,"鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹癸紒:(","鏌ユ壘",JOptionPane.INFORMATION_MESSAGE); } } } @@ -609,7 +609,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener con.setLayout(new FlowLayout(FlowLayout.CENTER)); JLabel findContentLabel=new JLabel("鏌ユ壘鍐呭锛"); final JTextField findText=new JTextField(15); - JButton findNextButton=new JButton("鏌ユ壘涓嬩竴涓:"); + JButton findNextButton=new JButton("鏌ユ壘"); JLabel replaceLabel=new JLabel("鏇挎崲涓猴細"); final JTextField replaceText=new JTextField(15); JButton replaceButton=new JButton("鏇挎崲"); @@ -681,7 +681,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener { JOptionPane.showMessageDialog(replaceDialog, "鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹!", "璁颁簨鏈",JOptionPane.INFORMATION_MESSAGE); } else - { JOptionPane.showMessageDialog(replaceDialog,"鎴愬姛鏇挎崲"+replaceCount+"涓尮閰嶅唴瀹!","鏇挎崲鎴愬姛",JOptionPane.INFORMATION_MESSAGE); + { JOptionPane.showMessageDialog(replaceDialog,"鎴愬姛鏇挎崲"+replaceCount+"涓尮閰嶅唴瀹!","鏇挎崲鎴愬姛(鈮р垏鈮)",JOptionPane.INFORMATION_MESSAGE); } } } @@ -697,10 +697,10 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener } else { if(replaceCount==0) - { JOptionPane.showMessageDialog(replaceDialog, "鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹!", "璁颁簨鏈",JOptionPane.INFORMATION_MESSAGE); + { JOptionPane.showMessageDialog(replaceDialog, "鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹!:(", "璁颁簨鏈",JOptionPane.INFORMATION_MESSAGE); } else - { JOptionPane.showMessageDialog(replaceDialog,"鎴愬姛鏇挎崲(鈮р垏鈮)"+replaceCount+"涓尮閰嶅唴瀹!","鏇挎崲鎴愬姛",JOptionPane.INFORMATION_MESSAGE); + { JOptionPane.showMessageDialog(replaceDialog,"鎴愬姛鏇挎崲"+replaceCount+"涓尮閰嶅唴瀹!","鏇挎崲鎴愬姛(鈮р垏鈮)",JOptionPane.INFORMATION_MESSAGE); } } } -- Gitee From d152698f0cf20d00b08d31ddc9bf19e5ade2873e Mon Sep 17 00:00:00 2001 From: lenovo Date: Fri, 11 Jun 2021 08:29:10 +0800 Subject: [PATCH 10/19] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=AE=BE=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/TimeNoteBook/Notebook.java | 209 +++++++++++++++++++++------------ 1 file changed, 137 insertions(+), 72 deletions(-) diff --git a/src/TimeNoteBook/Notebook.java b/src/TimeNoteBook/Notebook.java index 7c97b13..277599d 100644 --- a/src/TimeNoteBook/Notebook.java +++ b/src/TimeNoteBook/Notebook.java @@ -5,6 +5,7 @@ import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; +import java.io.Serializable ; import javax.swing.*; import javax.swing.event.DocumentEvent; @@ -18,9 +19,10 @@ import javax.swing.event.UndoableEditListener; import javax.swing.undo.CannotUndoException; import javax.swing.undo.UndoManager; import java.io.*; -import java.sql.Date; + import java.text.SimpleDateFormat; import java.util.Calendar; +import java.util.Date; import java.util.Enumeration; import javax.swing.*; @@ -29,7 +31,7 @@ import java.beans.EventHandler; public class Notebook extends JFrame implements DocumentListener,ActionListener { private static Icon statusBar_status; - private static String codestyle = "UTF-8"; + public static final Color doushalv = new Color(199,237,204);//鏂板缓棰滆壊涓烘姢鐪艰眴娌欑豢 /*鑿滃崟*/ @@ -38,6 +40,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener private JMenu menu_format; private JMenu menu_help; private JMenu menu_view; + private JMenu menu_settime; /*鍙抽敭寮瑰嚭鑿滃崟*/ private JPopupMenu popupMenu; @@ -77,10 +80,15 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener private JMenuItem menuItem_HelpTopics; private JMenuItem menuItem_AboutNoteBook; + /*瀹氭椂鍣*/ + private JMenuItem settime; + /*鏂囨湰缂栬緫鍖哄煙*/ private JTextArea textArea; + /*鐘舵佹爮*/ private JLabel statusLabel; + /*鑿滃崟*/ private JMenuBar menuBar; private JLabel labelTime; @@ -98,7 +106,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener //褰撳墠鏂囦欢鍚 public Notebook(){ - super("鏃堕棿璁颁簨鏈"); + super("鎶ょ溂璁颁簨鏈"); Font font=new Font("Courier Prime", Font.PLAIN, 12); //鏀瑰彉绯荤粺榛樿瀛椾綋 Enumeration keys = UIManager.getDefaults().keys(); while (keys.hasMoreElements()) { @@ -200,7 +208,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener menuItem_font=new JMenuItem("瀛椾綋"); menuItem_font.addActionListener(this); - //鍒涘缓鏌ョ湅鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚 + /*鍒涘缓鏌ョ湅鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚*/ menu_view=new JMenu("鏌ョ湅"); menu_view.setMnemonic('V');//璁剧疆蹇嵎閿瓵LT+V @@ -209,7 +217,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener menuItem_status.setState(true); menuItem_status.addActionListener(this); - //鍒涘缓甯姪鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚 + /*鍒涘缓甯姪鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚*/ menu_help = new JMenu("甯姪"); menu_help.setMnemonic('H');//璁剧疆蹇嵎閿瓵LT+H @@ -219,6 +227,11 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener menuItem_AboutNoteBook = new JMenuItem("鍏充簬璁颁簨鏈"); menuItem_AboutNoteBook.addActionListener(this); + + /*瀹氭椂鍣*/ + menu_settime=new JMenu ("瀹氭椂鍣"); + settime=new JMenuItem("瀹氭椂鍣"); + settime.addActionListener(this); /*鍚戣彍鍗曟潯娣诲姞"鏂囦欢"鑿滃崟鍙婅彍鍗曢」*/ menuBar.add(menu_file); @@ -254,11 +267,15 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener menu_help.add(menuItem_HelpTopics); menu_help.addSeparator(); menu_help.add(menuItem_AboutNoteBook); + + /*鍚戣彍鍗曟潯娣诲姞鈥滃畾鏃跺櫒鈥濊彍鍗*/ + menuBar.add(menu_settime); + menu_settime.add(settime); /*鍚戠獥鍙f坊鍔犺彍鍗曟潯*/ this.setJMenuBar(menuBar); - //鍒涘缓鏂囨湰缂栬緫鍖哄苟娣诲姞婊氬姩鏉 + /*鍒涘缓鏂囨湰缂栬緫鍖哄苟娣诲姞婊氬姩鏉*/ textArea=new JTextArea(20,50); JScrollPane scroller=new JScrollPane(textArea); scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); @@ -266,15 +283,14 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener textArea.setBackground(doushalv); textArea.setWrapStyleWord(true);//璁剧疆鍗曡瘝鍦ㄤ竴琛屼笉瓒冲绾虫椂鎹㈣ textArea.setLineWrap(true);//璁剧疆鏂囨湰缂栬緫鍖鸿嚜鍔ㄦ崲琛岄粯璁や负true,鍗充細"鑷姩鎹㈣" - //this.add(editArea,BorderLayout.CENTER);//鍚戠獥鍙f坊鍔犳枃鏈紪杈戝尯 oldValue=textArea.getText();//鑾峰彇鍘熸枃鏈紪杈戝尯鐨勫唴瀹 - //缂栬緫鍖烘敞鍐屼簨浠剁洃鍚(涓庢挙閿鎿嶄綔鏈夊叧) + /*缂栬緫鍖烘敞鍐屼簨浠剁洃鍚(涓庢挙閿鎿嶄綔鏈夊叧)*/ textArea.getDocument().addUndoableEditListener(undoHandler); textArea.getDocument().addDocumentListener(this); - //鍒涘缓鍙抽敭寮瑰嚭鑿滃崟 + /*鍒涘缓鍙抽敭寮瑰嚭鑿滃崟*/ popupMenu=new JPopupMenu(); popupmenu_undo=new JMenuItem("鎾ら攢"); popupmenu_cut=new JMenuItem("鍓垏"); @@ -284,7 +300,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener popupmenu_selectall=new JMenuItem("鍏ㄩ"); popupmenu_undo.setEnabled(false); - //鍚戝彸閿彍鍗曟坊鍔犺彍鍗曢」鍜屽垎闅旂 + /*鍚戝彸閿彍鍗曟坊鍔犺彍鍗曢」鍜屽垎闅旂*/ popupMenu.add(popupmenu_undo); popupMenu.addSeparator(); popupMenu.add(popupmenu_cut); @@ -294,7 +310,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener popupMenu.addSeparator(); popupMenu.add(popupmenu_selectall); - //鏂囨湰缂栬緫鍖烘敞鍐屽彸閿彍鍗曚簨浠 + /*鏂囨湰缂栬緫鍖烘敞鍐屽彸閿彍鍗曚簨浠*/ popupmenu_undo.addActionListener(this); popupmenu_cut.addActionListener(this); popupmenu_copy.addActionListener(this); @@ -302,7 +318,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener popupmenu_delete.addActionListener(this); popupmenu_selectall.addActionListener(this); - //鏂囨湰缂栬緫鍖烘敞鍐屽彸閿彍鍗曚簨浠 + /*鏂囨湰缂栬緫鍖烘敞鍐屽彸閿彍鍗曚簨浠*/ textArea.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if(e.isPopupTrigger())//杩斿洖姝ら紶鏍囦簨浠舵槸鍚︿负璇ュ钩鍙扮殑寮瑰嚭鑿滃崟瑙﹀彂浜嬩欢 @@ -322,67 +338,43 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener - //鍒涘缓鍜屾坊鍔犵姸鎬佹爮 - JToolBar statusLabel=new JToolBar(); + /*鍒涘缓鍜屾坊鍔犵姸鎬佹爮*/ + JToolBar statusLabel=new JToolBar(); statusLabel.setFloatable(false); - // Calendar calendar=Calendar.getInstance(); - //SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD"); - //JLabel label = new JLabel("褰撳墠瀛楁暟:",statusBar_status,SwingConstants.CENTER); - //labelTime = new JLabel("鏃ユ湡: "+sdf.format(new Date())); - JLabel labelCodeStyle = new JLabel("缂栫爜: "+codestyle); - //statusLabel.add(label); + SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd 浠婂勾鐨勭DD澶"); + labelTime = new JLabel("鏃ユ湡: "+sdf.format(new Date())); statusLabel.addSeparator(new Dimension(180,5)); - //statusLabel.add(labelTime); - statusLabel.addSeparator(new Dimension(180,5)); - statusLabel.add(labelCodeStyle); - this.add(statusLabel,BorderLayout.SOUTH);//鍚戠獥鍙f坊鍔犵姸鎬佹爮鏍囩 + statusLabel.add(labelTime); + statusLabel.setVisible(true); + + this.add(statusLabel,BorderLayout.SOUTH);//鍚戠獥鍙f坊鍔犵姸鎬佹爮鏍囩 this.setLocation(100,100);//璁剧疆绐楀彛鍦ㄥ睆骞曚笂鐨勪綅缃 this.setSize(650,550);//璁剧疆绐楀彛鍦ㄥ睆骞曚笂鐨勫ぇ灏 this.setVisible(true);//璁剧疆绐楀彛鍦ㄥ睆骞曚笂鐨勫彲瑙佹 -// -// 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(); // 涓鏃︽枃鏈湁鏀瑰彉灏辫缃悇鎸夐挳鐨勫彲鐢ㄦ -// } -// -// });} -// public void changeTextLengthStatus(){ // 鏂囨湰鐩戝惉 -// String content = textArea.getText().trim(); -// label.setText("褰撳墠瀛楁暟:"+content.length()); -// } -// -// private void isItemsAvalible(){ // 鐩戣鏂囨湰鍖哄苟璁剧疆鍚勫姛鑳介」鏄惁鍙敤 -// String content = textArea.getText(); -// if(content.equals("")){ -// menuItem_cut.setEnabled(false); -// menuItem_delete.setEnabled(false); -// menuItem_copy.setEnabled(false); -// menuItem_save.setEnabled(false); -// menuItem_saveas.setEnabled(false); -// popupmenu_copy.setEnabled(false); -// popupmenu_cut.setEnabled(false); -// }else{ -// menuItem_save.setEnabled(true); -// menuItem_cut.setEnabled(true); -// menuItem_delete.setEnabled(true); -// menuItem_copy.setEnabled(true); -// popupmenu_copy.setEnabled(true); -// popupmenu_cut.setEnabled(true); -// -// } -// -// + + /* + * 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(); // 涓鏃︽枃鏈湁鏀瑰彉灏辫缃悇鎸夐挳鐨勫彲鐢ㄦ } });} public void + * changeTextLengthStatus(){ // 鏂囨湰鐩戝惉 String content = textArea.getText().trim(); + * label.setText("褰撳墠瀛楁暟:"+content.length()); } private void isItemsAvalible(){ // + * 鐩戣鏂囨湰鍖哄苟璁剧疆鍚勫姛鑳介」鏄惁鍙敤 String content = textArea.getText(); if(content.equals("")){ + * menuItem_cut.setEnabled(false); menuItem_delete.setEnabled(false); + * menuItem_copy.setEnabled(false); menuItem_save.setEnabled(false); + * menuItem_saveas.setEnabled(false); popupmenu_copy.setEnabled(false); + * popupmenu_cut.setEnabled(false); }else{ menuItem_save.setEnabled(true); + * menuItem_cut.setEnabled(true); menuItem_delete.setEnabled(true); + * menuItem_copy.setEnabled(true); popupmenu_copy.setEnabled(true); + * popupmenu_cut.setEnabled(true); + * + * } + */ + + /*娣诲姞绐楀彛鐩戝惉鍣*/ addWindowListener(new WindowAdapter() @@ -877,6 +869,60 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener fontDialog.setVisible(true); } + + /*瀹氭椂鍣ㄦ柟娉*/ + public void Clock() + { + final JDialog replaceDialog=new JDialog(this,"瀹氭椂鍣",false); + Container con=replaceDialog.getContentPane(); //杩斿洖姝ゅ璇濇鐨刢ontentPane瀵硅薄 + con.setLayout(new FlowLayout(FlowLayout.CENTER)); + JOptionPane.showMessageDialog(null, "鍚姩瀹氭椂鍣"); + System.out.println("鍚姩瀹氭椂鍣紒"); + int setTime = setTime(); + if (-1 != setTime) { + System.out.println("寮濮嬭鏃讹紒"); + long tmil = 60 * 1000 * setTime; + while (true) { + try { + Thread.sleep(tmil); + int confirmDialog = JOptionPane.showConfirmDialog(null, "鍙彯鍙畘", "", JOptionPane.YES_NO_OPTION); + if (1 == confirmDialog) { + break; + } + } catch (InterruptedException e) { + e.printStackTrace(); + break; + } + } + } + JOptionPane.showMessageDialog(null, "缁撴潫瀹氭椂鍣"); + System.out.println("缁撴潫瀹氭椂鍣"); + } + public int setTime() { + String time = JOptionPane.showInputDialog("杈撳叆闂撮殧鏃堕棿(鍒嗛挓0-1440)锛"); + if (time == null) { + return -1; + } + int i = 5; + try {i = Integer.parseInt(time); + if (i < 1) { + throw new Exception("鏃堕棿杈撳叆鑼冨洿涓嶆纭!"); + } + if (i > 1440) { + i = 1440; + throw new Exception("鏃堕棿杈撳叆鑼冨洿涓嶆纭!"); + } + } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(null, "鏃堕棿杈撳叆閿欒锛"); + i = setTime(); + } catch (Exception e) { + JOptionPane.showMessageDialog(null, e.getMessage()); + i = setTime(); + } + return i; + } + + public void actionPerformed(ActionEvent e) { //鏂板缓 @@ -936,8 +982,9 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener menuItem_undo.setEnabled(false); oldValue=textArea.getText(); } - }//鏂板缓缁撴潫 - //鎵撳紑 + } + + /*鎵撳紑*/ else if(e.getSource()==menuItem_open) { textArea.requestFocus(); String currentValue=textArea.getText(); @@ -1239,19 +1286,37 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener font(); } + /*鐘舵佹爮璁剧疆*/ + else if(e.getSource()==menuItem_status) + { if(menuItem_status.getState()) + statusLabel.setVisible(true); + else + statusLabel.setVisible(false); + } + + + /*甯姪*/ else if(e.getSource()==menuItem_HelpTopics) { textArea.requestFocus(); - JOptionPane.showMessageDialog(this,"鍔姏锛侊紒","鏌ョ湅甯姪",JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(this,"浼樼涓嶆槸涓绉嶈涓猴紝鑰屾槸涓绉嶄範鎯紒","鏌ョ湅甯姪",JOptionPane.INFORMATION_MESSAGE); } /*鍏充簬*/ else if(e.getSource()==menuItem_AboutNoteBook) { textArea.requestFocus(); JOptionPane.showMessageDialog(this, - "鏈浜嬫湰鍙互*******", + "璁颁簨鏈腑鐨勫畾鏃跺櫒鍔熻兘浣跨敤璇存槑\n" + + "1.鈥滅‘璁も濃斺斺斺斿紑濮嬭鏃跺櫒\n锛堢‘璁ら棿闅旀椂闂村悗锛屽埌闂撮殧鏃堕棿灏变細寮瑰嚭绐楀彛鎻愰啋锛岃嫢鎸夆滄槸鈥濆垯缁х画浼戞伅璇ラ棿闅旀椂闂达級\n2.鈥滃彇娑堚濃斺斺斺旂粨鏉熻鏃跺櫒\n" + +"璁℃椂鍣ㄥ紑鍚悗锛屽垯涓嶈兘瀵硅浜嬫湰杩涜缂栬緫\n鎵浠ワ紝璇峰ソ濂戒紤鎭竴涓嬬溂鐫涘惂锛", "鍏充簬",JOptionPane.INFORMATION_MESSAGE); } + + /*瀹氭椂鍣*/ + else if(e.getSource()==settime) + { + Clock(); + } } /*瀹炵幇DocumentListener鎺ュ彛涓殑鏂规硶*/ -- Gitee From 03c8afe0372cb65500145d3bb1bbe27377e4c8fc Mon Sep 17 00:00:00 2001 From: lenovo Date: Fri, 11 Jun 2021 08:29:22 +0800 Subject: [PATCH 11/19] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=AE=BE=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/TimeNoteBook/Notebook.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/TimeNoteBook/Notebook.java b/src/TimeNoteBook/Notebook.java index 277599d..f02fd57 100644 --- a/src/TimeNoteBook/Notebook.java +++ b/src/TimeNoteBook/Notebook.java @@ -35,12 +35,12 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener public static final Color doushalv = new Color(199,237,204);//鏂板缓棰滆壊涓烘姢鐪艰眴娌欑豢 /*鑿滃崟*/ - private JMenu menu_file; - private JMenu menu_edit; - private JMenu menu_format; - private JMenu menu_help; - private JMenu menu_view; - private JMenu menu_settime; + private JMenu menu_file; //鏂囦欢鎸夐挳 + private JMenu menu_edit; //缂栬緫鎸夐挳 + private JMenu menu_format; //鏍煎紡鎸夐挳 + private JMenu menu_help; //甯姪鎸夐挳 + private JMenu menu_view; //鏌ョ湅鎸夐挳 + private JMenu menu_settime; //瀹氭椂鍣ㄦ寜閽 /*鍙抽敭寮瑰嚭鑿滃崟*/ private JPopupMenu popupMenu; @@ -390,7 +390,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener - //璁剧疆鑿滃崟椤圭殑鍙敤鎬э細鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ゅ姛鑳 + /*璁剧疆鑿滃崟椤圭殑鍙敤鎬э細鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ゅ姛鑳*/ public void checkMenuItemEnabled() { String selectText=textArea.getSelectedText(); if(selectText==null) @@ -520,7 +520,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener } }); - //"鏌ユ壘涓嬩竴涓"鎸夐挳鐩戝惉 + /*"鏌ユ壘涓嬩竴涓"鎸夐挳鐩戝惉*/ findNextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //"鍖哄垎澶у皬鍐"鐨凧CheckBox鏄惁琚変腑 -- Gitee From c3f78f06ea5d036ecaf8ddebb4f3325837ca778d Mon Sep 17 00:00:00 2001 From: lenovo Date: Fri, 11 Jun 2021 11:54:38 +0800 Subject: [PATCH 12/19] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .settings/org.eclipse.core.resources.prefs | 2 ++ src/TimeNoteBook/Notebook.java | 11 +++++------ 2 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 .settings/org.eclipse.core.resources.prefs diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..f12d244 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding//src/TimeNoteBook/Notebook.java=UTF-8 diff --git a/src/TimeNoteBook/Notebook.java b/src/TimeNoteBook/Notebook.java index f02fd57..cf1a85d 100644 --- a/src/TimeNoteBook/Notebook.java +++ b/src/TimeNoteBook/Notebook.java @@ -30,8 +30,11 @@ import java.awt.event.*; import java.beans.EventHandler; public class Notebook extends JFrame implements DocumentListener,ActionListener { + public static void main(String args[]) + { Notebook notebook=new Notebook(); + notebook.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//浣跨敤 System exit 鏂规硶閫鍑哄簲鐢ㄧ▼搴 + } private static Icon statusBar_status; - public static final Color doushalv = new Color(199,237,204);//鏂板缓棰滆壊涓烘姢鐪艰眴娌欑豢 /*鑿滃崟*/ @@ -90,7 +93,6 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener private JLabel statusLabel; /*鑿滃崟*/ private JMenuBar menuBar; - private JLabel labelTime; Toolkit toolkit=Toolkit.getDefaultToolkit(); Clipboard clipBoard=toolkit.getSystemClipboard(); @@ -1340,10 +1342,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener - public static void main(String args[]) - { Notebook notebook=new Notebook(); - notebook.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//浣跨敤 System exit 鏂规硶閫鍑哄簲鐢ㄧ▼搴 - } + } -- Gitee From 16376d17025135b8b85ab815d177092ae8e0eae5 Mon Sep 17 00:00:00 2001 From: lenovo Date: Fri, 11 Jun 2021 12:01:12 +0800 Subject: [PATCH 13/19] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Notebook.java | 1353 ++++++++++++++++++++++++++++++ 1 file changed, 1353 insertions(+) create mode 100644 src/java2020spring/Notebook.java diff --git a/src/java2020spring/Notebook.java b/src/java2020spring/Notebook.java new file mode 100644 index 0000000..fba5144 --- /dev/null +++ b/src/java2020spring/Notebook.java @@ -0,0 +1,1353 @@ +package java2020spring; + +import java.awt.*; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; +import java.io.Serializable ; + +import javax.swing.*; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import javax.swing.event.MenuEvent; +import javax.swing.event.MenuListener; +import javax.swing.event.UndoableEditEvent; +import javax.swing.event.UndoableEditListener; +import javax.swing.undo.CannotUndoException; +import javax.swing.undo.UndoManager; +import java.io.*; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Enumeration; + +import javax.swing.*; +import java.awt.event.*; +import java.beans.EventHandler; + +public class Notebook extends JFrame implements DocumentListener,ActionListener { + public static void main(String args[]) + { Notebook notebook=new Notebook(); + notebook.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//浣跨敤 System exit 鏂规硶閫鍑哄簲鐢ㄧ▼搴 + } + private static Icon statusBar_status; + public static final Color doushalv = new Color(199,237,204);//鏂板缓棰滆壊涓烘姢鐪艰眴娌欑豢 + + /*鑿滃崟*/ + private JMenu menu_file; //鏂囦欢鎸夐挳 + private JMenu menu_edit; //缂栬緫鎸夐挳 + private JMenu menu_format; //鏍煎紡鎸夐挳 + private JMenu menu_help; //甯姪鎸夐挳 + private JMenu menu_view; //鏌ョ湅鎸夐挳 + private JMenu menu_settime; //瀹氭椂鍣ㄦ寜閽 + + /*鍙抽敭寮瑰嚭鑿滃崟*/ + private JPopupMenu popupMenu; + private JMenuItem popupmenu_undo; + private JMenuItem popupmenu_cut; + private JMenuItem popupmenu_copy; + private JMenuItem popupmenu_paste; + private JMenuItem popupmenu_delete; + private JMenuItem popupmenu_selectall; + + /*鏂囦欢鑿滃崟*/ + private JMenuItem menuItem_new; + private JMenuItem menuItem_open; + private JMenuItem menuItem_save; + private JMenuItem menuItem_saveas; + private JMenuItem menuItem_exit; + + /*缂栬緫鑿滃崟*/ + private JMenuItem menuItem_undo; + private JMenuItem menuItem_time; + private JMenuItem menuItem_find; + private JMenuItem menuItem_replace; + private JMenuItem menuItem_paste; + private JMenuItem menuItem_selectall; + private JMenuItem menuItem_cut; + private JMenuItem menuItem_copy; + private JMenuItem menuItem_delete; + + /*鏍煎紡鑿滃崟*/ + private JCheckBoxMenuItem menuItem_linewrap; + private JMenuItem menuItem_font; + + /*鏌ョ湅鐨勮彍鍗*/ + private JCheckBoxMenuItem menuItem_status; + + /*甯姪鐨勮彍鍗*/ + private JMenuItem menuItem_HelpTopics; + private JMenuItem menuItem_AboutNoteBook; + + /*瀹氭椂鍣*/ + private JMenuItem settime; + + /*鏂囨湰缂栬緫鍖哄煙*/ + private JTextArea textArea; + + /*鐘舵佹爮*/ + private JLabel statusLabel; + /*鑿滃崟*/ + private JMenuBar menuBar; + private JLabel labelTime; + Toolkit toolkit=Toolkit.getDefaultToolkit(); + Clipboard clipBoard=toolkit.getSystemClipboard(); + + /*鍒涘缓鎾ら攢鎿嶄綔绠$悊鍣*/ + protected UndoManager undo=new UndoManager(); + protected UndoableEditListener undoHandler=new UndoHandler();//鍏朵粬鍙橀噺 + private String oldValue;//瀛樻斁缂栬緫鍖哄師鏉ョ殑鍐呭锛岀敤浜庢瘮杈冩枃鏈槸鍚︽湁鏀瑰姩 + boolean isNewFile=true;//鏄惁鏂版枃浠(鏈繚瀛樿繃鐨) + File currentFile; + private JLabel label; + + //褰撳墠鏂囦欢鍚 + + public Notebook(){ + super("鎶ょ溂璁颁簨鏈"); + Font font=new Font("Courier Prime", Font.PLAIN, 12); //鏀瑰彉绯荤粺榛樿瀛椾綋 + Enumeration keys = UIManager.getDefaults().keys(); + while (keys.hasMoreElements()) { + Object key = keys.nextElement(); + Object value = UIManager.get(key); + if (value instanceof javax.swing.plaf.FontUIResource) { + UIManager.put(key,font); + } + } + + + JMenuBar menuBar=new JMenuBar(); //鍒涘缓鑿滃崟鏉 + + /*鍒涘缓鏂囦欢鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚*/ + menu_file=new JMenu("鏂囦欢"); + + menuItem_new=new JMenuItem("鏂板缓"); + menuItem_new.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); + menuItem_new.addActionListener(this); + + menuItem_open=new JMenuItem("鎵撳紑"); + menuItem_open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); + menuItem_open.addActionListener(this); + + menuItem_save=new JMenuItem("淇濆瓨"); + menuItem_save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); + menuItem_save.addActionListener(this); + + menuItem_saveas=new JMenuItem("鍙﹀瓨涓"); + menuItem_saveas.addActionListener(this); + + menuItem_exit=new JMenuItem("閫鍑"); + menuItem_exit.addActionListener(this); + + /*鍒涘缓缂栬緫鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚*/ + menu_edit=new JMenu("缂栬緫"); + menu_edit.setMnemonic('E');//璁剧疆蹇嵎閿瓵LT+E + + /*褰撻夋嫨缂栬緫鑿滃崟鏃讹紝璁剧疆鍓垏銆佸鍒躲佺矘璐淬佸垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ*/ + + menu_edit.addMenuListener(new MenuListener() + { public void menuCanceled(MenuEvent e)//鍙栨秷鑿滃崟鏃惰皟鐢 + { checkMenuItemEnabled();//璁剧疆鍓垏銆佸鍒躲佺矘璐淬佸垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ + } + public void menuDeselected(MenuEvent e)//鍙栨秷閫夋嫨鏌愪釜鑿滃崟鏃惰皟鐢 + { checkMenuItemEnabled();//璁剧疆鍓垏銆佸鍒躲佺矘璐淬佸垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ + } + public void menuSelected(MenuEvent e)//閫夋嫨鏌愪釜鑿滃崟鏃惰皟鐢 + { checkMenuItemEnabled();//璁剧疆鍓垏銆佸鍒躲佺矘璐淬佸垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ + } + }); + + menuItem_undo=new JMenuItem("鎾ら攢"); + menuItem_undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK)); + menuItem_undo.addActionListener(this); + menuItem_undo.setEnabled(false); + + menuItem_cut=new JMenuItem("鍓垏"); + menuItem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK)); + menuItem_cut.addActionListener(this); + + menuItem_copy=new JMenuItem("澶嶅埗"); + menuItem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); + menuItem_copy.addActionListener(this); + + menuItem_paste=new JMenuItem("绮樿创"); + menuItem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); + menuItem_paste.addActionListener(this); + + menuItem_delete=new JMenuItem("鍒犻櫎"); + menuItem_delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0)); + menuItem_delete.addActionListener(this); + + menuItem_find=new JMenuItem("鏌ユ壘"); + menuItem_find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK)); + menuItem_find.addActionListener(this); + + menuItem_replace = new JMenuItem("鏇挎崲"); + menuItem_replace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK)); + menuItem_replace.addActionListener(this); + + menuItem_selectall = new JMenuItem("鍏ㄩ"); + menuItem_selectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); + menuItem_selectall.addActionListener(this); + + menuItem_time = new JMenuItem("鏃堕棿/鏃ユ湡"); + menuItem_time.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0)); + menuItem_time.addActionListener(this); + + /*鍒涘缓鏍煎紡鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚*/ + menu_format=new JMenu("鏍煎紡"); + menu_format.setMnemonic('O');//璁剧疆蹇嵎閿瓵LT+O + + menuItem_linewrap=new JCheckBoxMenuItem("鑷姩鎹㈣"); + menuItem_linewrap.setMnemonic('W');//璁剧疆蹇嵎閿瓵LT+W + menuItem_linewrap.setState(true); + menuItem_linewrap.addActionListener(this); + + menuItem_font=new JMenuItem("瀛椾綋"); + menuItem_font.addActionListener(this); + + /*鍒涘缓鏌ョ湅鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚*/ + menu_view=new JMenu("鏌ョ湅"); + menu_view.setMnemonic('V');//璁剧疆蹇嵎閿瓵LT+V + + menuItem_status=new JCheckBoxMenuItem("鐘舵佹爮"); + menuItem_status.setMnemonic('S');//璁剧疆蹇嵎閿瓵LT+S + menuItem_status.setState(true); + menuItem_status.addActionListener(this); + + /*鍒涘缓甯姪鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚*/ + menu_help = new JMenu("甯姪"); + menu_help.setMnemonic('H');//璁剧疆蹇嵎閿瓵LT+H + + menuItem_HelpTopics = new JMenuItem("甯姪涓婚"); + menuItem_HelpTopics.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0)); + menuItem_HelpTopics.addActionListener(this); + + menuItem_AboutNoteBook = new JMenuItem("鍏充簬璁颁簨鏈"); + menuItem_AboutNoteBook.addActionListener(this); + + /*瀹氭椂鍣*/ + menu_settime=new JMenu ("瀹氭椂鍣"); + settime=new JMenuItem("瀹氭椂鍣"); + settime.addActionListener(this); + + /*鍚戣彍鍗曟潯娣诲姞"鏂囦欢"鑿滃崟鍙婅彍鍗曢」*/ + menuBar.add(menu_file); + menu_file.add(menuItem_new); + menu_file.add(menuItem_open); + menu_file.add(menuItem_save); + menu_file.add(menuItem_saveas); + menu_file.add(menuItem_exit); + + /*鍚戣彍鍗曟潯娣诲姞"缂栬緫"鑿滃崟鍙婅彍鍗曢」 */ + menuBar.add(menu_edit); + menu_edit.add(menuItem_undo); + menu_edit.add(menuItem_cut); + menu_edit.add(menuItem_copy); + menu_edit.add(menuItem_paste); + menu_edit.add(menuItem_delete); + menu_edit.add(menuItem_find); + menu_edit.add(menuItem_replace); + menu_edit.add(menuItem_selectall); + menu_edit.add(menuItem_time); + + /*鍚戣彍鍗曟潯娣诲姞"鏍煎紡"鑿滃崟鍙婅彍鍗曢」*/ + menuBar.add(menu_format); + menu_format.add(menuItem_linewrap); + menu_format.add(menuItem_font); + + /*鍚戣彍鍗曟潯娣诲姞"鏌ョ湅"鑿滃崟鍙婅彍鍗曢」 */ + menuBar.add(menu_view); + menu_view.add(menuItem_status); + + /*鍚戣彍鍗曟潯娣诲姞"甯姪"鑿滃崟鍙婅彍鍗曢」*/ + menuBar.add(menu_help); + menu_help.add(menuItem_HelpTopics); + menu_help.addSeparator(); + menu_help.add(menuItem_AboutNoteBook); + + /*鍚戣彍鍗曟潯娣诲姞鈥滃畾鏃跺櫒鈥濊彍鍗*/ + menuBar.add(menu_settime); + menu_settime.add(settime); + + /*鍚戠獥鍙f坊鍔犺彍鍗曟潯*/ + this.setJMenuBar(menuBar); + + /*鍒涘缓鏂囨湰缂栬緫鍖哄苟娣诲姞婊氬姩鏉*/ + textArea=new JTextArea(20,50); + JScrollPane scroller=new JScrollPane(textArea); + scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + this.add(scroller,BorderLayout.CENTER);//鍚戠獥鍙f坊鍔犳枃鏈紪杈戝尯 + textArea.setBackground(doushalv); + textArea.setWrapStyleWord(true);//璁剧疆鍗曡瘝鍦ㄤ竴琛屼笉瓒冲绾虫椂鎹㈣ + textArea.setLineWrap(true);//璁剧疆鏂囨湰缂栬緫鍖鸿嚜鍔ㄦ崲琛岄粯璁や负true,鍗充細"鑷姩鎹㈣" + oldValue=textArea.getText();//鑾峰彇鍘熸枃鏈紪杈戝尯鐨勫唴瀹 + + /*缂栬緫鍖烘敞鍐屼簨浠剁洃鍚(涓庢挙閿鎿嶄綔鏈夊叧)*/ + textArea.getDocument().addUndoableEditListener(undoHandler); + textArea.getDocument().addDocumentListener(this); + + + /*鍒涘缓鍙抽敭寮瑰嚭鑿滃崟*/ + popupMenu=new JPopupMenu(); + popupmenu_undo=new JMenuItem("鎾ら攢"); + popupmenu_cut=new JMenuItem("鍓垏"); + popupmenu_copy=new JMenuItem("澶嶅埗"); + popupmenu_paste=new JMenuItem("绮樺笘"); + popupmenu_delete=new JMenuItem("鍒犻櫎"); + popupmenu_selectall=new JMenuItem("鍏ㄩ"); + popupmenu_undo.setEnabled(false); + + /*鍚戝彸閿彍鍗曟坊鍔犺彍鍗曢」鍜屽垎闅旂*/ + popupMenu.add(popupmenu_undo); + popupMenu.addSeparator(); + popupMenu.add(popupmenu_cut); + popupMenu.add(popupmenu_copy); + popupMenu.add(popupmenu_paste); + popupMenu.add(popupmenu_delete); + popupMenu.addSeparator(); + popupMenu.add(popupmenu_selectall); + + /*鏂囨湰缂栬緫鍖烘敞鍐屽彸閿彍鍗曚簨浠*/ + popupmenu_undo.addActionListener(this); + popupmenu_cut.addActionListener(this); + popupmenu_copy.addActionListener(this); + popupmenu_paste.addActionListener(this); + popupmenu_delete.addActionListener(this); + popupmenu_selectall.addActionListener(this); + + /*鏂囨湰缂栬緫鍖烘敞鍐屽彸閿彍鍗曚簨浠*/ + textArea.addMouseListener(new MouseAdapter() + { public void mousePressed(MouseEvent e) + { if(e.isPopupTrigger())//杩斿洖姝ら紶鏍囦簨浠舵槸鍚︿负璇ュ钩鍙扮殑寮瑰嚭鑿滃崟瑙﹀彂浜嬩欢 + { popupMenu.show(e.getComponent(),e.getX(),e.getY());//鍦ㄧ粍浠惰皟鐢ㄨ呯殑鍧愭爣绌洪棿涓殑浣嶇疆 X銆乊 鏄剧ず寮瑰嚭鑿滃崟 + } + checkMenuItemEnabled();//璁剧疆鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ + textArea.requestFocus();//缂栬緫鍖鸿幏鍙栫劍鐐 + } + public void mouseReleased(MouseEvent e) + { if(e.isPopupTrigger())//杩斿洖姝ら紶鏍囦簨浠舵槸鍚︿负璇ュ钩鍙扮殑寮瑰嚭鑿滃崟瑙﹀彂浜嬩欢 + { popupMenu.show(e.getComponent(),e.getX(),e.getY());//鍦ㄧ粍浠惰皟鐢ㄨ呯殑鍧愭爣绌洪棿涓殑浣嶇疆 X銆乊 鏄剧ず寮瑰嚭鑿滃崟 + } + checkMenuItemEnabled();//璁剧疆鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ + textArea.requestFocus();//缂栬緫鍖鸿幏鍙栫劍鐐 + } + }); + + + + /*鍒涘缓鍜屾坊鍔犵姸鎬佹爮*/ + JToolBar statusLabel=new JToolBar(); + statusLabel.setFloatable(false); + SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd 浠婂勾鐨勭DD澶"); + labelTime = new JLabel("鏃ユ湡: "+sdf.format(new Date())); + statusLabel.addSeparator(new Dimension(180,5)); + statusLabel.add(labelTime); + statusLabel.setVisible(true); + + this.add(statusLabel,BorderLayout.SOUTH);//鍚戠獥鍙f坊鍔犵姸鎬佹爮鏍囩 + this.setLocation(100,100);//璁剧疆绐楀彛鍦ㄥ睆骞曚笂鐨勪綅缃 + this.setSize(650,550);//璁剧疆绐楀彛鍦ㄥ睆骞曚笂鐨勫ぇ灏 + this.setVisible(true);//璁剧疆绐楀彛鍦ㄥ睆骞曚笂鐨勫彲瑙佹 + + /* + * 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(); // 涓鏃︽枃鏈湁鏀瑰彉灏辫缃悇鎸夐挳鐨勫彲鐢ㄦ } });} public void + * changeTextLengthStatus(){ // 鏂囨湰鐩戝惉 String content = textArea.getText().trim(); + * label.setText("褰撳墠瀛楁暟:"+content.length()); } private void isItemsAvalible(){ // + * 鐩戣鏂囨湰鍖哄苟璁剧疆鍚勫姛鑳介」鏄惁鍙敤 String content = textArea.getText(); if(content.equals("")){ + * menuItem_cut.setEnabled(false); menuItem_delete.setEnabled(false); + * menuItem_copy.setEnabled(false); menuItem_save.setEnabled(false); + * menuItem_saveas.setEnabled(false); popupmenu_copy.setEnabled(false); + * popupmenu_cut.setEnabled(false); }else{ menuItem_save.setEnabled(true); + * menuItem_cut.setEnabled(true); menuItem_delete.setEnabled(true); + * menuItem_copy.setEnabled(true); popupmenu_copy.setEnabled(true); + * popupmenu_cut.setEnabled(true); + * + * } + */ + + + + /*娣诲姞绐楀彛鐩戝惉鍣*/ + addWindowListener(new WindowAdapter() + { public void windowClosing(WindowEvent e) + { exitWindowChoose(); + } + }); + checkMenuItemEnabled(); + textArea.requestFocus();} + + + + + + + /*璁剧疆鑿滃崟椤圭殑鍙敤鎬э細鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ゅ姛鑳*/ + public void checkMenuItemEnabled() + { String selectText=textArea.getSelectedText(); + if(selectText==null) + { menuItem_cut.setEnabled(false); + popupmenu_cut.setEnabled(false); + menuItem_copy.setEnabled(false); + popupmenu_copy.setEnabled(false); + menuItem_delete.setEnabled(false); + popupmenu_delete.setEnabled(false); + } + else + { menuItem_cut.setEnabled(true); + popupmenu_cut.setEnabled(true); + menuItem_copy.setEnabled(true); + popupmenu_copy.setEnabled(true); + menuItem_delete.setEnabled(true); + popupmenu_delete.setEnabled(true); + } + /*绮樺笘鍔熻兘鍙敤鎬у垽鏂*/ + Transferable contents=clipBoard.getContents(this); + if(contents==null) + { menuItem_paste.setEnabled(false); + popupmenu_paste.setEnabled(false); + } + else + { menuItem_paste.setEnabled(true); + popupmenu_paste.setEnabled(true); + } + } + + /*鍏抽棴绐楀彛鏃惰皟鐢*/ + public void exitWindowChoose() + { textArea.requestFocus(); + String currentValue=textArea.getText(); + if(currentValue.equals(oldValue)==true) + { System.exit(0); + } + else + { int exitChoose=JOptionPane.showConfirmDialog(this,"浣犵殑鏂囦欢杩樻病淇濆瓨鍝:)","閫鍑!!",JOptionPane.YES_NO_CANCEL_OPTION); + if(exitChoose==JOptionPane.YES_OPTION) + { boolean isSave=false; + if(isNewFile) + { + String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setApproveButtonText("!!!纭畾!!!"); + fileChooser.setDialogTitle("鍙﹀瓨涓"); + + int result=fileChooser.showSaveDialog(this); + + File saveFileName=fileChooser.getSelectedFile(); + + if(result==JFileChooser.CANCEL_OPTION) + { statusLabel.setText("銆鎮ㄦ病鏈変繚瀛樻枃浠"); + return; + } + + if(saveFileName==null||saveFileName.getName().equals("")) + { JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚!","涓嶅悎娉曠殑鏂囦欢鍚!",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileWriter fw=new FileWriter(saveFileName); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush(); + fw.close(); + + isNewFile=false; + currentFile=saveFileName; + oldValue=textArea.getText(); + + this.setTitle(saveFileName.getName()+" de璁颁簨鏈"); + statusLabel.setText("銆褰撳墠鎵撳紑鏂囦欢:"+saveFileName.getAbsoluteFile()); + isSave=true; + } + catch(IOException ioException){ + } + } + } + else + { + try + { FileWriter fw=new FileWriter(currentFile); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush(); + fw.close(); + isSave=true; + } + catch(IOException ioException){ + } + } + System.exit(0); + if(isSave)System.exit(0); + else return; + } + else if(exitChoose==JOptionPane.NO_OPTION) + { System.exit(0); + } + else + { return; + } + } + } + + /*鏌ユ壘鏂规硶*/ + public void find() + { final JDialog findDialog=new JDialog(this,"鏌ユ壘",false);//false鏃跺厑璁稿叾浠栫獥鍙e悓鏃跺浜庢縺娲荤姸鎬(鍗虫棤妯″紡) + Container con=findDialog.getContentPane();//杩斿洖姝ゅ璇濇鐨刢ontentPane瀵硅薄 + con.setLayout(new FlowLayout(FlowLayout.LEFT)); + JLabel findContentLabel=new JLabel("鏌ユ壘鍐呭锛"); + final JTextField findText=new JTextField(15); + JButton findNextButton=new JButton("鏌ユ壘"); + final JCheckBox matchCheckBox=new JCheckBox("鍖哄垎澶у皬鍐"); + ButtonGroup bGroup=new ButtonGroup(); + final JRadioButton upButton=new JRadioButton("寰涓"); + final JRadioButton downButton=new JRadioButton("寰涓"); + downButton.setSelected(true); + bGroup.add(upButton); + bGroup.add(downButton); + JButton cancel=new JButton("鍙栨秷");//鍙栨秷鎸夐挳浜嬩欢 + cancel.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { findDialog.dispose(); + } + }); + + /*"鏌ユ壘涓嬩竴涓"鎸夐挳鐩戝惉*/ + findNextButton.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { //"鍖哄垎澶у皬鍐"鐨凧CheckBox鏄惁琚変腑 + int k=0,m=0; + final String str1,str2,str3,str4,strA,strB; + str1=textArea.getText(); + str2=findText.getText(); + str3=str1.toUpperCase(); + str4=str2.toUpperCase(); + if(matchCheckBox.isSelected())//鍖哄垎澶у皬鍐 + { strA=str1; + strB=str2; + } + else//涓嶅尯鍒嗗ぇ灏忓啓,姝ゆ椂鎶婃墍閫夊唴瀹瑰叏閮ㄥ寲鎴愬ぇ鍐(鎴栧皬鍐)锛屼互渚夸簬鏌ユ壘 + { strA=str3; + strB=str4; + } + if(upButton.isSelected()) + { + if(textArea.getSelectedText()==null) + k=strA.lastIndexOf(strB,textArea.getCaretPosition()-1); + else + k=strA.lastIndexOf(strB, textArea.getCaretPosition()-findText.getText().length()-1); + if(k>-1) + { + textArea.setCaretPosition(k); + textArea.select(k,k+strB.length()); + } + else + { JOptionPane.showMessageDialog(null,"鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹癸紒:(","鏌ユ壘",JOptionPane.INFORMATION_MESSAGE); + } + } + else if(downButton.isSelected()) + { if(textArea.getSelectedText()==null) + k=strA.indexOf(strB,textArea.getCaretPosition()+1); + else + k=strA.indexOf(strB, textArea.getCaretPosition()-findText.getText().length()+1); + if(k>-1) + { + textArea.setCaretPosition(k); + textArea.select(k,k+strB.length()); + } + else + { JOptionPane.showMessageDialog(null,"鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹癸紒:(","鏌ユ壘",JOptionPane.INFORMATION_MESSAGE); + } + } + } + }); + + /*鍒涘缓"鏌ユ壘"瀵硅瘽妗嗙殑鐣岄潰*/ + JPanel panel1=new JPanel(); + JPanel panel2=new JPanel(); + JPanel panel3=new JPanel(); + JPanel directionPanel=new JPanel(); + directionPanel.setBorder(BorderFactory.createTitledBorder("浣嶇疆")); //璁剧疆directionPanel缁勪欢鐨勮竟妗; + directionPanel.add(upButton); + directionPanel.add(downButton); + panel1.setLayout(new GridLayout(2,1)); + panel1.add(findNextButton); + panel1.add(cancel); + panel2.add(findContentLabel); + panel2.add(findText); + panel2.add(panel1); + panel3.add(matchCheckBox); + panel3.add(directionPanel); + con.add(panel2); + con.add(panel3); + findDialog.setSize(410,180); + findDialog.setResizable(false);//涓嶅彲璋冩暣澶у皬 + findDialog.setLocation(230,280); + findDialog.setVisible(true); + } + + /*鏇挎崲鏂规硶*/ + public void replace() + { final JDialog replaceDialog=new JDialog(this,"鏇挎崲",false);//false鏃跺厑璁稿叾浠栫獥鍙e悓鏃跺浜庢縺娲荤姸鎬(鍗虫棤妯″紡) + Container con=replaceDialog.getContentPane();//杩斿洖姝ゅ璇濇鐨刢ontentPane瀵硅薄 + con.setLayout(new FlowLayout(FlowLayout.CENTER)); + JLabel findContentLabel=new JLabel("鏌ユ壘鍐呭锛"); + final JTextField findText=new JTextField(15); + JButton findNextButton=new JButton("鏌ユ壘"); + JLabel replaceLabel=new JLabel("鏇挎崲涓猴細"); + final JTextField replaceText=new JTextField(15); + JButton replaceButton=new JButton("鏇挎崲"); + JButton replaceAllButton=new JButton("鍏ㄩ儴鏇挎崲"); + JButton cancel=new JButton("鍙栨秷"); + cancel.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { replaceDialog.dispose(); + } + }); + final JCheckBox matchCheckBox=new JCheckBox("鍖哄垎澶у皬鍐"); + ButtonGroup bGroup=new ButtonGroup(); + final JRadioButton upButton=new JRadioButton("寰涓"); + final JRadioButton downButton=new JRadioButton("寰涓"); + downButton.setSelected(true); + bGroup.add(upButton); + bGroup.add(downButton); + + + + /*"鏇挎崲"鎸夐挳鐩戝惉*/ + replaceButton.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { if(replaceText.getText().length()==0 && textArea.getSelectedText()!=null) + textArea.replaceSelection(""); + if(replaceText.getText().length()>0 && textArea.getSelectedText()!=null) + textArea.replaceSelection(replaceText.getText()); + } + });//"鏇挎崲"鎸夐挳鐩戝惉缁撴潫 + + /*"鍏ㄩ儴鏇挎崲"鎸夐挳鐩戝惉*/ + replaceAllButton.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { textArea.setCaretPosition(0); //灏嗗厜鏍囨斁鍒扮紪杈戝尯寮澶 + int k=0,m=0,replaceCount=0; + if(findText.getText().length()==0) + { JOptionPane.showMessageDialog(replaceDialog,"璇峰~鍐欐煡鎵惧唴瀹!","鎻愮ず",JOptionPane.WARNING_MESSAGE); + findText.requestFocus(true); + return; + } + while(k>-1)//褰撴枃鏈腑鏈夊唴瀹硅閫変腑鏃(k>-1琚変腑)杩涜鏇挎崲锛屽惁鍒欎笉杩涜while寰幆 + { + final String str1,str2,str3,str4,strA,strB; + str1=textArea.getText(); + str2=findText.getText(); + str3=str1.toUpperCase(); + str4=str2.toUpperCase(); + if(matchCheckBox.isSelected())//鍖哄垎澶у皬鍐 + { strA=str1; + strB=str2; + } + else//涓嶅尯鍒嗗ぇ灏忓啓,姝ゆ椂鎶婃墍閫夊唴瀹瑰叏閮ㄥ寲鎴愬ぇ鍐(鎴栧皬鍐)锛屼互渚夸簬鏌ユ壘 + { strA=str3; + strB=str4; + } + if(upButton.isSelected()) + { + if(textArea.getSelectedText()==null) + k=strA.lastIndexOf(strB,textArea.getCaretPosition()-1); + else + k=strA.lastIndexOf(strB, textArea.getCaretPosition()-findText.getText().length()-1); + if(k>-1) + { + textArea.setCaretPosition(k); + textArea.select(k,k+strB.length()); + } + else + { if(replaceCount==0) + { JOptionPane.showMessageDialog(replaceDialog, "鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹!", "璁颁簨鏈",JOptionPane.INFORMATION_MESSAGE); + } + else + { JOptionPane.showMessageDialog(replaceDialog,"鎴愬姛鏇挎崲"+replaceCount+"涓尮閰嶅唴瀹!","鏇挎崲鎴愬姛(鈮р垏鈮)",JOptionPane.INFORMATION_MESSAGE); + } + } + } + else if(downButton.isSelected()) + { if(textArea.getSelectedText()==null) + k=strA.indexOf(strB,textArea.getCaretPosition()+1); + else + k=strA.indexOf(strB, textArea.getCaretPosition()-findText.getText().length()+1); + if(k>-1) + { + textArea.setCaretPosition(k); + textArea.select(k,k+strB.length()); + } + else + { if(replaceCount==0) + { JOptionPane.showMessageDialog(replaceDialog, "鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹!:(", "璁颁簨鏈",JOptionPane.INFORMATION_MESSAGE); + } + else + { JOptionPane.showMessageDialog(replaceDialog,"鎴愬姛鏇挎崲"+replaceCount+"涓尮閰嶅唴瀹!","鏇挎崲鎴愬姛(鈮р垏鈮)",JOptionPane.INFORMATION_MESSAGE); + } + } + } + if(replaceText.getText().length()==0 && textArea.getSelectedText()!= null) + { textArea.replaceSelection(""); + replaceCount++; + } + + if(replaceText.getText().length()>0 && textArea.getSelectedText()!= null) + { textArea.replaceSelection(replaceText.getText()); + replaceCount++; + } + } + } + }); + + /*鍒涘缓"鏇挎崲"瀵硅瘽妗嗙殑鐣岄潰*/ + JPanel directionPanel=new JPanel(); + directionPanel.setBorder(BorderFactory.createTitledBorder("浣嶇疆"));//璁剧疆directionPanel缁勪欢鐨勮竟妗; + directionPanel.add(upButton); + directionPanel.add(downButton); + JPanel panel1=new JPanel(); + JPanel panel2=new JPanel(); + JPanel panel3=new JPanel(); + JPanel panel4=new JPanel(); + panel4.setLayout(new GridLayout(2,1)); + panel1.add(findContentLabel); + panel1.add(findText); + panel1.add(findNextButton); + panel4.add(replaceButton); + panel4.add(replaceAllButton); + panel2.add(replaceLabel); + panel2.add(replaceText); + panel2.add(panel4); + panel3.add(matchCheckBox); + panel3.add(directionPanel); + panel3.add(cancel); + con.add(panel1); + con.add(panel2); + con.add(panel3); + replaceDialog.setSize(420,220); + replaceDialog.setResizable(false);//涓嶅彲璋冩暣澶у皬 + replaceDialog.setLocation(230,280); + replaceDialog.setVisible(true); + } + + /*"瀛椾綋"鏂规硶*/ + + public void font() + { final JDialog fontDialog=new JDialog(this,"瀛椾綋璁剧疆",false); + Container con=fontDialog.getContentPane(); + con.setLayout(new FlowLayout(FlowLayout.LEFT)); + JLabel fontLabel=new JLabel("瀛椾綋锛"); + fontLabel.setPreferredSize(new Dimension(100,20));//鏋勯犱竴涓狣imension锛屽苟灏嗗叾鍒濆鍖栦负鎸囧畾瀹藉害鍜岄珮搴 + JLabel styleLabel=new JLabel("瀛楀舰锛"); + styleLabel.setPreferredSize(new Dimension(100,20)); + JLabel sizeLabel=new JLabel("澶у皬锛"); + sizeLabel.setPreferredSize(new Dimension(100,20)); + final JLabel sample=new JLabel("鏄綘鎯宠鐨勫瓧浣撳悧锛(*^锛燸*)"); + sample.setHorizontalAlignment(SwingConstants.CENTER); + final JTextField fontText=new JTextField(9); + fontText.setPreferredSize(new Dimension(200,20)); + final JTextField styleText=new JTextField(8); + styleText.setPreferredSize(new Dimension(200,20)); + final int style[]={Font.PLAIN,Font.BOLD,Font.ITALIC,Font.BOLD+Font.ITALIC}; + final JTextField sizeText=new JTextField(5); + sizeText.setPreferredSize(new Dimension(200,20)); + JButton okButton=new JButton("纭畾"); + JButton cancel=new JButton("鍙栨秷"); + cancel.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { fontDialog.dispose(); + } + }); + Font currentFont=textArea.getFont(); + fontText.setText(currentFont.getFontName()); + fontText.selectAll(); + if(currentFont.getStyle()==Font.PLAIN) + styleText.setText("甯歌"); + else if(currentFont.getStyle()==Font.BOLD) + styleText.setText("绮椾綋"); + else if(currentFont.getStyle()==Font.ITALIC) + styleText.setText("鏂滀綋"); + else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC)) + styleText.setText("绮楁枩浣"); + styleText.selectAll(); + String str=String.valueOf(currentFont.getSize()); + sizeText.setText(str); + sizeText.selectAll(); + final JList fontList,styleList,sizeList; + GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); + final String fontName[]=ge.getAvailableFontFamilyNames(); + fontList=new JList(fontName); + fontList.setFixedCellWidth(86); + fontList.setFixedCellHeight(20); + fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + final String fontStyle[]={"甯歌","绮椾綋","鏂滀綋","绮楁枩浣"}; + styleList=new JList(fontStyle); + styleList.setFixedCellWidth(86); + styleList.setFixedCellHeight(20); + styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + + if(currentFont.getStyle()==Font.PLAIN) + styleList.setSelectedIndex(0); + else if(currentFont.getStyle()==Font.BOLD) + styleList.setSelectedIndex(1); + else if(currentFont.getStyle()==Font.ITALIC) + styleList.setSelectedIndex(2); + else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC)) + styleList.setSelectedIndex(3); + final String fontSize[]={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"}; + sizeList=new JList(fontSize); + sizeList.setFixedCellWidth(43); + sizeList.setFixedCellHeight(20); + sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + + fontList.addListSelectionListener(new ListSelectionListener() + { public void valueChanged(ListSelectionEvent event) + { fontText.setText(fontName[fontList.getSelectedIndex()]); + fontText.selectAll(); + Font sampleFont1=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); + sample.setFont(sampleFont1); + } + }); + + styleList.addListSelectionListener(new ListSelectionListener() + { public void valueChanged(ListSelectionEvent event) + { int s=style[styleList.getSelectedIndex()]; + styleText.setText(fontStyle[s]); + styleText.selectAll(); + Font sampleFont2=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); + sample.setFont(sampleFont2); + } + }); + + sizeList.addListSelectionListener(new ListSelectionListener() + { public void valueChanged(ListSelectionEvent event) + { sizeText.setText(fontSize[sizeList.getSelectedIndex()]); + sizeText.requestFocus(); + sizeText.selectAll(); + Font sampleFont3=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); + sample.setFont(sampleFont3); + } + }); + okButton.addActionListener(new ActionListener() + { public void actionPerformed(ActionEvent e) + { Font okFont=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); + textArea.setFont(okFont); + fontDialog.dispose(); + } + }); + JPanel samplePanel=new JPanel(); + samplePanel.setBorder(BorderFactory.createTitledBorder("绀轰緥")); + samplePanel.setLayout(new FlowLayout(FlowLayout.CENTER)); + samplePanel.add(sample); + JPanel panel1=new JPanel(); + JPanel panel2=new JPanel(); + JPanel panel3=new JPanel(); + panel2.add(fontText); + panel2.add(styleText); + panel2.add(sizeText); + panel2.add(okButton); + panel3.add(new JScrollPane(fontList));//JList涓嶆敮鎸佺洿鎺ユ粴鍔紝鎵浠ヨ璁㎎List浣滀负JScrollPane鐨勮鍙h鍥 + panel3.add(new JScrollPane(styleList)); + panel3.add(new JScrollPane(sizeList)); + panel3.add(cancel); + con.add(panel1); + con.add(panel2); + con.add(panel3); + con.add(samplePanel); + fontDialog.setSize(350,340); + fontDialog.setLocation(200,200); + fontDialog.setResizable(false); + fontDialog.setVisible(true); + } + + + /*瀹氭椂鍣ㄦ柟娉*/ + public void Clock() + { + final JDialog replaceDialog=new JDialog(this,"瀹氭椂鍣",false); + Container con=replaceDialog.getContentPane(); //杩斿洖姝ゅ璇濇鐨刢ontentPane瀵硅薄 + con.setLayout(new FlowLayout(FlowLayout.CENTER)); + JOptionPane.showMessageDialog(null, "鍚姩瀹氭椂鍣"); + System.out.println("鍚姩瀹氭椂鍣紒"); + int setTime = setTime(); + if (-1 != setTime) { + System.out.println("寮濮嬭鏃讹紒"); + long tmil = 60 * 1000 * setTime; + while (true) { + try { + Thread.sleep(tmil); + int confirmDialog = JOptionPane.showConfirmDialog(null, "鍙彯鍙畘", "", JOptionPane.YES_NO_OPTION); + if (1 == confirmDialog) { + break; + } + } catch (InterruptedException e) { + e.printStackTrace(); + break; + } + } + } + JOptionPane.showMessageDialog(null, "缁撴潫瀹氭椂鍣"); + System.out.println("缁撴潫瀹氭椂鍣"); + } + public int setTime() { + String time = JOptionPane.showInputDialog("杈撳叆闂撮殧鏃堕棿(鍒嗛挓0-1440)锛"); + if (time == null) { + return -1; + } + int i = 5; + try {i = Integer.parseInt(time); + if (i < 1) { + throw new Exception("鏃堕棿杈撳叆鑼冨洿涓嶆纭!"); + } + if (i > 1440) { + i = 1440; + throw new Exception("鏃堕棿杈撳叆鑼冨洿涓嶆纭!"); + } + } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(null, "鏃堕棿杈撳叆閿欒锛"); + i = setTime(); + } catch (Exception e) { + JOptionPane.showMessageDialog(null, e.getMessage()); + i = setTime(); + } + return i; + } + + + + public void actionPerformed(ActionEvent e) + { //鏂板缓 + if(e.getSource()==menuItem_new) + { textArea.requestFocus(); + String currentValue=textArea.getText(); + boolean isTextChange=(currentValue.equals(oldValue))?false:true; + if(isTextChange) + { int saveChoose=JOptionPane.showConfirmDialog(this,"鎮ㄧ殑鏂囦欢灏氭湭淇濆瓨锛屾槸鍚︿繚瀛橈紵","鎻愮ず",JOptionPane.YES_NO_CANCEL_OPTION); + if(saveChoose==JOptionPane.YES_OPTION) + { String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setDialogTitle("鍙﹀瓨涓"); + int result=fileChooser.showSaveDialog(this); + File saveFileName=fileChooser.getSelectedFile(); + if(saveFileName==null || saveFileName.getName().equals("")) + { JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileWriter fw=new FileWriter(saveFileName); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush(); + bfw.close(); + isNewFile=false; + currentFile=saveFileName; + oldValue=textArea.getText(); + this.setTitle(saveFileName.getName()+" de 璁颁簨鏈"); + statusLabel.setText("褰撳墠鎵撳紑鏂囦欢锛"+saveFileName.getAbsoluteFile()); + } + catch (IOException ioException) + { + } + } + } + else if(saveChoose==JOptionPane.NO_OPTION) + { textArea.replaceRange("",0,textArea.getText().length()); + statusLabel.setText(" 鏂板缓鏂囦欢"); + this.setTitle("鏃犳爣棰 de璁颁簨鏈"); + isNewFile=true; + undo.discardAllEdits(); //鎾ゆ秷鎵鏈夌殑"鎾ゆ秷"鎿嶄綔 + menuItem_undo.setEnabled(false); + oldValue=textArea.getText(); + } + else if(saveChoose==JOptionPane.CANCEL_OPTION) + { return; + } + } + else + { textArea.replaceRange("",0,textArea.getText().length()); + statusLabel.setText(" 鏂板缓鏂囦欢"); + this.setTitle("鏃犳爣棰 - 璁颁簨鏈"); + isNewFile=true; + undo.discardAllEdits();//鎾ゆ秷鎵鏈夌殑"鎾ゆ秷"鎿嶄綔 + menuItem_undo.setEnabled(false); + oldValue=textArea.getText(); + } + } + + /*鎵撳紑*/ + else if(e.getSource()==menuItem_open) + { textArea.requestFocus(); + String currentValue=textArea.getText(); + boolean isTextChange=(currentValue.equals(oldValue))?false:true; + if(isTextChange) + { int saveChoose=JOptionPane.showConfirmDialog(this,"鎮ㄧ殑鏂囦欢灏氭湭淇濆瓨:(","鎻愮ず",JOptionPane.YES_NO_CANCEL_OPTION); + if(saveChoose==JOptionPane.YES_OPTION) + { String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setDialogTitle("鍙﹀瓨涓"); + int result=fileChooser.showSaveDialog(this); + File saveFileName=fileChooser.getSelectedFile(); + if(saveFileName==null || saveFileName.getName().equals("")) + { + statusLabel.setText("鎮ㄦ病鏈夐夋嫨浠讳綍鏂囦欢"); + JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileWriter fw=new FileWriter(saveFileName); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush();//鍒锋柊璇ユ祦鐨勭紦鍐 + bfw.close(); + isNewFile=false; + currentFile=saveFileName; + oldValue=textArea.getText(); + this.setTitle(saveFileName.getName()+" de璁颁簨鏈"); + statusLabel.setText("褰撳墠鎵撳紑鏂囦欢锛"+saveFileName.getAbsoluteFile()); + } + catch (IOException ioException) + { + } + } + } + else if(saveChoose==JOptionPane.NO_OPTION) + { String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setDialogTitle("鎵撳紑鏂囦欢"); + int result=fileChooser.showOpenDialog(this); + File fileName=fileChooser.getSelectedFile(); + if(fileName==null || fileName.getName().equals("")){ + statusLabel.setText("鎮ㄦ病鏈夐夋嫨浠讳綍鏂囦欢"); + JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileReader fr=new FileReader(fileName); + BufferedReader bfr=new BufferedReader(fr); + textArea.setText(""); + while((str=bfr.readLine())!=null) + { textArea.append(str); + } + this.setTitle(fileName.getName()+" de 璁颁簨鏈"); + statusLabel.setText(" 褰撳墠鎵撳紑鏂囦欢锛"+fileName.getAbsoluteFile()); + fr.close(); + isNewFile=false; + currentFile=fileName; + oldValue=textArea.getText(); + } + catch (IOException ioException) + { + } + } + } + else + { return; + } + } + else + { String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setDialogTitle("鎵撳紑鏂囦欢"); + int result=fileChooser.showOpenDialog(this); + File fileName=fileChooser.getSelectedFile(); + if(fileName==null || fileName.getName().equals("")) + { JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileReader fr=new FileReader(fileName); + BufferedReader bfr=new BufferedReader(fr); + textArea.setText(""); + while((str=bfr.readLine())!=null) + { textArea.append(str); + } + this.setTitle(fileName.getName()+" de 璁颁簨鏈"); + statusLabel.setText(" 褰撳墠鎵撳紑鏂囦欢锛"+fileName.getAbsoluteFile()); + fr.close(); + isNewFile=false; + currentFile=fileName; + oldValue=textArea.getText(); + } + catch (IOException ioException) + { + } + } + } + } + + /*淇濆瓨*/ + else if(e.getSource()==menuItem_save) + { textArea.requestFocus(); + if(isNewFile) + { String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + + fileChooser.setDialogTitle("淇濆瓨"); + int result=fileChooser.showSaveDialog(this); + if(result==JFileChooser.CANCEL_OPTION) + { statusLabel.setText("鎮ㄦ病鏈夐夋嫨浠讳綍鏂囦欢"); + return; + } + + File saveFileName=fileChooser.getSelectedFile(); + if(saveFileName==null || saveFileName.getName().equals("")) + { JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileWriter fw=new FileWriter(saveFileName); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush();//鍒锋柊璇ユ祦鐨勭紦鍐 + bfw.close(); + isNewFile=false; + currentFile=saveFileName; + oldValue=textArea.getText(); + this.setTitle(saveFileName.getName()+" - 璁颁簨鏈"); + statusLabel.setText("褰撳墠鎵撳紑鏂囦欢锛"+saveFileName.getAbsoluteFile()); + } + catch (IOException ioException) + { + } + } + } + else + { try + { FileWriter fw=new FileWriter(currentFile); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush(); + fw.close(); + } + catch(IOException ioException) + { + } + } + } + + /*鍙﹀瓨涓*/ + else if(e.getSource()==menuItem_saveas) + { textArea.requestFocus(); + String str=null; + JFileChooser fileChooser=new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + + fileChooser.setDialogTitle("鍙﹀瓨涓"); + int result=fileChooser.showSaveDialog(this); + if(result==JFileChooser.CANCEL_OPTION) + { statusLabel.setText("銆鎮ㄦ病鏈夐夋嫨浠讳綍鏂囦欢"); + return; + } + + File saveFileName=fileChooser.getSelectedFile(); + if(saveFileName==null||saveFileName.getName().equals("")) + { JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); + } + else + { try + { FileWriter fw=new FileWriter(saveFileName); + BufferedWriter bfw=new BufferedWriter(fw); + bfw.write(textArea.getText(),0,textArea.getText().length()); + bfw.flush(); + fw.close(); + oldValue=textArea.getText(); + this.setTitle(saveFileName.getName()+" de璁颁簨鏈"); + statusLabel.setText("銆褰撳墠鎵撳紑鏂囦欢:"+saveFileName.getAbsoluteFile()); + + } + catch(IOException ioException) + { + } + } + } + + /*閫鍑*/ + else if(e.getSource()==menuItem_exit) + { int exitChoose=JOptionPane.showConfirmDialog(this,"浣犵湡鐨勭‘瀹氳閫鍑哄悧?淇濆瓨浜嗗悧","閫鍑烘彁绀",JOptionPane.OK_CANCEL_OPTION); + if(exitChoose==JOptionPane.OK_OPTION) + { System.exit(0); + } + else + { return; + } + } + /*鎾ら攢*/ + else if(e.getSource()==menuItem_undo || e.getSource()==popupmenu_undo) + { textArea.requestFocus(); + if(undo.canUndo()) + { try + { undo.undo(); + } + catch (CannotUndoException ex) + { System.out.println("Unable to undo:" + ex); + + } + } + if(!undo.canUndo()) + { menuItem_undo.setEnabled(false); + } + } + + /*鍓垏*/ + else if(e.getSource()==menuItem_cut || e.getSource()==popupmenu_cut) + { textArea.requestFocus(); + String text=textArea.getSelectedText(); + StringSelection selection=new StringSelection(text); + clipBoard.setContents(selection,null); + textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd()); + checkMenuItemEnabled();//璁剧疆鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ゅ姛鑳界殑鍙敤鎬 + } + + /*澶嶅埗*/ + else if(e.getSource()==menuItem_copy || e.getSource()==popupmenu_copy) + { textArea.requestFocus(); + String text=textArea.getSelectedText(); + StringSelection selection=new StringSelection(text); + clipBoard.setContents(selection,null); + checkMenuItemEnabled();//璁剧疆鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ゅ姛鑳界殑鍙敤鎬 + } + + /*绮樺笘*/ + else if(e.getSource()==menuItem_paste || e.getSource()==popupmenu_paste) + { textArea.requestFocus(); + Transferable contents=clipBoard.getContents(this); + if(contents==null)return; + String text=""; + try + { text=(String)contents.getTransferData(DataFlavor.stringFlavor); + } + catch (Exception exception) + { + } + textArea.replaceRange(text,textArea.getSelectionStart(),textArea.getSelectionEnd()); + checkMenuItemEnabled(); + } + + /*鍒犻櫎*/ + else if(e.getSource()==menuItem_delete || e.getSource()==popupmenu_delete) + { textArea.requestFocus(); + textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd()); + checkMenuItemEnabled(); //璁剧疆鍓垏銆佸鍒躲佺矘璐淬佸垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ + } + + /*鏌ユ壘*/ + else if(e.getSource()==menuItem_find) + { textArea.requestFocus(); + find(); + } + + /*鏇挎崲*/ + else if(e.getSource()==menuItem_replace) + { textArea.requestFocus(); + replace(); + }//鏇挎崲缁撴潫 + + /*鏃堕棿鏃ユ湡*/ + else if(e.getSource()==menuItem_time) + { textArea.requestFocus(); +// SimpleDateFormat currentDateTime=new SimpleDateFormat(" yyyy/MM/dd HH:mm:ss "); +// textArea.insert(currentDateTime.format(new Date()),textArea.getCaretPosition()); + Calendar rightNow=Calendar.getInstance(); + java.util.Date date=rightNow.getTime(); + textArea.insert(date.toString(),textArea.getCaretPosition()); + } + + /*鍏ㄩ*/ + else if(e.getSource()==menuItem_selectall || e.getSource()==popupmenu_selectall) + { textArea.selectAll(); + } + + /*鑷姩鎹㈣*/ + else if(e.getSource()==menuItem_linewrap) + { if(menuItem_linewrap.getState()) + textArea.setLineWrap(true); + else + textArea.setLineWrap(false); + + } + + /*瀛椾綋璁剧疆*/ + else if(e.getSource()==menuItem_font) + { textArea.requestFocus(); + font(); + } + + /*鐘舵佹爮璁剧疆*/ + else if(e.getSource()==menuItem_status) + { if(menuItem_status.getState()) + statusLabel.setVisible(true); + else + statusLabel.setVisible(false); + } + + + + /*甯姪*/ + else if(e.getSource()==menuItem_HelpTopics) + { textArea.requestFocus(); + JOptionPane.showMessageDialog(this,"浼樼涓嶆槸涓绉嶈涓猴紝鑰屾槸涓绉嶄範鎯紒","鏌ョ湅甯姪",JOptionPane.INFORMATION_MESSAGE); + } + + /*鍏充簬*/ + else if(e.getSource()==menuItem_AboutNoteBook) + { textArea.requestFocus(); + JOptionPane.showMessageDialog(this, + "璁颁簨鏈腑鐨勫畾鏃跺櫒鍔熻兘浣跨敤璇存槑\n" + + "1.鈥滅‘璁も濃斺斺斺斿紑濮嬭鏃跺櫒\n锛堢‘璁ら棿闅旀椂闂村悗锛屽埌闂撮殧鏃堕棿灏变細寮瑰嚭绐楀彛鎻愰啋锛岃嫢鎸夆滄槸鈥濆垯缁х画浼戞伅璇ラ棿闅旀椂闂达級\n2.鈥滃彇娑堚濃斺斺斺旂粨鏉熻鏃跺櫒\n" + +"璁℃椂鍣ㄥ紑鍚悗锛屽垯涓嶈兘瀵硅浜嬫湰杩涜缂栬緫\n鎵浠ワ紝璇峰ソ濂戒紤鎭竴涓嬬溂鐫涘惂锛", + "鍏充簬",JOptionPane.INFORMATION_MESSAGE); + } + + /*瀹氭椂鍣*/ + else if(e.getSource()==settime) + { + Clock(); + } + } + + /*瀹炵幇DocumentListener鎺ュ彛涓殑鏂规硶*/ + public void removeUpdate(DocumentEvent e) + { menuItem_undo.setEnabled(true); + } + public void insertUpdate(DocumentEvent e) + { menuItem_undo.setEnabled(true); + } + public void changedUpdate(DocumentEvent e) + { menuItem_undo.setEnabled(true); + } + + + /*瀹炵幇鎺ュ彛UndoableEditListener鐨勭被UndoHandler*/ + class UndoHandler implements UndoableEditListener + { public void undoableEditHappened(UndoableEditEvent uee) + { undo.addEdit(uee.getEdit()); + } + } + + + + + } + + + + + + + -- Gitee From 098b69fcd42d7e56f19f3e0f9854c717e88c19d7 Mon Sep 17 00:00:00 2001 From: lenovo Date: Fri, 11 Jun 2021 12:03:54 +0800 Subject: [PATCH 14/19] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/TimeNoteBook/Notebook.java | 1353 -------------------------------- 1 file changed, 1353 deletions(-) delete mode 100644 src/TimeNoteBook/Notebook.java diff --git a/src/TimeNoteBook/Notebook.java b/src/TimeNoteBook/Notebook.java deleted file mode 100644 index cf1a85d..0000000 --- a/src/TimeNoteBook/Notebook.java +++ /dev/null @@ -1,1353 +0,0 @@ -package TimeNoteBook; - -import java.awt.*; -import java.awt.datatransfer.Clipboard; -import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.StringSelection; -import java.awt.datatransfer.Transferable; -import java.io.Serializable ; - -import javax.swing.*; -import javax.swing.event.DocumentEvent; -import javax.swing.event.DocumentListener; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; -import javax.swing.event.MenuEvent; -import javax.swing.event.MenuListener; -import javax.swing.event.UndoableEditEvent; -import javax.swing.event.UndoableEditListener; -import javax.swing.undo.CannotUndoException; -import javax.swing.undo.UndoManager; -import java.io.*; - -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.Enumeration; - -import javax.swing.*; -import java.awt.event.*; -import java.beans.EventHandler; - -public class Notebook extends JFrame implements DocumentListener,ActionListener { - public static void main(String args[]) - { Notebook notebook=new Notebook(); - notebook.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//浣跨敤 System exit 鏂规硶閫鍑哄簲鐢ㄧ▼搴 - } - private static Icon statusBar_status; - public static final Color doushalv = new Color(199,237,204);//鏂板缓棰滆壊涓烘姢鐪艰眴娌欑豢 - - /*鑿滃崟*/ - private JMenu menu_file; //鏂囦欢鎸夐挳 - private JMenu menu_edit; //缂栬緫鎸夐挳 - private JMenu menu_format; //鏍煎紡鎸夐挳 - private JMenu menu_help; //甯姪鎸夐挳 - private JMenu menu_view; //鏌ョ湅鎸夐挳 - private JMenu menu_settime; //瀹氭椂鍣ㄦ寜閽 - - /*鍙抽敭寮瑰嚭鑿滃崟*/ - private JPopupMenu popupMenu; - private JMenuItem popupmenu_undo; - private JMenuItem popupmenu_cut; - private JMenuItem popupmenu_copy; - private JMenuItem popupmenu_paste; - private JMenuItem popupmenu_delete; - private JMenuItem popupmenu_selectall; - - /*鏂囦欢鑿滃崟*/ - private JMenuItem menuItem_new; - private JMenuItem menuItem_open; - private JMenuItem menuItem_save; - private JMenuItem menuItem_saveas; - private JMenuItem menuItem_exit; - - /*缂栬緫鑿滃崟*/ - private JMenuItem menuItem_undo; - private JMenuItem menuItem_time; - private JMenuItem menuItem_find; - private JMenuItem menuItem_replace; - private JMenuItem menuItem_paste; - private JMenuItem menuItem_selectall; - private JMenuItem menuItem_cut; - private JMenuItem menuItem_copy; - private JMenuItem menuItem_delete; - - /*鏍煎紡鑿滃崟*/ - private JCheckBoxMenuItem menuItem_linewrap; - private JMenuItem menuItem_font; - - /*鏌ョ湅鐨勮彍鍗*/ - private JCheckBoxMenuItem menuItem_status; - - /*甯姪鐨勮彍鍗*/ - private JMenuItem menuItem_HelpTopics; - private JMenuItem menuItem_AboutNoteBook; - - /*瀹氭椂鍣*/ - private JMenuItem settime; - - /*鏂囨湰缂栬緫鍖哄煙*/ - private JTextArea textArea; - - /*鐘舵佹爮*/ - private JLabel statusLabel; - /*鑿滃崟*/ - private JMenuBar menuBar; - private JLabel labelTime; - Toolkit toolkit=Toolkit.getDefaultToolkit(); - Clipboard clipBoard=toolkit.getSystemClipboard(); - - /*鍒涘缓鎾ら攢鎿嶄綔绠$悊鍣*/ - protected UndoManager undo=new UndoManager(); - protected UndoableEditListener undoHandler=new UndoHandler();//鍏朵粬鍙橀噺 - private String oldValue;//瀛樻斁缂栬緫鍖哄師鏉ョ殑鍐呭锛岀敤浜庢瘮杈冩枃鏈槸鍚︽湁鏀瑰姩 - boolean isNewFile=true;//鏄惁鏂版枃浠(鏈繚瀛樿繃鐨) - File currentFile; - private JLabel label; - - //褰撳墠鏂囦欢鍚 - - public Notebook(){ - super("鎶ょ溂璁颁簨鏈"); - Font font=new Font("Courier Prime", Font.PLAIN, 12); //鏀瑰彉绯荤粺榛樿瀛椾綋 - Enumeration keys = UIManager.getDefaults().keys(); - while (keys.hasMoreElements()) { - Object key = keys.nextElement(); - Object value = UIManager.get(key); - if (value instanceof javax.swing.plaf.FontUIResource) { - UIManager.put(key,font); - } - } - - - JMenuBar menuBar=new JMenuBar(); //鍒涘缓鑿滃崟鏉 - - /*鍒涘缓鏂囦欢鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚*/ - menu_file=new JMenu("鏂囦欢"); - - menuItem_new=new JMenuItem("鏂板缓"); - menuItem_new.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); - menuItem_new.addActionListener(this); - - menuItem_open=new JMenuItem("鎵撳紑"); - menuItem_open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); - menuItem_open.addActionListener(this); - - menuItem_save=new JMenuItem("淇濆瓨"); - menuItem_save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); - menuItem_save.addActionListener(this); - - menuItem_saveas=new JMenuItem("鍙﹀瓨涓"); - menuItem_saveas.addActionListener(this); - - menuItem_exit=new JMenuItem("閫鍑"); - menuItem_exit.addActionListener(this); - - /*鍒涘缓缂栬緫鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚*/ - menu_edit=new JMenu("缂栬緫"); - menu_edit.setMnemonic('E');//璁剧疆蹇嵎閿瓵LT+E - - /*褰撻夋嫨缂栬緫鑿滃崟鏃讹紝璁剧疆鍓垏銆佸鍒躲佺矘璐淬佸垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ*/ - - menu_edit.addMenuListener(new MenuListener() - { public void menuCanceled(MenuEvent e)//鍙栨秷鑿滃崟鏃惰皟鐢 - { checkMenuItemEnabled();//璁剧疆鍓垏銆佸鍒躲佺矘璐淬佸垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ - } - public void menuDeselected(MenuEvent e)//鍙栨秷閫夋嫨鏌愪釜鑿滃崟鏃惰皟鐢 - { checkMenuItemEnabled();//璁剧疆鍓垏銆佸鍒躲佺矘璐淬佸垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ - } - public void menuSelected(MenuEvent e)//閫夋嫨鏌愪釜鑿滃崟鏃惰皟鐢 - { checkMenuItemEnabled();//璁剧疆鍓垏銆佸鍒躲佺矘璐淬佸垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ - } - }); - - menuItem_undo=new JMenuItem("鎾ら攢"); - menuItem_undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK)); - menuItem_undo.addActionListener(this); - menuItem_undo.setEnabled(false); - - menuItem_cut=new JMenuItem("鍓垏"); - menuItem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK)); - menuItem_cut.addActionListener(this); - - menuItem_copy=new JMenuItem("澶嶅埗"); - menuItem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); - menuItem_copy.addActionListener(this); - - menuItem_paste=new JMenuItem("绮樿创"); - menuItem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); - menuItem_paste.addActionListener(this); - - menuItem_delete=new JMenuItem("鍒犻櫎"); - menuItem_delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0)); - menuItem_delete.addActionListener(this); - - menuItem_find=new JMenuItem("鏌ユ壘"); - menuItem_find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK)); - menuItem_find.addActionListener(this); - - menuItem_replace = new JMenuItem("鏇挎崲"); - menuItem_replace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK)); - menuItem_replace.addActionListener(this); - - menuItem_selectall = new JMenuItem("鍏ㄩ"); - menuItem_selectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); - menuItem_selectall.addActionListener(this); - - menuItem_time = new JMenuItem("鏃堕棿/鏃ユ湡"); - menuItem_time.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0)); - menuItem_time.addActionListener(this); - - /*鍒涘缓鏍煎紡鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚*/ - menu_format=new JMenu("鏍煎紡"); - menu_format.setMnemonic('O');//璁剧疆蹇嵎閿瓵LT+O - - menuItem_linewrap=new JCheckBoxMenuItem("鑷姩鎹㈣"); - menuItem_linewrap.setMnemonic('W');//璁剧疆蹇嵎閿瓵LT+W - menuItem_linewrap.setState(true); - menuItem_linewrap.addActionListener(this); - - menuItem_font=new JMenuItem("瀛椾綋"); - menuItem_font.addActionListener(this); - - /*鍒涘缓鏌ョ湅鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚*/ - menu_view=new JMenu("鏌ョ湅"); - menu_view.setMnemonic('V');//璁剧疆蹇嵎閿瓵LT+V - - menuItem_status=new JCheckBoxMenuItem("鐘舵佹爮"); - menuItem_status.setMnemonic('S');//璁剧疆蹇嵎閿瓵LT+S - menuItem_status.setState(true); - menuItem_status.addActionListener(this); - - /*鍒涘缓甯姪鑿滃崟鍙婅彍鍗曢」骞舵敞鍐屼簨浠剁洃鍚*/ - menu_help = new JMenu("甯姪"); - menu_help.setMnemonic('H');//璁剧疆蹇嵎閿瓵LT+H - - menuItem_HelpTopics = new JMenuItem("甯姪涓婚"); - menuItem_HelpTopics.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0)); - menuItem_HelpTopics.addActionListener(this); - - menuItem_AboutNoteBook = new JMenuItem("鍏充簬璁颁簨鏈"); - menuItem_AboutNoteBook.addActionListener(this); - - /*瀹氭椂鍣*/ - menu_settime=new JMenu ("瀹氭椂鍣"); - settime=new JMenuItem("瀹氭椂鍣"); - settime.addActionListener(this); - - /*鍚戣彍鍗曟潯娣诲姞"鏂囦欢"鑿滃崟鍙婅彍鍗曢」*/ - menuBar.add(menu_file); - menu_file.add(menuItem_new); - menu_file.add(menuItem_open); - menu_file.add(menuItem_save); - menu_file.add(menuItem_saveas); - menu_file.add(menuItem_exit); - - /*鍚戣彍鍗曟潯娣诲姞"缂栬緫"鑿滃崟鍙婅彍鍗曢」 */ - menuBar.add(menu_edit); - menu_edit.add(menuItem_undo); - menu_edit.add(menuItem_cut); - menu_edit.add(menuItem_copy); - menu_edit.add(menuItem_paste); - menu_edit.add(menuItem_delete); - menu_edit.add(menuItem_find); - menu_edit.add(menuItem_replace); - menu_edit.add(menuItem_selectall); - menu_edit.add(menuItem_time); - - /*鍚戣彍鍗曟潯娣诲姞"鏍煎紡"鑿滃崟鍙婅彍鍗曢」*/ - menuBar.add(menu_format); - menu_format.add(menuItem_linewrap); - menu_format.add(menuItem_font); - - /*鍚戣彍鍗曟潯娣诲姞"鏌ョ湅"鑿滃崟鍙婅彍鍗曢」 */ - menuBar.add(menu_view); - menu_view.add(menuItem_status); - - /*鍚戣彍鍗曟潯娣诲姞"甯姪"鑿滃崟鍙婅彍鍗曢」*/ - menuBar.add(menu_help); - menu_help.add(menuItem_HelpTopics); - menu_help.addSeparator(); - menu_help.add(menuItem_AboutNoteBook); - - /*鍚戣彍鍗曟潯娣诲姞鈥滃畾鏃跺櫒鈥濊彍鍗*/ - menuBar.add(menu_settime); - menu_settime.add(settime); - - /*鍚戠獥鍙f坊鍔犺彍鍗曟潯*/ - this.setJMenuBar(menuBar); - - /*鍒涘缓鏂囨湰缂栬緫鍖哄苟娣诲姞婊氬姩鏉*/ - textArea=new JTextArea(20,50); - JScrollPane scroller=new JScrollPane(textArea); - scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); - this.add(scroller,BorderLayout.CENTER);//鍚戠獥鍙f坊鍔犳枃鏈紪杈戝尯 - textArea.setBackground(doushalv); - textArea.setWrapStyleWord(true);//璁剧疆鍗曡瘝鍦ㄤ竴琛屼笉瓒冲绾虫椂鎹㈣ - textArea.setLineWrap(true);//璁剧疆鏂囨湰缂栬緫鍖鸿嚜鍔ㄦ崲琛岄粯璁や负true,鍗充細"鑷姩鎹㈣" - oldValue=textArea.getText();//鑾峰彇鍘熸枃鏈紪杈戝尯鐨勫唴瀹 - - /*缂栬緫鍖烘敞鍐屼簨浠剁洃鍚(涓庢挙閿鎿嶄綔鏈夊叧)*/ - textArea.getDocument().addUndoableEditListener(undoHandler); - textArea.getDocument().addDocumentListener(this); - - - /*鍒涘缓鍙抽敭寮瑰嚭鑿滃崟*/ - popupMenu=new JPopupMenu(); - popupmenu_undo=new JMenuItem("鎾ら攢"); - popupmenu_cut=new JMenuItem("鍓垏"); - popupmenu_copy=new JMenuItem("澶嶅埗"); - popupmenu_paste=new JMenuItem("绮樺笘"); - popupmenu_delete=new JMenuItem("鍒犻櫎"); - popupmenu_selectall=new JMenuItem("鍏ㄩ"); - popupmenu_undo.setEnabled(false); - - /*鍚戝彸閿彍鍗曟坊鍔犺彍鍗曢」鍜屽垎闅旂*/ - popupMenu.add(popupmenu_undo); - popupMenu.addSeparator(); - popupMenu.add(popupmenu_cut); - popupMenu.add(popupmenu_copy); - popupMenu.add(popupmenu_paste); - popupMenu.add(popupmenu_delete); - popupMenu.addSeparator(); - popupMenu.add(popupmenu_selectall); - - /*鏂囨湰缂栬緫鍖烘敞鍐屽彸閿彍鍗曚簨浠*/ - popupmenu_undo.addActionListener(this); - popupmenu_cut.addActionListener(this); - popupmenu_copy.addActionListener(this); - popupmenu_paste.addActionListener(this); - popupmenu_delete.addActionListener(this); - popupmenu_selectall.addActionListener(this); - - /*鏂囨湰缂栬緫鍖烘敞鍐屽彸閿彍鍗曚簨浠*/ - textArea.addMouseListener(new MouseAdapter() - { public void mousePressed(MouseEvent e) - { if(e.isPopupTrigger())//杩斿洖姝ら紶鏍囦簨浠舵槸鍚︿负璇ュ钩鍙扮殑寮瑰嚭鑿滃崟瑙﹀彂浜嬩欢 - { popupMenu.show(e.getComponent(),e.getX(),e.getY());//鍦ㄧ粍浠惰皟鐢ㄨ呯殑鍧愭爣绌洪棿涓殑浣嶇疆 X銆乊 鏄剧ず寮瑰嚭鑿滃崟 - } - checkMenuItemEnabled();//璁剧疆鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ - textArea.requestFocus();//缂栬緫鍖鸿幏鍙栫劍鐐 - } - public void mouseReleased(MouseEvent e) - { if(e.isPopupTrigger())//杩斿洖姝ら紶鏍囦簨浠舵槸鍚︿负璇ュ钩鍙扮殑寮瑰嚭鑿滃崟瑙﹀彂浜嬩欢 - { popupMenu.show(e.getComponent(),e.getX(),e.getY());//鍦ㄧ粍浠惰皟鐢ㄨ呯殑鍧愭爣绌洪棿涓殑浣嶇疆 X銆乊 鏄剧ず寮瑰嚭鑿滃崟 - } - checkMenuItemEnabled();//璁剧疆鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ - textArea.requestFocus();//缂栬緫鍖鸿幏鍙栫劍鐐 - } - }); - - - - /*鍒涘缓鍜屾坊鍔犵姸鎬佹爮*/ - JToolBar statusLabel=new JToolBar(); - statusLabel.setFloatable(false); - SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd 浠婂勾鐨勭DD澶"); - labelTime = new JLabel("鏃ユ湡: "+sdf.format(new Date())); - statusLabel.addSeparator(new Dimension(180,5)); - statusLabel.add(labelTime); - statusLabel.setVisible(true); - - this.add(statusLabel,BorderLayout.SOUTH);//鍚戠獥鍙f坊鍔犵姸鎬佹爮鏍囩 - this.setLocation(100,100);//璁剧疆绐楀彛鍦ㄥ睆骞曚笂鐨勪綅缃 - this.setSize(650,550);//璁剧疆绐楀彛鍦ㄥ睆骞曚笂鐨勫ぇ灏 - this.setVisible(true);//璁剧疆绐楀彛鍦ㄥ睆骞曚笂鐨勫彲瑙佹 - - /* - * 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(); // 涓鏃︽枃鏈湁鏀瑰彉灏辫缃悇鎸夐挳鐨勫彲鐢ㄦ } });} public void - * changeTextLengthStatus(){ // 鏂囨湰鐩戝惉 String content = textArea.getText().trim(); - * label.setText("褰撳墠瀛楁暟:"+content.length()); } private void isItemsAvalible(){ // - * 鐩戣鏂囨湰鍖哄苟璁剧疆鍚勫姛鑳介」鏄惁鍙敤 String content = textArea.getText(); if(content.equals("")){ - * menuItem_cut.setEnabled(false); menuItem_delete.setEnabled(false); - * menuItem_copy.setEnabled(false); menuItem_save.setEnabled(false); - * menuItem_saveas.setEnabled(false); popupmenu_copy.setEnabled(false); - * popupmenu_cut.setEnabled(false); }else{ menuItem_save.setEnabled(true); - * menuItem_cut.setEnabled(true); menuItem_delete.setEnabled(true); - * menuItem_copy.setEnabled(true); popupmenu_copy.setEnabled(true); - * popupmenu_cut.setEnabled(true); - * - * } - */ - - - - /*娣诲姞绐楀彛鐩戝惉鍣*/ - addWindowListener(new WindowAdapter() - { public void windowClosing(WindowEvent e) - { exitWindowChoose(); - } - }); - checkMenuItemEnabled(); - textArea.requestFocus();} - - - - - - - /*璁剧疆鑿滃崟椤圭殑鍙敤鎬э細鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ゅ姛鑳*/ - public void checkMenuItemEnabled() - { String selectText=textArea.getSelectedText(); - if(selectText==null) - { menuItem_cut.setEnabled(false); - popupmenu_cut.setEnabled(false); - menuItem_copy.setEnabled(false); - popupmenu_copy.setEnabled(false); - menuItem_delete.setEnabled(false); - popupmenu_delete.setEnabled(false); - } - else - { menuItem_cut.setEnabled(true); - popupmenu_cut.setEnabled(true); - menuItem_copy.setEnabled(true); - popupmenu_copy.setEnabled(true); - menuItem_delete.setEnabled(true); - popupmenu_delete.setEnabled(true); - } - /*绮樺笘鍔熻兘鍙敤鎬у垽鏂*/ - Transferable contents=clipBoard.getContents(this); - if(contents==null) - { menuItem_paste.setEnabled(false); - popupmenu_paste.setEnabled(false); - } - else - { menuItem_paste.setEnabled(true); - popupmenu_paste.setEnabled(true); - } - } - - /*鍏抽棴绐楀彛鏃惰皟鐢*/ - public void exitWindowChoose() - { textArea.requestFocus(); - String currentValue=textArea.getText(); - if(currentValue.equals(oldValue)==true) - { System.exit(0); - } - else - { int exitChoose=JOptionPane.showConfirmDialog(this,"浣犵殑鏂囦欢杩樻病淇濆瓨鍝:)","閫鍑!!",JOptionPane.YES_NO_CANCEL_OPTION); - if(exitChoose==JOptionPane.YES_OPTION) - { boolean isSave=false; - if(isNewFile) - { - String str=null; - JFileChooser fileChooser=new JFileChooser(); - fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - fileChooser.setApproveButtonText("!!!纭畾!!!"); - fileChooser.setDialogTitle("鍙﹀瓨涓"); - - int result=fileChooser.showSaveDialog(this); - - File saveFileName=fileChooser.getSelectedFile(); - - if(result==JFileChooser.CANCEL_OPTION) - { statusLabel.setText("銆鎮ㄦ病鏈変繚瀛樻枃浠"); - return; - } - - if(saveFileName==null||saveFileName.getName().equals("")) - { JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚!","涓嶅悎娉曠殑鏂囦欢鍚!",JOptionPane.ERROR_MESSAGE); - } - else - { try - { FileWriter fw=new FileWriter(saveFileName); - BufferedWriter bfw=new BufferedWriter(fw); - bfw.write(textArea.getText(),0,textArea.getText().length()); - bfw.flush(); - fw.close(); - - isNewFile=false; - currentFile=saveFileName; - oldValue=textArea.getText(); - - this.setTitle(saveFileName.getName()+" de璁颁簨鏈"); - statusLabel.setText("銆褰撳墠鎵撳紑鏂囦欢:"+saveFileName.getAbsoluteFile()); - isSave=true; - } - catch(IOException ioException){ - } - } - } - else - { - try - { FileWriter fw=new FileWriter(currentFile); - BufferedWriter bfw=new BufferedWriter(fw); - bfw.write(textArea.getText(),0,textArea.getText().length()); - bfw.flush(); - fw.close(); - isSave=true; - } - catch(IOException ioException){ - } - } - System.exit(0); - if(isSave)System.exit(0); - else return; - } - else if(exitChoose==JOptionPane.NO_OPTION) - { System.exit(0); - } - else - { return; - } - } - } - - /*鏌ユ壘鏂规硶*/ - public void find() - { final JDialog findDialog=new JDialog(this,"鏌ユ壘",false);//false鏃跺厑璁稿叾浠栫獥鍙e悓鏃跺浜庢縺娲荤姸鎬(鍗虫棤妯″紡) - Container con=findDialog.getContentPane();//杩斿洖姝ゅ璇濇鐨刢ontentPane瀵硅薄 - con.setLayout(new FlowLayout(FlowLayout.LEFT)); - JLabel findContentLabel=new JLabel("鏌ユ壘鍐呭锛"); - final JTextField findText=new JTextField(15); - JButton findNextButton=new JButton("鏌ユ壘"); - final JCheckBox matchCheckBox=new JCheckBox("鍖哄垎澶у皬鍐"); - ButtonGroup bGroup=new ButtonGroup(); - final JRadioButton upButton=new JRadioButton("寰涓"); - final JRadioButton downButton=new JRadioButton("寰涓"); - downButton.setSelected(true); - bGroup.add(upButton); - bGroup.add(downButton); - JButton cancel=new JButton("鍙栨秷");//鍙栨秷鎸夐挳浜嬩欢 - cancel.addActionListener(new ActionListener() - { public void actionPerformed(ActionEvent e) - { findDialog.dispose(); - } - }); - - /*"鏌ユ壘涓嬩竴涓"鎸夐挳鐩戝惉*/ - findNextButton.addActionListener(new ActionListener() - { public void actionPerformed(ActionEvent e) - { //"鍖哄垎澶у皬鍐"鐨凧CheckBox鏄惁琚変腑 - int k=0,m=0; - final String str1,str2,str3,str4,strA,strB; - str1=textArea.getText(); - str2=findText.getText(); - str3=str1.toUpperCase(); - str4=str2.toUpperCase(); - if(matchCheckBox.isSelected())//鍖哄垎澶у皬鍐 - { strA=str1; - strB=str2; - } - else//涓嶅尯鍒嗗ぇ灏忓啓,姝ゆ椂鎶婃墍閫夊唴瀹瑰叏閮ㄥ寲鎴愬ぇ鍐(鎴栧皬鍐)锛屼互渚夸簬鏌ユ壘 - { strA=str3; - strB=str4; - } - if(upButton.isSelected()) - { - if(textArea.getSelectedText()==null) - k=strA.lastIndexOf(strB,textArea.getCaretPosition()-1); - else - k=strA.lastIndexOf(strB, textArea.getCaretPosition()-findText.getText().length()-1); - if(k>-1) - { - textArea.setCaretPosition(k); - textArea.select(k,k+strB.length()); - } - else - { JOptionPane.showMessageDialog(null,"鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹癸紒:(","鏌ユ壘",JOptionPane.INFORMATION_MESSAGE); - } - } - else if(downButton.isSelected()) - { if(textArea.getSelectedText()==null) - k=strA.indexOf(strB,textArea.getCaretPosition()+1); - else - k=strA.indexOf(strB, textArea.getCaretPosition()-findText.getText().length()+1); - if(k>-1) - { - textArea.setCaretPosition(k); - textArea.select(k,k+strB.length()); - } - else - { JOptionPane.showMessageDialog(null,"鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹癸紒:(","鏌ユ壘",JOptionPane.INFORMATION_MESSAGE); - } - } - } - }); - - /*鍒涘缓"鏌ユ壘"瀵硅瘽妗嗙殑鐣岄潰*/ - JPanel panel1=new JPanel(); - JPanel panel2=new JPanel(); - JPanel panel3=new JPanel(); - JPanel directionPanel=new JPanel(); - directionPanel.setBorder(BorderFactory.createTitledBorder("浣嶇疆")); //璁剧疆directionPanel缁勪欢鐨勮竟妗; - directionPanel.add(upButton); - directionPanel.add(downButton); - panel1.setLayout(new GridLayout(2,1)); - panel1.add(findNextButton); - panel1.add(cancel); - panel2.add(findContentLabel); - panel2.add(findText); - panel2.add(panel1); - panel3.add(matchCheckBox); - panel3.add(directionPanel); - con.add(panel2); - con.add(panel3); - findDialog.setSize(410,180); - findDialog.setResizable(false);//涓嶅彲璋冩暣澶у皬 - findDialog.setLocation(230,280); - findDialog.setVisible(true); - } - - /*鏇挎崲鏂规硶*/ - public void replace() - { final JDialog replaceDialog=new JDialog(this,"鏇挎崲",false);//false鏃跺厑璁稿叾浠栫獥鍙e悓鏃跺浜庢縺娲荤姸鎬(鍗虫棤妯″紡) - Container con=replaceDialog.getContentPane();//杩斿洖姝ゅ璇濇鐨刢ontentPane瀵硅薄 - con.setLayout(new FlowLayout(FlowLayout.CENTER)); - JLabel findContentLabel=new JLabel("鏌ユ壘鍐呭锛"); - final JTextField findText=new JTextField(15); - JButton findNextButton=new JButton("鏌ユ壘"); - JLabel replaceLabel=new JLabel("鏇挎崲涓猴細"); - final JTextField replaceText=new JTextField(15); - JButton replaceButton=new JButton("鏇挎崲"); - JButton replaceAllButton=new JButton("鍏ㄩ儴鏇挎崲"); - JButton cancel=new JButton("鍙栨秷"); - cancel.addActionListener(new ActionListener() - { public void actionPerformed(ActionEvent e) - { replaceDialog.dispose(); - } - }); - final JCheckBox matchCheckBox=new JCheckBox("鍖哄垎澶у皬鍐"); - ButtonGroup bGroup=new ButtonGroup(); - final JRadioButton upButton=new JRadioButton("寰涓"); - final JRadioButton downButton=new JRadioButton("寰涓"); - downButton.setSelected(true); - bGroup.add(upButton); - bGroup.add(downButton); - - - - /*"鏇挎崲"鎸夐挳鐩戝惉*/ - replaceButton.addActionListener(new ActionListener() - { public void actionPerformed(ActionEvent e) - { if(replaceText.getText().length()==0 && textArea.getSelectedText()!=null) - textArea.replaceSelection(""); - if(replaceText.getText().length()>0 && textArea.getSelectedText()!=null) - textArea.replaceSelection(replaceText.getText()); - } - });//"鏇挎崲"鎸夐挳鐩戝惉缁撴潫 - - /*"鍏ㄩ儴鏇挎崲"鎸夐挳鐩戝惉*/ - replaceAllButton.addActionListener(new ActionListener() - { public void actionPerformed(ActionEvent e) - { textArea.setCaretPosition(0); //灏嗗厜鏍囨斁鍒扮紪杈戝尯寮澶 - int k=0,m=0,replaceCount=0; - if(findText.getText().length()==0) - { JOptionPane.showMessageDialog(replaceDialog,"璇峰~鍐欐煡鎵惧唴瀹!","鎻愮ず",JOptionPane.WARNING_MESSAGE); - findText.requestFocus(true); - return; - } - while(k>-1)//褰撴枃鏈腑鏈夊唴瀹硅閫変腑鏃(k>-1琚変腑)杩涜鏇挎崲锛屽惁鍒欎笉杩涜while寰幆 - { - final String str1,str2,str3,str4,strA,strB; - str1=textArea.getText(); - str2=findText.getText(); - str3=str1.toUpperCase(); - str4=str2.toUpperCase(); - if(matchCheckBox.isSelected())//鍖哄垎澶у皬鍐 - { strA=str1; - strB=str2; - } - else//涓嶅尯鍒嗗ぇ灏忓啓,姝ゆ椂鎶婃墍閫夊唴瀹瑰叏閮ㄥ寲鎴愬ぇ鍐(鎴栧皬鍐)锛屼互渚夸簬鏌ユ壘 - { strA=str3; - strB=str4; - } - if(upButton.isSelected()) - { - if(textArea.getSelectedText()==null) - k=strA.lastIndexOf(strB,textArea.getCaretPosition()-1); - else - k=strA.lastIndexOf(strB, textArea.getCaretPosition()-findText.getText().length()-1); - if(k>-1) - { - textArea.setCaretPosition(k); - textArea.select(k,k+strB.length()); - } - else - { if(replaceCount==0) - { JOptionPane.showMessageDialog(replaceDialog, "鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹!", "璁颁簨鏈",JOptionPane.INFORMATION_MESSAGE); - } - else - { JOptionPane.showMessageDialog(replaceDialog,"鎴愬姛鏇挎崲"+replaceCount+"涓尮閰嶅唴瀹!","鏇挎崲鎴愬姛(鈮р垏鈮)",JOptionPane.INFORMATION_MESSAGE); - } - } - } - else if(downButton.isSelected()) - { if(textArea.getSelectedText()==null) - k=strA.indexOf(strB,textArea.getCaretPosition()+1); - else - k=strA.indexOf(strB, textArea.getCaretPosition()-findText.getText().length()+1); - if(k>-1) - { - textArea.setCaretPosition(k); - textArea.select(k,k+strB.length()); - } - else - { if(replaceCount==0) - { JOptionPane.showMessageDialog(replaceDialog, "鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹!:(", "璁颁簨鏈",JOptionPane.INFORMATION_MESSAGE); - } - else - { JOptionPane.showMessageDialog(replaceDialog,"鎴愬姛鏇挎崲"+replaceCount+"涓尮閰嶅唴瀹!","鏇挎崲鎴愬姛(鈮р垏鈮)",JOptionPane.INFORMATION_MESSAGE); - } - } - } - if(replaceText.getText().length()==0 && textArea.getSelectedText()!= null) - { textArea.replaceSelection(""); - replaceCount++; - } - - if(replaceText.getText().length()>0 && textArea.getSelectedText()!= null) - { textArea.replaceSelection(replaceText.getText()); - replaceCount++; - } - } - } - }); - - /*鍒涘缓"鏇挎崲"瀵硅瘽妗嗙殑鐣岄潰*/ - JPanel directionPanel=new JPanel(); - directionPanel.setBorder(BorderFactory.createTitledBorder("浣嶇疆"));//璁剧疆directionPanel缁勪欢鐨勮竟妗; - directionPanel.add(upButton); - directionPanel.add(downButton); - JPanel panel1=new JPanel(); - JPanel panel2=new JPanel(); - JPanel panel3=new JPanel(); - JPanel panel4=new JPanel(); - panel4.setLayout(new GridLayout(2,1)); - panel1.add(findContentLabel); - panel1.add(findText); - panel1.add(findNextButton); - panel4.add(replaceButton); - panel4.add(replaceAllButton); - panel2.add(replaceLabel); - panel2.add(replaceText); - panel2.add(panel4); - panel3.add(matchCheckBox); - panel3.add(directionPanel); - panel3.add(cancel); - con.add(panel1); - con.add(panel2); - con.add(panel3); - replaceDialog.setSize(420,220); - replaceDialog.setResizable(false);//涓嶅彲璋冩暣澶у皬 - replaceDialog.setLocation(230,280); - replaceDialog.setVisible(true); - } - - /*"瀛椾綋"鏂规硶*/ - - public void font() - { final JDialog fontDialog=new JDialog(this,"瀛椾綋璁剧疆",false); - Container con=fontDialog.getContentPane(); - con.setLayout(new FlowLayout(FlowLayout.LEFT)); - JLabel fontLabel=new JLabel("瀛椾綋锛"); - fontLabel.setPreferredSize(new Dimension(100,20));//鏋勯犱竴涓狣imension锛屽苟灏嗗叾鍒濆鍖栦负鎸囧畾瀹藉害鍜岄珮搴 - JLabel styleLabel=new JLabel("瀛楀舰锛"); - styleLabel.setPreferredSize(new Dimension(100,20)); - JLabel sizeLabel=new JLabel("澶у皬锛"); - sizeLabel.setPreferredSize(new Dimension(100,20)); - final JLabel sample=new JLabel("鏄綘鎯宠鐨勫瓧浣撳悧锛(*^锛燸*)"); - sample.setHorizontalAlignment(SwingConstants.CENTER); - final JTextField fontText=new JTextField(9); - fontText.setPreferredSize(new Dimension(200,20)); - final JTextField styleText=new JTextField(8); - styleText.setPreferredSize(new Dimension(200,20)); - final int style[]={Font.PLAIN,Font.BOLD,Font.ITALIC,Font.BOLD+Font.ITALIC}; - final JTextField sizeText=new JTextField(5); - sizeText.setPreferredSize(new Dimension(200,20)); - JButton okButton=new JButton("纭畾"); - JButton cancel=new JButton("鍙栨秷"); - cancel.addActionListener(new ActionListener() - { public void actionPerformed(ActionEvent e) - { fontDialog.dispose(); - } - }); - Font currentFont=textArea.getFont(); - fontText.setText(currentFont.getFontName()); - fontText.selectAll(); - if(currentFont.getStyle()==Font.PLAIN) - styleText.setText("甯歌"); - else if(currentFont.getStyle()==Font.BOLD) - styleText.setText("绮椾綋"); - else if(currentFont.getStyle()==Font.ITALIC) - styleText.setText("鏂滀綋"); - else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC)) - styleText.setText("绮楁枩浣"); - styleText.selectAll(); - String str=String.valueOf(currentFont.getSize()); - sizeText.setText(str); - sizeText.selectAll(); - final JList fontList,styleList,sizeList; - GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); - final String fontName[]=ge.getAvailableFontFamilyNames(); - fontList=new JList(fontName); - fontList.setFixedCellWidth(86); - fontList.setFixedCellHeight(20); - fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - final String fontStyle[]={"甯歌","绮椾綋","鏂滀綋","绮楁枩浣"}; - styleList=new JList(fontStyle); - styleList.setFixedCellWidth(86); - styleList.setFixedCellHeight(20); - styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - - if(currentFont.getStyle()==Font.PLAIN) - styleList.setSelectedIndex(0); - else if(currentFont.getStyle()==Font.BOLD) - styleList.setSelectedIndex(1); - else if(currentFont.getStyle()==Font.ITALIC) - styleList.setSelectedIndex(2); - else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC)) - styleList.setSelectedIndex(3); - final String fontSize[]={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"}; - sizeList=new JList(fontSize); - sizeList.setFixedCellWidth(43); - sizeList.setFixedCellHeight(20); - sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - - fontList.addListSelectionListener(new ListSelectionListener() - { public void valueChanged(ListSelectionEvent event) - { fontText.setText(fontName[fontList.getSelectedIndex()]); - fontText.selectAll(); - Font sampleFont1=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); - sample.setFont(sampleFont1); - } - }); - - styleList.addListSelectionListener(new ListSelectionListener() - { public void valueChanged(ListSelectionEvent event) - { int s=style[styleList.getSelectedIndex()]; - styleText.setText(fontStyle[s]); - styleText.selectAll(); - Font sampleFont2=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); - sample.setFont(sampleFont2); - } - }); - - sizeList.addListSelectionListener(new ListSelectionListener() - { public void valueChanged(ListSelectionEvent event) - { sizeText.setText(fontSize[sizeList.getSelectedIndex()]); - sizeText.requestFocus(); - sizeText.selectAll(); - Font sampleFont3=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); - sample.setFont(sampleFont3); - } - }); - okButton.addActionListener(new ActionListener() - { public void actionPerformed(ActionEvent e) - { Font okFont=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText())); - textArea.setFont(okFont); - fontDialog.dispose(); - } - }); - JPanel samplePanel=new JPanel(); - samplePanel.setBorder(BorderFactory.createTitledBorder("绀轰緥")); - samplePanel.setLayout(new FlowLayout(FlowLayout.CENTER)); - samplePanel.add(sample); - JPanel panel1=new JPanel(); - JPanel panel2=new JPanel(); - JPanel panel3=new JPanel(); - panel2.add(fontText); - panel2.add(styleText); - panel2.add(sizeText); - panel2.add(okButton); - panel3.add(new JScrollPane(fontList));//JList涓嶆敮鎸佺洿鎺ユ粴鍔紝鎵浠ヨ璁㎎List浣滀负JScrollPane鐨勮鍙h鍥 - panel3.add(new JScrollPane(styleList)); - panel3.add(new JScrollPane(sizeList)); - panel3.add(cancel); - con.add(panel1); - con.add(panel2); - con.add(panel3); - con.add(samplePanel); - fontDialog.setSize(350,340); - fontDialog.setLocation(200,200); - fontDialog.setResizable(false); - fontDialog.setVisible(true); - } - - - /*瀹氭椂鍣ㄦ柟娉*/ - public void Clock() - { - final JDialog replaceDialog=new JDialog(this,"瀹氭椂鍣",false); - Container con=replaceDialog.getContentPane(); //杩斿洖姝ゅ璇濇鐨刢ontentPane瀵硅薄 - con.setLayout(new FlowLayout(FlowLayout.CENTER)); - JOptionPane.showMessageDialog(null, "鍚姩瀹氭椂鍣"); - System.out.println("鍚姩瀹氭椂鍣紒"); - int setTime = setTime(); - if (-1 != setTime) { - System.out.println("寮濮嬭鏃讹紒"); - long tmil = 60 * 1000 * setTime; - while (true) { - try { - Thread.sleep(tmil); - int confirmDialog = JOptionPane.showConfirmDialog(null, "鍙彯鍙畘", "", JOptionPane.YES_NO_OPTION); - if (1 == confirmDialog) { - break; - } - } catch (InterruptedException e) { - e.printStackTrace(); - break; - } - } - } - JOptionPane.showMessageDialog(null, "缁撴潫瀹氭椂鍣"); - System.out.println("缁撴潫瀹氭椂鍣"); - } - public int setTime() { - String time = JOptionPane.showInputDialog("杈撳叆闂撮殧鏃堕棿(鍒嗛挓0-1440)锛"); - if (time == null) { - return -1; - } - int i = 5; - try {i = Integer.parseInt(time); - if (i < 1) { - throw new Exception("鏃堕棿杈撳叆鑼冨洿涓嶆纭!"); - } - if (i > 1440) { - i = 1440; - throw new Exception("鏃堕棿杈撳叆鑼冨洿涓嶆纭!"); - } - } catch (NumberFormatException e) { - JOptionPane.showMessageDialog(null, "鏃堕棿杈撳叆閿欒锛"); - i = setTime(); - } catch (Exception e) { - JOptionPane.showMessageDialog(null, e.getMessage()); - i = setTime(); - } - return i; - } - - - - public void actionPerformed(ActionEvent e) - { //鏂板缓 - if(e.getSource()==menuItem_new) - { textArea.requestFocus(); - String currentValue=textArea.getText(); - boolean isTextChange=(currentValue.equals(oldValue))?false:true; - if(isTextChange) - { int saveChoose=JOptionPane.showConfirmDialog(this,"鎮ㄧ殑鏂囦欢灏氭湭淇濆瓨锛屾槸鍚︿繚瀛橈紵","鎻愮ず",JOptionPane.YES_NO_CANCEL_OPTION); - if(saveChoose==JOptionPane.YES_OPTION) - { String str=null; - JFileChooser fileChooser=new JFileChooser(); - fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - fileChooser.setDialogTitle("鍙﹀瓨涓"); - int result=fileChooser.showSaveDialog(this); - File saveFileName=fileChooser.getSelectedFile(); - if(saveFileName==null || saveFileName.getName().equals("")) - { JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); - } - else - { try - { FileWriter fw=new FileWriter(saveFileName); - BufferedWriter bfw=new BufferedWriter(fw); - bfw.write(textArea.getText(),0,textArea.getText().length()); - bfw.flush(); - bfw.close(); - isNewFile=false; - currentFile=saveFileName; - oldValue=textArea.getText(); - this.setTitle(saveFileName.getName()+" de 璁颁簨鏈"); - statusLabel.setText("褰撳墠鎵撳紑鏂囦欢锛"+saveFileName.getAbsoluteFile()); - } - catch (IOException ioException) - { - } - } - } - else if(saveChoose==JOptionPane.NO_OPTION) - { textArea.replaceRange("",0,textArea.getText().length()); - statusLabel.setText(" 鏂板缓鏂囦欢"); - this.setTitle("鏃犳爣棰 de璁颁簨鏈"); - isNewFile=true; - undo.discardAllEdits(); //鎾ゆ秷鎵鏈夌殑"鎾ゆ秷"鎿嶄綔 - menuItem_undo.setEnabled(false); - oldValue=textArea.getText(); - } - else if(saveChoose==JOptionPane.CANCEL_OPTION) - { return; - } - } - else - { textArea.replaceRange("",0,textArea.getText().length()); - statusLabel.setText(" 鏂板缓鏂囦欢"); - this.setTitle("鏃犳爣棰 - 璁颁簨鏈"); - isNewFile=true; - undo.discardAllEdits();//鎾ゆ秷鎵鏈夌殑"鎾ゆ秷"鎿嶄綔 - menuItem_undo.setEnabled(false); - oldValue=textArea.getText(); - } - } - - /*鎵撳紑*/ - else if(e.getSource()==menuItem_open) - { textArea.requestFocus(); - String currentValue=textArea.getText(); - boolean isTextChange=(currentValue.equals(oldValue))?false:true; - if(isTextChange) - { int saveChoose=JOptionPane.showConfirmDialog(this,"鎮ㄧ殑鏂囦欢灏氭湭淇濆瓨:(","鎻愮ず",JOptionPane.YES_NO_CANCEL_OPTION); - if(saveChoose==JOptionPane.YES_OPTION) - { String str=null; - JFileChooser fileChooser=new JFileChooser(); - fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - fileChooser.setDialogTitle("鍙﹀瓨涓"); - int result=fileChooser.showSaveDialog(this); - File saveFileName=fileChooser.getSelectedFile(); - if(saveFileName==null || saveFileName.getName().equals("")) - { - statusLabel.setText("鎮ㄦ病鏈夐夋嫨浠讳綍鏂囦欢"); - JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); - } - else - { try - { FileWriter fw=new FileWriter(saveFileName); - BufferedWriter bfw=new BufferedWriter(fw); - bfw.write(textArea.getText(),0,textArea.getText().length()); - bfw.flush();//鍒锋柊璇ユ祦鐨勭紦鍐 - bfw.close(); - isNewFile=false; - currentFile=saveFileName; - oldValue=textArea.getText(); - this.setTitle(saveFileName.getName()+" de璁颁簨鏈"); - statusLabel.setText("褰撳墠鎵撳紑鏂囦欢锛"+saveFileName.getAbsoluteFile()); - } - catch (IOException ioException) - { - } - } - } - else if(saveChoose==JOptionPane.NO_OPTION) - { String str=null; - JFileChooser fileChooser=new JFileChooser(); - fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - fileChooser.setDialogTitle("鎵撳紑鏂囦欢"); - int result=fileChooser.showOpenDialog(this); - File fileName=fileChooser.getSelectedFile(); - if(fileName==null || fileName.getName().equals("")){ - statusLabel.setText("鎮ㄦ病鏈夐夋嫨浠讳綍鏂囦欢"); - JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); - } - else - { try - { FileReader fr=new FileReader(fileName); - BufferedReader bfr=new BufferedReader(fr); - textArea.setText(""); - while((str=bfr.readLine())!=null) - { textArea.append(str); - } - this.setTitle(fileName.getName()+" de 璁颁簨鏈"); - statusLabel.setText(" 褰撳墠鎵撳紑鏂囦欢锛"+fileName.getAbsoluteFile()); - fr.close(); - isNewFile=false; - currentFile=fileName; - oldValue=textArea.getText(); - } - catch (IOException ioException) - { - } - } - } - else - { return; - } - } - else - { String str=null; - JFileChooser fileChooser=new JFileChooser(); - fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - fileChooser.setDialogTitle("鎵撳紑鏂囦欢"); - int result=fileChooser.showOpenDialog(this); - File fileName=fileChooser.getSelectedFile(); - if(fileName==null || fileName.getName().equals("")) - { JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); - } - else - { try - { FileReader fr=new FileReader(fileName); - BufferedReader bfr=new BufferedReader(fr); - textArea.setText(""); - while((str=bfr.readLine())!=null) - { textArea.append(str); - } - this.setTitle(fileName.getName()+" de 璁颁簨鏈"); - statusLabel.setText(" 褰撳墠鎵撳紑鏂囦欢锛"+fileName.getAbsoluteFile()); - fr.close(); - isNewFile=false; - currentFile=fileName; - oldValue=textArea.getText(); - } - catch (IOException ioException) - { - } - } - } - } - - /*淇濆瓨*/ - else if(e.getSource()==menuItem_save) - { textArea.requestFocus(); - if(isNewFile) - { String str=null; - JFileChooser fileChooser=new JFileChooser(); - fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - - fileChooser.setDialogTitle("淇濆瓨"); - int result=fileChooser.showSaveDialog(this); - if(result==JFileChooser.CANCEL_OPTION) - { statusLabel.setText("鎮ㄦ病鏈夐夋嫨浠讳綍鏂囦欢"); - return; - } - - File saveFileName=fileChooser.getSelectedFile(); - if(saveFileName==null || saveFileName.getName().equals("")) - { JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); - } - else - { try - { FileWriter fw=new FileWriter(saveFileName); - BufferedWriter bfw=new BufferedWriter(fw); - bfw.write(textArea.getText(),0,textArea.getText().length()); - bfw.flush();//鍒锋柊璇ユ祦鐨勭紦鍐 - bfw.close(); - isNewFile=false; - currentFile=saveFileName; - oldValue=textArea.getText(); - this.setTitle(saveFileName.getName()+" - 璁颁簨鏈"); - statusLabel.setText("褰撳墠鎵撳紑鏂囦欢锛"+saveFileName.getAbsoluteFile()); - } - catch (IOException ioException) - { - } - } - } - else - { try - { FileWriter fw=new FileWriter(currentFile); - BufferedWriter bfw=new BufferedWriter(fw); - bfw.write(textArea.getText(),0,textArea.getText().length()); - bfw.flush(); - fw.close(); - } - catch(IOException ioException) - { - } - } - } - - /*鍙﹀瓨涓*/ - else if(e.getSource()==menuItem_saveas) - { textArea.requestFocus(); - String str=null; - JFileChooser fileChooser=new JFileChooser(); - fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - - fileChooser.setDialogTitle("鍙﹀瓨涓"); - int result=fileChooser.showSaveDialog(this); - if(result==JFileChooser.CANCEL_OPTION) - { statusLabel.setText("銆鎮ㄦ病鏈夐夋嫨浠讳綍鏂囦欢"); - return; - } - - File saveFileName=fileChooser.getSelectedFile(); - if(saveFileName==null||saveFileName.getName().equals("")) - { JOptionPane.showMessageDialog(this,"涓嶅悎娉曠殑鏂囦欢鍚:(","涓嶅悎娉曠殑鏂囦欢鍚:(",JOptionPane.ERROR_MESSAGE); - } - else - { try - { FileWriter fw=new FileWriter(saveFileName); - BufferedWriter bfw=new BufferedWriter(fw); - bfw.write(textArea.getText(),0,textArea.getText().length()); - bfw.flush(); - fw.close(); - oldValue=textArea.getText(); - this.setTitle(saveFileName.getName()+" de璁颁簨鏈"); - statusLabel.setText("銆褰撳墠鎵撳紑鏂囦欢:"+saveFileName.getAbsoluteFile()); - - } - catch(IOException ioException) - { - } - } - } - - /*閫鍑*/ - else if(e.getSource()==menuItem_exit) - { int exitChoose=JOptionPane.showConfirmDialog(this,"浣犵湡鐨勭‘瀹氳閫鍑哄悧?淇濆瓨浜嗗悧","閫鍑烘彁绀",JOptionPane.OK_CANCEL_OPTION); - if(exitChoose==JOptionPane.OK_OPTION) - { System.exit(0); - } - else - { return; - } - } - /*鎾ら攢*/ - else if(e.getSource()==menuItem_undo || e.getSource()==popupmenu_undo) - { textArea.requestFocus(); - if(undo.canUndo()) - { try - { undo.undo(); - } - catch (CannotUndoException ex) - { System.out.println("Unable to undo:" + ex); - - } - } - if(!undo.canUndo()) - { menuItem_undo.setEnabled(false); - } - } - - /*鍓垏*/ - else if(e.getSource()==menuItem_cut || e.getSource()==popupmenu_cut) - { textArea.requestFocus(); - String text=textArea.getSelectedText(); - StringSelection selection=new StringSelection(text); - clipBoard.setContents(selection,null); - textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd()); - checkMenuItemEnabled();//璁剧疆鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ゅ姛鑳界殑鍙敤鎬 - } - - /*澶嶅埗*/ - else if(e.getSource()==menuItem_copy || e.getSource()==popupmenu_copy) - { textArea.requestFocus(); - String text=textArea.getSelectedText(); - StringSelection selection=new StringSelection(text); - clipBoard.setContents(selection,null); - checkMenuItemEnabled();//璁剧疆鍓垏锛屽鍒讹紝绮樺笘锛屽垹闄ゅ姛鑳界殑鍙敤鎬 - } - - /*绮樺笘*/ - else if(e.getSource()==menuItem_paste || e.getSource()==popupmenu_paste) - { textArea.requestFocus(); - Transferable contents=clipBoard.getContents(this); - if(contents==null)return; - String text=""; - try - { text=(String)contents.getTransferData(DataFlavor.stringFlavor); - } - catch (Exception exception) - { - } - textArea.replaceRange(text,textArea.getSelectionStart(),textArea.getSelectionEnd()); - checkMenuItemEnabled(); - } - - /*鍒犻櫎*/ - else if(e.getSource()==menuItem_delete || e.getSource()==popupmenu_delete) - { textArea.requestFocus(); - textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd()); - checkMenuItemEnabled(); //璁剧疆鍓垏銆佸鍒躲佺矘璐淬佸垹闄ょ瓑鍔熻兘鐨勫彲鐢ㄦ - } - - /*鏌ユ壘*/ - else if(e.getSource()==menuItem_find) - { textArea.requestFocus(); - find(); - } - - /*鏇挎崲*/ - else if(e.getSource()==menuItem_replace) - { textArea.requestFocus(); - replace(); - }//鏇挎崲缁撴潫 - - /*鏃堕棿鏃ユ湡*/ - else if(e.getSource()==menuItem_time) - { textArea.requestFocus(); -// SimpleDateFormat currentDateTime=new SimpleDateFormat(" yyyy/MM/dd HH:mm:ss "); -// textArea.insert(currentDateTime.format(new Date()),textArea.getCaretPosition()); - Calendar rightNow=Calendar.getInstance(); - java.util.Date date=rightNow.getTime(); - textArea.insert(date.toString(),textArea.getCaretPosition()); - } - - /*鍏ㄩ*/ - else if(e.getSource()==menuItem_selectall || e.getSource()==popupmenu_selectall) - { textArea.selectAll(); - } - - /*鑷姩鎹㈣*/ - else if(e.getSource()==menuItem_linewrap) - { if(menuItem_linewrap.getState()) - textArea.setLineWrap(true); - else - textArea.setLineWrap(false); - - } - - /*瀛椾綋璁剧疆*/ - else if(e.getSource()==menuItem_font) - { textArea.requestFocus(); - font(); - } - - /*鐘舵佹爮璁剧疆*/ - else if(e.getSource()==menuItem_status) - { if(menuItem_status.getState()) - statusLabel.setVisible(true); - else - statusLabel.setVisible(false); - } - - - - /*甯姪*/ - else if(e.getSource()==menuItem_HelpTopics) - { textArea.requestFocus(); - JOptionPane.showMessageDialog(this,"浼樼涓嶆槸涓绉嶈涓猴紝鑰屾槸涓绉嶄範鎯紒","鏌ョ湅甯姪",JOptionPane.INFORMATION_MESSAGE); - } - - /*鍏充簬*/ - else if(e.getSource()==menuItem_AboutNoteBook) - { textArea.requestFocus(); - JOptionPane.showMessageDialog(this, - "璁颁簨鏈腑鐨勫畾鏃跺櫒鍔熻兘浣跨敤璇存槑\n" - + "1.鈥滅‘璁も濃斺斺斺斿紑濮嬭鏃跺櫒\n锛堢‘璁ら棿闅旀椂闂村悗锛屽埌闂撮殧鏃堕棿灏变細寮瑰嚭绐楀彛鎻愰啋锛岃嫢鎸夆滄槸鈥濆垯缁х画浼戞伅璇ラ棿闅旀椂闂达級\n2.鈥滃彇娑堚濃斺斺斺旂粨鏉熻鏃跺櫒\n" - +"璁℃椂鍣ㄥ紑鍚悗锛屽垯涓嶈兘瀵硅浜嬫湰杩涜缂栬緫\n鎵浠ワ紝璇峰ソ濂戒紤鎭竴涓嬬溂鐫涘惂锛", - "鍏充簬",JOptionPane.INFORMATION_MESSAGE); - } - - /*瀹氭椂鍣*/ - else if(e.getSource()==settime) - { - Clock(); - } - } - - /*瀹炵幇DocumentListener鎺ュ彛涓殑鏂规硶*/ - public void removeUpdate(DocumentEvent e) - { menuItem_undo.setEnabled(true); - } - public void insertUpdate(DocumentEvent e) - { menuItem_undo.setEnabled(true); - } - public void changedUpdate(DocumentEvent e) - { menuItem_undo.setEnabled(true); - } - - - /*瀹炵幇鎺ュ彛UndoableEditListener鐨勭被UndoHandler*/ - class UndoHandler implements UndoableEditListener - { public void undoableEditHappened(UndoableEditEvent uee) - { undo.addEdit(uee.getEdit()); - } - } - - - - - } - - - - - - - -- Gitee From 70d8194afcbcd6b2a94e6fb3dc412e16dd8cff2f Mon Sep 17 00:00:00 2001 From: lenovo Date: Fri, 11 Jun 2021 12:05:28 +0800 Subject: [PATCH 15/19] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .settings/org.eclipse.core.resources.prefs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index f12d244..de9b862 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -1,2 +1,2 @@ eclipse.preferences.version=1 -encoding//src/TimeNoteBook/Notebook.java=UTF-8 +encoding//src/java2020spring/Notebook.java=UTF-8 -- Gitee From c5a840748d38a614539fdef08332925c56bbd8d0 Mon Sep 17 00:00:00 2001 From: lenovo Date: Fri, 11 Jun 2021 12:19:23 +0800 Subject: [PATCH 16/19] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Notebook.java | 5 +---- src/java2020spring/Test.java | 10 ---------- src/java2020spring/window.java | 12 ++++++++++++ 3 files changed, 13 insertions(+), 14 deletions(-) delete mode 100644 src/java2020spring/Test.java create mode 100644 src/java2020spring/window.java diff --git a/src/java2020spring/Notebook.java b/src/java2020spring/Notebook.java index fba5144..e603287 100644 --- a/src/java2020spring/Notebook.java +++ b/src/java2020spring/Notebook.java @@ -30,10 +30,7 @@ import java.awt.event.*; import java.beans.EventHandler; public class Notebook extends JFrame implements DocumentListener,ActionListener { - public static void main(String args[]) - { Notebook notebook=new Notebook(); - notebook.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//浣跨敤 System exit 鏂规硶閫鍑哄簲鐢ㄧ▼搴 - } + private static Icon statusBar_status; public static final Color doushalv = new Color(199,237,204);//鏂板缓棰滆壊涓烘姢鐪艰眴娌欑豢 diff --git a/src/java2020spring/Test.java b/src/java2020spring/Test.java deleted file mode 100644 index fc09c19..0000000 --- a/src/java2020spring/Test.java +++ /dev/null @@ -1,10 +0,0 @@ -package java2020spring; - -public class Test { - - public static void main(String[] args) { - System.out.println("Hello world!"); - - } - -} diff --git a/src/java2020spring/window.java b/src/java2020spring/window.java new file mode 100644 index 0000000..9166519 --- /dev/null +++ b/src/java2020spring/window.java @@ -0,0 +1,12 @@ +package java2020spring; + +import javax.swing.JFrame; + +public class window { + + public static void main(String[] args) { + Notebook notebook=new Notebook(); + notebook.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//使用 System exit 方法退出应用程序 + } + +} -- Gitee From f78d2095c6e45d06cf3d2478852999e9e98019ae Mon Sep 17 00:00:00 2001 From: "nuoxian0626@163.com" Date: Fri, 11 Jun 2021 12:33:28 +0800 Subject: [PATCH 17/19] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Notebook.java | 2 -- src/java2020spring/window.java | 1 - 2 files changed, 3 deletions(-) diff --git a/src/java2020spring/Notebook.java b/src/java2020spring/Notebook.java index e603287..c060045 100644 --- a/src/java2020spring/Notebook.java +++ b/src/java2020spring/Notebook.java @@ -30,10 +30,8 @@ import java.awt.event.*; import java.beans.EventHandler; public class Notebook extends JFrame implements DocumentListener,ActionListener { - private static Icon statusBar_status; public static final Color doushalv = new Color(199,237,204);//鏂板缓棰滆壊涓烘姢鐪艰眴娌欑豢 - /*鑿滃崟*/ private JMenu menu_file; //鏂囦欢鎸夐挳 private JMenu menu_edit; //缂栬緫鎸夐挳 diff --git a/src/java2020spring/window.java b/src/java2020spring/window.java index 9166519..6d450f2 100644 --- a/src/java2020spring/window.java +++ b/src/java2020spring/window.java @@ -3,7 +3,6 @@ package java2020spring; import javax.swing.JFrame; public class window { - public static void main(String[] args) { Notebook notebook=new Notebook(); notebook.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//使用 System exit 方法退出应用程序 -- Gitee From 46fad0bacd0e419938dc002995d93a4ff699afc5 Mon Sep 17 00:00:00 2001 From: "nuoxian0626@163.com" Date: Fri, 11 Jun 2021 12:42:11 +0800 Subject: [PATCH 18/19] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Notebook.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java2020spring/Notebook.java b/src/java2020spring/Notebook.java index c060045..de5f75e 100644 --- a/src/java2020spring/Notebook.java +++ b/src/java2020spring/Notebook.java @@ -343,7 +343,7 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener statusLabel.addSeparator(new Dimension(180,5)); statusLabel.add(labelTime); statusLabel.setVisible(true); - + this.add(statusLabel,BorderLayout.SOUTH);//鍚戠獥鍙f坊鍔犵姸鎬佹爮鏍囩 this.setLocation(100,100);//璁剧疆绐楀彛鍦ㄥ睆骞曚笂鐨勪綅缃 this.setSize(650,550);//璁剧疆绐楀彛鍦ㄥ睆骞曚笂鐨勫ぇ灏 -- Gitee From c51cb12043715bc9cb0cfede49f2e15077a7f078 Mon Sep 17 00:00:00 2001 From: "nuoxian0626@163.com" Date: Fri, 11 Jun 2021 12:49:07 +0800 Subject: [PATCH 19/19] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Notebook.java | 2 -- src/java2020spring/window.java | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/java2020spring/Notebook.java b/src/java2020spring/Notebook.java index de5f75e..aa84d24 100644 --- a/src/java2020spring/Notebook.java +++ b/src/java2020spring/Notebook.java @@ -333,8 +333,6 @@ public class Notebook extends JFrame implements DocumentListener,ActionListener } }); - - /*鍒涘缓鍜屾坊鍔犵姸鎬佹爮*/ JToolBar statusLabel=new JToolBar(); statusLabel.setFloatable(false); diff --git a/src/java2020spring/window.java b/src/java2020spring/window.java index 6d450f2..3c8e96e 100644 --- a/src/java2020spring/window.java +++ b/src/java2020spring/window.java @@ -1,11 +1,9 @@ package java2020spring; import javax.swing.JFrame; - public class window { public static void main(String[] args) { Notebook notebook=new Notebook(); notebook.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//使用 System exit 方法退出应用程序 } - } -- Gitee