From f1ff7472f2525421c09b31e95974740bd6998dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E8=BF=B9s?= <澧ㄨ抗s@LAPTOP-QGOA2U92> Date: Fri, 4 Jun 2021 00:00:27 +0800 Subject: [PATCH 01/15] =?UTF-8?q?=E8=BF=99=E6=98=AF=E4=BA=8B=E9=A1=B9?= =?UTF-8?q?=E5=BE=85=E5=8A=9E=E5=BA=94=E7=94=A8=E7=9A=84=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=EF=BC=8C=E5=88=9B=E5=BB=BA=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E5=92=8C=E8=8F=9C=E5=8D=95=E9=A1=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/MainClass.java | 11 +++++++++ src/java2020spring/TodolistFrame.java | 32 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/java2020spring/MainClass.java create mode 100644 src/java2020spring/TodolistFrame.java diff --git a/src/java2020spring/MainClass.java b/src/java2020spring/MainClass.java new file mode 100644 index 0000000..05112c8 --- /dev/null +++ b/src/java2020spring/MainClass.java @@ -0,0 +1,11 @@ +package java2020spring; +public class MainClass { +public static void main(String args[]) { + //TodolistFrame frame=new TodolistFrame("todolist",30,50,300,250); + //new TodolistFrame(); + CalendarFrame frame =new CalendarFrame(); + frame.setBounds(100,100,360,300); + frame.setVisible(true); + frame.setYearAndMonth(2021,6); +} +} diff --git a/src/java2020spring/TodolistFrame.java b/src/java2020spring/TodolistFrame.java new file mode 100644 index 0000000..3d3c105 --- /dev/null +++ b/src/java2020spring/TodolistFrame.java @@ -0,0 +1,32 @@ +package java2020spring; +import javax.swing.*; + +import java.awt.event.InputEvent; +import static javax.swing.JFrame.*; +public class TodolistFrame extends JFrame{ + JMenuBar menubar; + JMenu menu1,menu2; + JMenuItem item1,item2; + public TodolistFrame() {} + public TodolistFrame(String s,int x,int y,int w,int h) { + init(s); + setLocation(x,y); + setSize(w,h); + setVisible(true); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + } + void init(String s) { + setTitle(s); + menubar=new JMenuBar(); + menu1=new JMenu("每日事项"); + menu2=new JMenu("日历视图"); + item1=new JMenuItem("未完成",new ImageIcon("a.gif")); + item2=new JMenuItem("已完成",new ImageIcon("b.gif")); + menu1.add(item1); + menu1.addSeparator(); + menu1.add(item2); + menubar.add(menu1); + menubar.add(menu2); + setJMenuBar(menubar); + } +} -- Gitee From 1de22b29450182ea7120a57e402c0d455fb75ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E8=BF=B9s?= <澧ㄨ抗s@LAPTOP-QGOA2U92> Date: Sun, 6 Jun 2021 00:00:15 +0800 Subject: [PATCH 02/15] =?UTF-8?q?=E4=BB=8A=E5=A4=A9=E5=81=9A=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=97=A5=E5=8E=86=E8=A7=86=E5=9B=BE=EF=BC=8C?= =?UTF-8?q?=E7=BB=99=E6=97=A5=E6=9C=9F=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E4=BA=8B=E4=BB=B6=E4=BD=BF=E5=AE=83=E5=BC=B9=E5=87=BA?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E6=A1=86=E7=95=8C=E9=9D=A2=EF=BC=8C=E4=BD=86?= =?UTF-8?q?=E5=9C=A8TodolistFrame=E9=87=8C=E8=BF=98=E6=B2=A1=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E5=BB=BA=E7=AB=8B=E4=B8=BB=E7=95=8C=E9=9D=A2=E5=AF=B9?= =?UTF-8?q?=E6=97=A5=E5=8E=86=E8=A7=86=E5=9B=BE=E7=9A=84=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E3=80=82=E7=8E=B0=E5=9C=A8=E8=BF=98=E5=90=84=E6=98=AF=E7=8B=AC?= =?UTF-8?q?=E7=AB=8B=E7=9A=84=E7=89=88=E5=9D=97=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/CalendarBean.java | 40 ++++++++++++ src/java2020spring/CalendarFrame.java | 92 +++++++++++++++++++++++++++ src/java2020spring/MyButton.java | 62 ++++++++++++++++++ src/java2020spring/TodolistFrame.java | 18 +++++- 4 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 src/java2020spring/CalendarBean.java create mode 100644 src/java2020spring/CalendarFrame.java create mode 100644 src/java2020spring/MyButton.java diff --git a/src/java2020spring/CalendarBean.java b/src/java2020spring/CalendarBean.java new file mode 100644 index 0000000..2dec80b --- /dev/null +++ b/src/java2020spring/CalendarBean.java @@ -0,0 +1,40 @@ +package java2020spring; +import java.util.Calendar; +public class CalendarBean { +String day[]; +int year =2005,month=0; +public void setYear(int year) { + this.year=year; +} +public int getYear() { + return year; +} +public void setMonth(int month) { + this.month=month; +} +public int getMonth(){ + return month; +} +public String[] getCalendar() { + String a[]=new String[42]; + Calendar 日历=Calendar.getInstance(); + 日历.set(year, month-1, 1); + int 星期几=日历.get(Calendar.DAY_OF_WEEK)-1; + int day=0; + if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) + day=31; + if(month==4||month==6||month==9||month==11) + day=30; + if(month==2) { + if(((year%4==0)&&(year%100!=0))||(year%400==0)) + day=29; + else + day=28; + } + for(int i=星期几,n=1;i<星期几+day;i++) { + a[i]=String.valueOf(n); + n++; + } + return a; +} +} diff --git a/src/java2020spring/CalendarFrame.java b/src/java2020spring/CalendarFrame.java new file mode 100644 index 0000000..c683658 --- /dev/null +++ b/src/java2020spring/CalendarFrame.java @@ -0,0 +1,92 @@ +package java2020spring; +import java.util.*; +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.border.*; + +public class CalendarFrame extends JFrame implements ActionListener{ + MyButton buttonDay[]=new MyButton[42]; + JButton titleName[]=new JButton[7]; + //OperatorListener operator; + //operator=new OperatorListener(); + JFrame f; + + String name[]= {"日","一","二","三","四","五","六"}; + JButton nextMonth,previousMonth; + CalendarBean calendar; + JLabel showMessage=new JLabel("",JLabel.CENTER); + int year=2011,month=2; + public CalendarFrame() { + JPanel pCenter=new JPanel(); + pCenter.setLayout(new GridLayout(7,7));//1 + + for (int i=0;i<7;i++) { + titleName[i]=new JButton(name[i]); + titleName[i].setBorder(new SoftBevelBorder(BevelBorder.RAISED)); + pCenter.add(titleName[i]); + } + for(int i=0;i<42;i++) { + buttonDay[i]=new MyButton(); + buttonDay[i].addActionListener(buttonDay[i]); + buttonDay[i].setBorder(new SoftBevelBorder(BevelBorder.LOWERED)); + pCenter.add(buttonDay[i]); + + } + + calendar=new CalendarBean(); + nextMonth=new JButton("下月"); + previousMonth=new JButton("上月"); + nextMonth.addActionListener(this); + JPanel pNorth=new JPanel(), + pSouth=new JPanel(); + pNorth.add(previousMonth); + pNorth.add(nextMonth); + pSouth.add(showMessage); + add(pCenter,BorderLayout.CENTER); + + add(pNorth,BorderLayout.NORTH); + add(pSouth,BorderLayout.SOUTH); + + setYearAndMonth(year,month); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + } + public void setYearAndMonth(int y,int m) { + calendar.setYear(y); + calendar.setMonth(m); + String day[]=calendar.getCalendar(); + for(int i=0;i<42;i++) + buttonDay[i].setText(day[i]); + showMessage.setText("日历:"+calendar.getYear()+"年"+calendar.getMonth()+"月");} + +public void actionPerformed(ActionEvent e) { + if(e.getSource()==nextMonth) { + + month=month+1; + if(month>12) + month=1; + calendar.setMonth(month); + String day[]=calendar.getCalendar(); + for(int i=0;i<42;i++) { + buttonDay[i].setText(day[i]); + + + } + + } + else if (e.getSource()==previousMonth) { + month=month-1; + if(month<1) + month=12; + calendar.setMonth(month); + String day[]=calendar.getCalendar(); + for(int i=0;i<42;i++) { + buttonDay[i].setText(day[i]); + + + } + } + showMessage.setText("日历:"+calendar.getYear()+"年"+calendar.getMonth()+"月"); + } +} + diff --git a/src/java2020spring/MyButton.java b/src/java2020spring/MyButton.java new file mode 100644 index 0000000..7454281 --- /dev/null +++ b/src/java2020spring/MyButton.java @@ -0,0 +1,62 @@ +package java2020spring; + +import java.awt.BorderLayout; +import java.awt.Container; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JSplitPane; +import javax.swing.JTextArea; + +public class MyButton extends JButton implements ActionListener{ + public void MyButton() { + +} + + @Override + public void actionPerformed(ActionEvent e) { + + GridLayout layout=new GridLayout(2,2); + JFrame f=new JFrame("things"); + f.setBounds(100,200,288,308); + f.setLayout(layout); + + JSplitPane []p=new JSplitPane[4]; + JTextArea []Area=new JTextArea[4]; + JLabel label[]=new JLabel[4]; + label[0]=new JLabel("重要不紧急"); + label[1]=new JLabel("重要且紧急"); + label[2]=new JLabel("不重要不紧急"); + label[3]=new JLabel("不重要但紧急"); + for (int j=0;j<4;j++) { + + Area[j]=new JTextArea(30,90); + p[j]=new JSplitPane(HORIZONTAL,label[j],new JScrollPane(Area[j])); + + f.add(p[j]); + } + + + +// p[0].add(new JLabel("重要不紧急"),BorderLayout.NORTH); +// p[0].add(new JScrollPane(Area[0]),BorderLayout.SOUTH); +// p[1].add(new JLabel("重要且紧急"),BorderLayout.NORTH); +// p[1].add(new JScrollPane(Area[1]),BorderLayout.CENTER); +// p[2].add(new JLabel("不重要不紧急"),BorderLayout.NORTH); +// p[2].add(new JScrollPane(Area[2]),BorderLayout.CENTER); +// p[3].add(new JLabel("不重要但紧急"),BorderLayout.NORTH); +// p[3].add(new JScrollPane(Area[3]),BorderLayout.CENTER); + + + f.setVisible(true); + f.setDefaultCloseOperation(f.DISPOSE_ON_CLOSE); + + + } + } \ No newline at end of file diff --git a/src/java2020spring/TodolistFrame.java b/src/java2020spring/TodolistFrame.java index 3d3c105..8651d06 100644 --- a/src/java2020spring/TodolistFrame.java +++ b/src/java2020spring/TodolistFrame.java @@ -1,11 +1,13 @@ package java2020spring; import javax.swing.*; - +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.awt.event.InputEvent; import static javax.swing.JFrame.*; public class TodolistFrame extends JFrame{ JMenuBar menubar; - JMenu menu1,menu2; + JMenu menu1; + JMenu menu2; JMenuItem item1,item2; public TodolistFrame() {} public TodolistFrame(String s,int x,int y,int w,int h) { @@ -20,6 +22,18 @@ public class TodolistFrame extends JFrame{ menubar=new JMenuBar(); menu1=new JMenu("每日事项"); menu2=new JMenu("日历视图"); + menu2.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(e.getSource()==menu2) { + CalendarFrame frame =new CalendarFrame(); + frame.setBounds(100,100,360,300); + frame.setYearAndMonth(2021,6); + setVisible(true); + setDefaultCloseOperation(DISPOSE_ON_CLOSE);} + + } + }); item1=new JMenuItem("未完成",new ImageIcon("a.gif")); item2=new JMenuItem("已完成",new ImageIcon("b.gif")); menu1.add(item1); -- Gitee From 674ba5808a5e67cc9479c69233749eef524728a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E8=BF=B9s?= <澧ㄨ抗s@LAPTOP-QGOA2U92> Date: Mon, 7 Jun 2021 00:07:27 +0800 Subject: [PATCH 03/15] =?UTF-8?q?=E4=B8=BA=E8=AE=B0=E4=BA=8B=E6=9C=AC?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E4=BA=86=E5=9F=BA=E6=9C=AC=E7=9A=84=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=E4=B8=8E=E6=A1=86=E6=9E=B6=EF=BC=8C=E5=8C=85=E6=8B=AC?= =?UTF-8?q?=E5=BF=AB=E6=8D=B7=E9=94=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/NotepadMainFrame.java | 1235 ++++++++++++++++++++++ 1 file changed, 1235 insertions(+) create mode 100644 src/java2020spring/NotepadMainFrame.java diff --git a/src/java2020spring/NotepadMainFrame.java b/src/java2020spring/NotepadMainFrame.java new file mode 100644 index 0000000..7538f5d --- /dev/null +++ b/src/java2020spring/NotepadMainFrame.java @@ -0,0 +1,1235 @@ +package java2020spring; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Component; +import java.awt.Container; +import java.awt.Dimension; +import java.awt.Event; +import java.awt.EventQueue; +import java.awt.FlowLayout; +import java.awt.Font; +import java.awt.Graphics; +import java.awt.GridLayout; +import java.awt.PrintJob; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.InputEvent; +import java.awt.event.KeyAdapter; +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.print.PageFormat; +import java.awt.print.PrinterJob; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Calendar; +import java.util.GregorianCalendar; + +import javax.swing.BorderFactory; +import javax.swing.ButtonGroup; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JCheckBoxMenuItem; +import javax.swing.JColorChooser; +import javax.swing.JDialog; +import javax.swing.JFileChooser; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JPopupMenu; +import javax.swing.JRadioButton; +import javax.swing.JScrollPane; +import javax.swing.JSeparator; +import javax.swing.JTextArea; +import javax.swing.JTextField; +import javax.swing.JToolBar; +import javax.swing.KeyStroke; +import javax.swing.UIManager; +import javax.swing.UnsupportedLookAndFeelException; +import javax.swing.border.EmptyBorder; +import javax.swing.event.CaretEvent; +import javax.swing.event.CaretListener; +import javax.swing.undo.UndoManager; + + +public class NotepadMainFrame extends JFrame implements ActionListener{ + /** + * 序列号 + */ + private static final long serialVersionUID = 8585210209467333480L; + private JPanel contentPane; + private JTextArea textArea; + private JMenuItem itemOpen; + private JMenuItem itemSave; + + //1:新建 + //2:修改过 + //3:保存过的 + int flag=0; + + //当前文件名 + String currentFileName=null; + + PrintJob p=null;//声明一个要打印的对象 + Graphics g=null;//要打印的对象 + + //当前文件路径 + String currentPath=null; + + //背景颜色 + JColorChooser jcc1=null; + Color color=Color.BLACK; + + //文本的行数和列数 + int linenum = 1; + int columnnum = 1; + + //撤销管理器 + public UndoManager undoMgr = new UndoManager(); + + //剪贴板 + public Clipboard clipboard = new Clipboard("系统剪切板"); + + private JMenuItem itemSaveAs; + private JMenuItem itemNew; + private JMenuItem itemPage; + private JSeparator separator; + private JMenuItem itemPrint; + private JMenuItem itemExit; + private JSeparator separator_1; + private JMenu itemEdit; + private JMenu itFormat; + private JMenu itemCheck; + private JMenu itemHelp; + private JMenuItem itemSearchForHelp; + private JMenuItem itemAboutNotepad; + private JMenuItem itemUndo; + private JMenuItem itemCut; + private JMenuItem itemCopy; + private JMenuItem itemPaste; + private JMenuItem itemDelete; + private JMenuItem itemFind; + private JMenuItem itemFindNext; + private JMenuItem itemReplace; + private JMenuItem itemTurnTo; + private JMenuItem itemSelectAll; + private JMenuItem itemTime; + private JMenuItem itemFont; + private JMenuItem itemColor; + private JMenuItem itemFontColor; + private JCheckBoxMenuItem itemNextLine; + private JScrollPane scrollPane; + private JCheckBoxMenuItem itemStatement; + private JToolBar toolState; + public static JLabel label1; + private JLabel label2; + private JLabel label3; + int length=0; + int sum=0; + + /** + * Launch the application. + */ + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + NotepadMainFrame frame = new NotepadMainFrame(); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + GregorianCalendar c=new GregorianCalendar(); + int hour=c.get(Calendar.HOUR_OF_DAY); + int min=c.get(Calendar.MINUTE); + int second=c.get(Calendar.SECOND); + private JPopupMenu popupMenu; + private JMenuItem popM_Undo; + private JMenuItem popM_Cut; + private JMenuItem popM_Copy; + private JMenuItem popM_Paste; + private JMenuItem popM_Delete; + private JMenuItem popM_SelectAll; + private JMenuItem popM_toLeft; + private JMenuItem popM_showUnicode; + private JMenuItem popM_closeIMe; + private JMenuItem popM_InsertUnicode; + private JMenuItem popM_RestartSelect; + private JSeparator separator_2; + private JSeparator separator_3; + private JSeparator separator_4; + private JSeparator separator_5; + private JMenuItem itemRedo; + private JSeparator separator_6; + private JSeparator separator_7; + private JSeparator separator_8; + private JMenuItem popM_Redo; + + /** + * Create the frame. + */ + public NotepadMainFrame() { + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (ClassNotFoundException e1) { + e1.printStackTrace(); + } catch (InstantiationException e1) { + e1.printStackTrace(); + } catch (IllegalAccessException e1) { + e1.printStackTrace(); + } catch (UnsupportedLookAndFeelException e1) { + e1.printStackTrace(); + } + setTitle("无标题"); + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + setBounds(100, 100, 521, 572); + + JMenuBar menuBar = new JMenuBar(); + setJMenuBar(menuBar); + + JMenu itemFile = new JMenu("文件(F)"); + itemFile.setMnemonic('F'); + menuBar.add(itemFile); + + itemNew = new JMenuItem("新建(N)",'N'); + itemNew.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, + java.awt.Event.CTRL_MASK)); + itemNew.addActionListener(this); + itemFile.add(itemNew); + + itemOpen = new JMenuItem("打开(O)",'O'); + itemOpen.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, + java.awt.Event.CTRL_MASK)); + itemOpen.addActionListener(this); + itemFile.add(itemOpen); + + itemSave = new JMenuItem("保存(S)"); + itemSave.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, + java.awt.Event.CTRL_MASK)); + itemSave.addActionListener(this); + itemFile.add(itemSave); + + itemSaveAs = new JMenuItem("另存为(A)"); + itemSaveAs.addActionListener(this); + itemFile.add(itemSaveAs); + + separator = new JSeparator(); + itemFile.add(separator); + + itemPage = new JMenuItem("页面设置(U)",'U'); + itemPage.addActionListener(this); + itemFile.add(itemPage); + + itemPrint = new JMenuItem("打印(P)...",'P'); + itemPrint.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, + java.awt.Event.CTRL_MASK)); + itemPrint.addActionListener(this); + itemFile.add(itemPrint); + + separator_1 = new JSeparator(); + itemFile.add(separator_1); + + itemExit = new JMenuItem("退出(X)",'X'); + itemExit.addActionListener(this); + itemFile.add(itemExit); + + itemEdit = new JMenu("编辑(E)"); + itemEdit.setMnemonic('E'); + menuBar.add(itemEdit); + + itemUndo = new JMenuItem("撤销(U)",'U'); + itemUndo.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, + java.awt.Event.CTRL_MASK)); + itemUndo.addActionListener(this); + itemEdit.add(itemUndo); + + itemRedo = new JMenuItem("恢复(R)"); + itemRedo.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, + java.awt.Event.CTRL_MASK)); + itemRedo.addActionListener(this); + itemEdit.add(itemRedo); + + itemCut = new JMenuItem("剪切(T)",'T'); + itemCut.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, + java.awt.Event.CTRL_MASK)); + itemCut.addActionListener(this); + + separator_6 = new JSeparator(); + itemEdit.add(separator_6); + itemEdit.add(itemCut); + + itemCopy = new JMenuItem("复制(C)",'C'); + itemCopy.addActionListener(this); + itemCopy.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, + java.awt.Event.CTRL_MASK)); + itemEdit.add(itemCopy); + + itemPaste = new JMenuItem("粘贴(P)",'P'); + itemPaste.addActionListener(this); + itemPaste.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, + java.awt.Event.CTRL_MASK)); + itemEdit.add(itemPaste); + + itemDelete = new JMenuItem("删除(L)",'L'); + itemDelete.addActionListener(this); + itemDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, + InputEvent.CTRL_MASK)); + itemEdit.add(itemDelete); + + separator_7 = new JSeparator(); + itemEdit.add(separator_7); + + itemFind = new JMenuItem("查找(F)",'F'); + itemFind.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, + Event.CTRL_MASK)); + itemFind.addActionListener(this); + itemEdit.add(itemFind); + + itemFindNext = new JMenuItem("查找下一个(N)",'N'); + itemFindNext.setAccelerator(KeyStroke.getKeyStroke("F3")); + itemFindNext.addActionListener(this); + itemEdit.add(itemFindNext); + + itemReplace = new JMenuItem("替换(R)",'R'); + itemReplace.addActionListener(this); + itemReplace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, + Event.CTRL_MASK)); + itemEdit.add(itemReplace); + + itemTurnTo = new JMenuItem("转到(G)",'G'); + itemTurnTo.addActionListener(this); + itemTurnTo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, + Event.CTRL_MASK)); + itemEdit.add(itemTurnTo); + + itemSelectAll = new JMenuItem("全选(A)",'A'); + itemSelectAll.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, + java.awt.Event.CTRL_MASK)); + itemSelectAll.addActionListener(this); + + separator_8 = new JSeparator(); + itemEdit.add(separator_8); + itemEdit.add(itemSelectAll); + + itemTime = new JMenuItem("时间/日期(D)",'D'); + itemTime.addActionListener(this); + itemTime.setAccelerator(KeyStroke.getKeyStroke("F5")); + itemEdit.add(itemTime); + + itFormat = new JMenu("格式(O)"); + itFormat.setMnemonic('O'); + menuBar.add(itFormat); + + itemNextLine = new JCheckBoxMenuItem("自动换行(W)"); + itemNextLine.addActionListener(this); + itFormat.add(itemNextLine); + + itemFont = new JMenuItem("字体大小(F)..."); + itemFont.addActionListener(this); + itFormat.add(itemFont); + + itemColor = new JMenuItem("背景颜色(C)..."); + itemColor.addActionListener(this); + itFormat.add(itemColor); + + itemFontColor = new JMenuItem("字体颜色(I)..."); + itemFontColor.addActionListener(this); + itFormat.add(itemFontColor); + + itemCheck = new JMenu("查看(V)"); + itemCheck.setMnemonic('V'); + menuBar.add(itemCheck); + + itemStatement = new JCheckBoxMenuItem("状态栏(S)"); + itemStatement.addActionListener(this); + itemCheck.add(itemStatement); + + itemHelp = new JMenu("帮助(H)"); + itemHelp.setMnemonic('H'); + menuBar.add(itemHelp); + + itemSearchForHelp = new JMenuItem("查看帮助(H)",'H'); + itemSearchForHelp.addActionListener(this); + itemHelp.add(itemSearchForHelp); + + itemAboutNotepad = new JMenuItem("关于记事本(A)",'A'); + itemAboutNotepad.addActionListener(this); + itemHelp.add(itemAboutNotepad); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + //设置边框布局 + contentPane.setLayout(new BorderLayout(0, 0)); + setContentPane(contentPane); + + textArea = new JTextArea(); + + //VERTICAL垂直 HORIZONTAL水平 + scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + + //contentPane2=new JPanel(); + //contentPane2.setSize(10,textArea.getSize().height); + //contentPane.add(contentPane2, BorderLayout.WEST); + + + + popupMenu = new JPopupMenu(); + addPopup(textArea, popupMenu); + + popM_Undo = new JMenuItem("撤销(U)"); + popM_Undo.addActionListener(this); + popupMenu.add(popM_Undo); + + popM_Redo = new JMenuItem("恢复(R)"); + popM_Redo.addActionListener(this); + popupMenu.add(popM_Redo); + + separator_2 = new JSeparator(); + popupMenu.add(separator_2); + + popM_Cut = new JMenuItem("剪切(T)"); + popM_Cut.addActionListener(this); + popupMenu.add(popM_Cut); + + popM_Copy = new JMenuItem("复制(C)"); + popM_Copy.addActionListener(this); + popupMenu.add(popM_Copy); + + popM_Paste = new JMenuItem("粘贴(P)"); + popM_Paste.addActionListener(this); + popupMenu.add(popM_Paste); + + popM_Delete = new JMenuItem("删除(D)"); + popM_Delete.addActionListener(this); + popupMenu.add(popM_Delete); + + separator_3 = new JSeparator(); + popupMenu.add(separator_3); + + popM_SelectAll = new JMenuItem("全选(A)"); + popM_SelectAll.addActionListener(this); + popupMenu.add(popM_SelectAll); + + separator_4 = new JSeparator(); + popupMenu.add(separator_4); + + popM_toLeft = new JMenuItem("从右到左的阅读顺序(R)"); + popM_toLeft.addActionListener(this); + popupMenu.add(popM_toLeft); + + popM_showUnicode = new JMenuItem("显示Unicode控制字符(S)"); + popM_showUnicode.addActionListener(this); + popupMenu.add(popM_showUnicode); + + popM_InsertUnicode = new JMenuItem("插入Unicode控制字符(I)"); + popM_InsertUnicode.addActionListener(this); + popupMenu.add(popM_InsertUnicode); + + separator_5 = new JSeparator(); + popupMenu.add(separator_5); + + popM_closeIMe = new JMenuItem("关闭IME(L)"); + popM_closeIMe.addActionListener(this); + popupMenu.add(popM_closeIMe); + + popM_RestartSelect = new JMenuItem("汉字重选(R)"); + popM_RestartSelect.addActionListener(this); + popupMenu.add(popM_RestartSelect); + //添加到面板中【中间】 + contentPane.add(scrollPane, BorderLayout.CENTER); + + //添加撤销管理器 + textArea.getDocument().addUndoableEditListener(undoMgr); + + + toolState = new JToolBar(); + toolState.setSize(textArea.getSize().width, 10);//toolState.setLayout(new FlowLayout(FlowLayout.LEFT)); + label1 = new JLabel(" 当前系统时间:" + hour + ":" + min + ":" + second+" "); + toolState.add(label1); + toolState.addSeparator(); + label2 = new JLabel(" 第 " + linenum + " 行, 第 " + columnnum+" 列 "); + toolState.add(label2); + toolState.addSeparator(); + + label3 = new JLabel(" 一共 " +length+" 字 "); + toolState.add(label3); + textArea.addCaretListener(new CaretListener() { //记录行数和列数 + public void caretUpdate(CaretEvent e) { + //sum=0; + JTextArea editArea = (JTextArea)e.getSource(); + + try { + int caretpos = editArea.getCaretPosition(); + linenum = editArea.getLineOfOffset(caretpos); + columnnum = caretpos - textArea.getLineStartOffset(linenum); + linenum += 1; + label2.setText(" 第 " + linenum + " 行, 第 " + (columnnum+1)+" 列 "); + //sum+=columnnum+1; + //length+=sum; + length=NotepadMainFrame.this.textArea.getText().toString().length(); + label3.setText(" 一共 " +length+" 字 "); + } + catch(Exception ex) { } + }}); + + contentPane.add(toolState, BorderLayout.SOUTH); + toolState.setVisible(false); + toolState.setFloatable(false); + + + + + // 创建弹出菜单 + final JPopupMenu jp=new JPopupMenu(); //创建弹出式菜单,下面三项是菜单项 + textArea.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + if(e.getButton()==MouseEvent.BUTTON3)//只响应鼠标右键单击事件 + { + jp.show(e.getComponent(),e.getX(),e.getY());//在鼠标位置显示弹出式菜单 + } + } + }); + + isChanged(); + + this.MainFrameWidowListener(); + } + + + /*===============================1====================================*/ + /** + * 是否有变化 + */ + private void isChanged() { + textArea.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + //在这里我进行了对使用快捷键,但是没有输入字符却没有改变textArea中内容的判断 + Character c=e.getKeyChar(); + if(c != null && !textArea.getText().toString().equals("")){ + flag=2; + } + } + }); + } + /*===================================================================*/ + + + /*===============================2====================================*/ + /** + * 新建的或保存过的退出只有两个选择 + */ + private void MainFrameWidowListener() { + this.addWindowListener(new WindowAdapter(){ + @Override + public void windowClosing(WindowEvent e) { + if(flag==2 && currentPath==null){ + //这是弹出小窗口 + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.saveAs(); + }else if(result==JOptionPane.NO_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + }else if(flag==2 && currentPath!=null){ + //这是弹出小窗口 + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.save(); + }else if(result==JOptionPane.NO_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + }else{ + //这是弹出小窗口 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "确定关闭?", "系统提示", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + } + } + }); + } + /*===================================================================*/ + + + /*==============================3=====================================*/ + /** + * 行为动作 + */ + public void actionPerformed(ActionEvent e) { + if(e.getSource()==itemOpen){ //打开 + openFile(); + }else if(e.getSource()==itemSave){ //保存 + //如果该文件是打开的,就可以直接保存 + save(); + }else if(e.getSource()==itemSaveAs){ //另存为 + saveAs(); + }else if(e.getSource()==itemNew){ //新建 + newFile(); + }else if(e.getSource()==itemExit){ //退出 + exit(); + }else if(e.getSource()==itemPage){ //页面设置 + ///页面设置,百度到的,不知道具体的用法 + PageFormat pf = new PageFormat(); + PrinterJob.getPrinterJob().pageDialog(pf); + }else if(e.getSource()==itemPrint){ //打印 + //打印机 + Print(); + }else if(e.getSource()==itemUndo || e.getSource()==popM_Undo){ //撤销 + if(undoMgr.canUndo()){ + undoMgr.undo(); + } + }else if(e.getSource()==itemRedo || e.getSource()==popM_Redo){ //恢复 + if(undoMgr.canRedo()){ + undoMgr.redo(); + } + }else if(e.getSource()==itemCut || e.getSource()==popM_Cut){ //剪切 + cut(); + }else if(e.getSource()==itemCopy || e.getSource()==popM_Copy){ //复制 + copy(); + }else if(e.getSource()==itemPaste || e.getSource()==popM_Paste){ //粘贴 + paste(); + }else if(e.getSource()==itemDelete || e.getSource()==popM_Delete){ //删除 + String tem=textArea.getText().toString(); + textArea.setText(tem.substring(0,textArea.getSelectionStart())); + }else if(e.getSource()==itemFind){ //查找 + mySearch(); + }else if(e.getSource()==itemFindNext){ //查找下一个 + mySearch(); + }else if(e.getSource()==itemReplace){ //替换 + mySearch(); + }else if(e.getSource()==itemTurnTo){ //转到 + turnTo(); + }else if(e.getSource()==itemSelectAll || e.getSource()==popM_SelectAll){ //选择全部 + textArea.selectAll(); + }else if(e.getSource()==itemTime){ //时间/日期 + textArea.append(hour+":"+min+" "+c.get(Calendar.YEAR)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DAY_OF_MONTH)); + }else if(e.getSource()==itemNextLine){ //设置自动换行 + //设置文本区的换行策略。如果设置为 true,则当行的长度大于所分配的宽度时,将换行。此属性默认为 false。 + if(itemNextLine.isSelected()){ + textArea.setLineWrap(true); + }else{ + textArea.setLineWrap(false); + } + }else if(e.getSource()==itemFont){ //设置字体大小 + // 构造字体选择器,参数字体为预设值 + // MQFontChooser fontChooser = new MQFontChooser(textArea.getFont()); + //fontChooser.showFontDialog(this); + // Font font = fontChooser.getSelectFont(); + // 将字体设置到JTextArea中 + //textArea.setFont(font); + }else if(e.getSource()==itemColor){ //设置背景颜色 + jcc1 = new JColorChooser(); + JOptionPane.showMessageDialog(this, jcc1,"选择背景颜色颜色",-1); + color = jcc1.getColor(); + textArea.setBackground(color); + }else if(e.getSource()==itemFontColor){ //设置字体颜色 + jcc1=new JColorChooser(); + JOptionPane.showMessageDialog(this, jcc1, "选择字体颜色", -1); + color = jcc1.getColor(); + //String string=textArea.getSelectedText(); + textArea.setForeground(color); + }else if(e.getSource()==itemStatement){ //设置状态 + if(itemStatement.isSelected()){ + //scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); + toolState.setVisible(true); + }else{ + //scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + toolState.setVisible(false); + } + }else if(e.getSource()==itemSearchForHelp){ + JOptionPane.showMessageDialog(this, "HELLO","行动起来",1); + }else if(e.getSource()==itemAboutNotepad){ + JOptionPane.showMessageDialog(this, "哈哈","说明文档",1); + } + } + /*===================================================================*/ + + + private void turnTo() { + final JDialog gotoDialog = new JDialog(this, "转到下列行"); + JLabel gotoLabel = new JLabel("行数(L):"); + final JTextField linenum = new JTextField(5); + linenum.setText("1"); + linenum.selectAll(); + + JButton okButton = new JButton("确定"); + okButton.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + int totalLine = textArea.getLineCount(); + int[] lineNumber = new int[totalLine + 1]; + String s = textArea.getText(); + int pos = 0, t = 0; + + while (true) { + pos = s.indexOf('\12', pos); + // System.out.println("引索pos:"+pos); + if (pos == -1) + break; + lineNumber[t++] = pos++; + } + + int gt = 1; + try { + gt = Integer.parseInt(linenum.getText()); + } catch (NumberFormatException efe) { + JOptionPane.showMessageDialog(null, "请输入行数!", "提示", JOptionPane.WARNING_MESSAGE); + linenum.requestFocus(true); + return; + } + + if (gt < 2 || gt >= totalLine) { + if (gt < 2) + textArea.setCaretPosition(0); + else + textArea.setCaretPosition(s.length()); + } else + textArea.setCaretPosition(lineNumber[gt - 2] + 1); + + gotoDialog.dispose(); + } + + }); + + JButton cancelButton = new JButton("取消"); + cancelButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + gotoDialog.dispose(); + } + }); + + Container con = gotoDialog.getContentPane(); + con.setLayout(new FlowLayout()); + con.add(gotoLabel); + con.add(linenum); + con.add(okButton); + con.add(cancelButton); + + gotoDialog.setSize(200, 100); + gotoDialog.setResizable(false); + gotoDialog.setLocation(300, 280); + gotoDialog.setVisible(true); + } + + + /*===============================8====================================*/ + /** + * 推出按钮,和窗口的红叉实现一样的功能 + */ + private void exit() { + if(flag==2 && currentPath==null){ + //这是弹出小窗口 + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.saveAs(); + }else if(result==JOptionPane.NO_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + }else if(flag==2 && currentPath!=null){ + //这是弹出小窗口 + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.save(); + }else if(result==JOptionPane.NO_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + }else{ + //这是弹出小窗口 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "确定关闭?", "系统提示", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + } + } + /*===================================================================*/ + + + /*===============================4====================================*/ + /** + * 新建文件,只有改过的和保存过的需要处理 + */ + private void newFile() { + if(flag==0 || flag==1){ //刚启动记事本为0,刚新建文档为1 + return; + }else if(flag==2 && this.currentPath==null){ //修改后 + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + this.saveAs(); //另存为 + }else if(result==JOptionPane.NO_OPTION){ + this.textArea.setText(""); + this.setTitle("无标题"); + flag=1; + } + return; + }else if(flag==2 && this.currentPath!=null ){ + //2、(保存的文件为3)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+this.currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + this.save(); //直接保存,有路径 + }else if(result==JOptionPane.NO_OPTION){ + this.textArea.setText(""); + this.setTitle("无标题"); + flag=1; + } + }else if(flag==3){ //保存的文件 + this.textArea.setText(""); + flag=1; + this.setTitle("无标题"); + } + } + /*===================================================================*/ + + + /*===============================5====================================*/ + /** + * 另存为 + */ + private void saveAs() { + //打开保存框 + JFileChooser choose=new JFileChooser(); + //选择文件 + int result=choose.showSaveDialog(this); + if(result==JFileChooser.APPROVE_OPTION){ + //取得选择的文件[文件名是自己输入的] + File file=choose.getSelectedFile(); + FileWriter fw=null; + //保存 + try { + fw=new FileWriter(file); + fw.write(textArea.getText()); + currentFileName=file.getName(); + currentPath=file.getAbsolutePath(); + //如果比较少,需要写 + fw.flush(); + this.flag=3; + this.setTitle(currentPath); + } catch (IOException e1) { + e1.printStackTrace(); + }finally{ + try { + if(fw!=null) fw.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + } + } + /*===================================================================*/ + + + /*===============================6====================================*/ + /** + * 保存 + */ + private void save() { + if(this.currentPath==null){ + this.saveAs(); + if(this.currentPath==null){ + return; + } + } + FileWriter fw=null; + //保存 + try { + fw=new FileWriter(new File(currentPath)); + fw.write(textArea.getText()); + //如果比较少,需要写 + fw.flush(); + flag=3; + this.setTitle(this.currentPath); + } catch (IOException e1) { + e1.printStackTrace(); + }finally{ + try { + if(fw!=null) fw.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + } + /*===================================================================*/ + + + /*================================7===================================*/ + /** + * 打开文件 + */ + private void openFile() { + if(flag==2 && this.currentPath==null){ + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + this.saveAs(); + } + }else if(flag==2 && this.currentPath!=null){ + //2、(打开的文件2,保存的文件3)条件下修改 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+this.currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + this.save(); + } + } + //打开文件选择框 + JFileChooser choose=new JFileChooser(); + //选择文件 + int result=choose.showOpenDialog(this); + if(result==JFileChooser.APPROVE_OPTION){ + //取得选择的文件 + File file=choose.getSelectedFile(); + //打开已存在的文件,提前将文件名存起来 + currentFileName=file.getName(); + //存在文件全路径 + currentPath=file.getAbsolutePath(); + flag=3; + this.setTitle(this.currentPath); + BufferedReader br=null; + try { + //建立文件流[字符流] + InputStreamReader isr=new InputStreamReader(new FileInputStream(file),"GBK"); + br=new BufferedReader(isr);//动态绑定 + //读取内容 + StringBuffer sb=new StringBuffer(); + String line=null; + while((line=br.readLine())!=null){ + sb.append(line+System.lineSeparator()); + } + //显示在文本框[多框] + textArea.setText(sb.toString()); + } catch (FileNotFoundException e1) { + e1.printStackTrace(); + } catch (IOException e1) { + e1.printStackTrace(); + } finally{ + try { + if(br!=null) br.close(); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + } + } + /*================================================================*/ + + + /*=============================9===================================*/ + public void Print() + { + try{ + p = getToolkit().getPrintJob(this,"ok",null);//创建一个Printfjob 对象 p + g = p.getGraphics();//p 获取一个用于打印的 Graphics 的对象 + //g.translate(120,200);//改变组建的位置 + this.textArea.printAll(g); + p.end();//释放对象 g + } + catch(Exception a){ + + } + } + /*================================================================*/ + + + private static void addPopup(Component component, final JPopupMenu popup) { + component.addMouseListener(new MouseAdapter() { + public void mousePressed(MouseEvent e) { + if (e.isPopupTrigger()) { + showMenu(e); + } + } + public void mouseReleased(MouseEvent e) { + if (e.isPopupTrigger()) { + showMenu(e); + } + } + private void showMenu(MouseEvent e) { + popup.show(e.getComponent(), e.getX(), e.getY()); + } + }); + } + + public void cut(){ + copy(); + //标记开始位置 + int start = this.textArea.getSelectionStart(); + //标记结束位置 + int end = this.textArea.getSelectionEnd(); + //删除所选段 + this.textArea.replaceRange("", start, end); + + } + + public void copy(){ + //拖动选取文本 + String temp = this.textArea.getSelectedText(); + //把获取的内容复制到连续字符器,这个类继承了剪贴板接口 + StringSelection text = new StringSelection(temp); + //把内容放在剪贴板 + this.clipboard.setContents(text, null); + } + + public void paste(){ + //Transferable接口,把剪贴板的内容转换成数据 + Transferable contents = this.clipboard.getContents(this); + //DataFalvor类判断是否能把剪贴板的内容转换成所需数据类型 + DataFlavor flavor = DataFlavor.stringFlavor; + //如果可以转换 + if(contents.isDataFlavorSupported(flavor)){ + String str; + try {//开始转换 + str=(String)contents.getTransferData(flavor); + //如果要粘贴时,鼠标已经选中了一些字符 + if(this.textArea.getSelectedText()!=null){ + //定位被选中字符的开始位置 + int start = this.textArea.getSelectionStart(); + //定位被选中字符的末尾位置 + int end = this.textArea.getSelectionEnd(); + //把粘贴的内容替换成被选中的内容 + this.textArea.replaceRange(str, start, end); + }else{ + //获取鼠标所在TextArea的位置 + int mouse = this.textArea.getCaretPosition(); + //在鼠标所在的位置粘贴内容 + this.textArea.insert(str, mouse); + } + } catch(UnsupportedFlavorException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch(IllegalArgumentException e){ + e.printStackTrace(); + } + } + } + + public void mySearch() { + final JDialog findDialog = new JDialog(this, "查找与替换", true); + Container con = findDialog.getContentPane(); + con.setLayout(new FlowLayout(FlowLayout.LEFT)); + JLabel searchContentLabel = new JLabel("查找内容(N) :"); + JLabel replaceContentLabel = new JLabel("替换为(P)  :"); + final JTextField findText = new JTextField(22); + final JTextField replaceText = new JTextField(22); + final JCheckBox matchcase = new JCheckBox("区分大小写"); + ButtonGroup bGroup = new ButtonGroup(); + final JRadioButton up = new JRadioButton("向上(U)"); + final JRadioButton down = new JRadioButton("向下(D)"); + down.setSelected(true); + bGroup.add(up); + bGroup.add(down); + JButton searchNext = new JButton("查找下一个(F)"); + JButton replace = new JButton("替换(R)"); + final JButton replaceAll = new JButton("全部替换(A)"); + searchNext.setPreferredSize(new Dimension(110, 22)); + replace.setPreferredSize(new Dimension(110, 22)); + replaceAll.setPreferredSize(new Dimension(110, 22)); + // "替换"按钮的事件处理 + replace.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()); + } + }); + + // "替换全部"按钮的事件处理 + replaceAll.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + textArea.setCaretPosition(0); // 将光标放到编辑区开头 + int a = 0, b = 0, replaceCount = 0; + + if (findText.getText().length() == 0) { + JOptionPane.showMessageDialog(findDialog, "请填写查找内容!", "提示", JOptionPane.WARNING_MESSAGE); + findText.requestFocus(true); + return; + } + while (a > -1) { + + int FindStartPos = textArea.getCaretPosition(); + String str1, str2, str3, str4, strA, strB; + str1 = textArea.getText(); + str2 = str1.toLowerCase(); + str3 = findText.getText(); + str4 = str3.toLowerCase(); + + if (matchcase.isSelected()) { + strA = str1; + strB = str3; + } else { + strA = str2; + strB = str4; + } + + if (up.isSelected()) { + if (textArea.getSelectedText() == null) { + a = strA.lastIndexOf(strB, FindStartPos - 1); + } else { + a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1); + } + } else if (down.isSelected()) { + if (textArea.getSelectedText() == null) { + a = strA.indexOf(strB, FindStartPos); + } else { + a = strA.indexOf(strB, FindStartPos - findText.getText().length() + 1); + } + + } + + if (a > -1) { + if (up.isSelected()) { + textArea.setCaretPosition(a); + b = findText.getText().length(); + textArea.select(a, a + b); + } else if (down.isSelected()) { + textArea.setCaretPosition(a); + b = findText.getText().length(); + textArea.select(a, a + b); + } + } else { + if (replaceCount == 0) { + JOptionPane.showMessageDialog(findDialog, "找不到您查找的内容!", "记事本", JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(findDialog, "成功替换" + 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++; + } + }// end while + } + }); /* "替换全部"按钮的事件处理结束 */ + + // "查找下一个"按钮事件处理 + searchNext.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + int a = 0, b = 0; + int FindStartPos = textArea.getCaretPosition(); + String str1, str2, str3, str4, strA, strB; + str1 = textArea.getText(); + str2 = str1.toLowerCase(); + str3 = findText.getText(); + str4 = str3.toLowerCase(); + // "区分大小写"的CheckBox被选中 + if (matchcase.isSelected()) { + strA = str1; + strB = str3; + } else { + strA = str2; + strB = str4; + } + + if (up.isSelected()) { + if (textArea.getSelectedText() == null) { + a = strA.lastIndexOf(strB, FindStartPos - 1); + } else { + a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1); + } + } else if (down.isSelected()) { + if (textArea.getSelectedText() == null) { + a = strA.indexOf(strB, FindStartPos); + } else { + a = strA.indexOf(strB, FindStartPos - findText.getText().length() + 1); + } + + } + if (a > -1) { + if (up.isSelected()) { + textArea.setCaretPosition(a); + b = findText.getText().length(); + textArea.select(a, a + b); + } else if (down.isSelected()) { + textArea.setCaretPosition(a); + b = findText.getText().length(); + textArea.select(a, a + b); + } + } else { + JOptionPane.showMessageDialog(null, "找不到您查找的内容!", "记事本", JOptionPane.INFORMATION_MESSAGE); + } + + } + });/* "查找下一个"按钮事件处理结束 */ + // "取消"按钮及事件处理 + JButton cancel = new JButton("取消"); + cancel.setPreferredSize(new Dimension(110, 22)); + cancel.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + findDialog.dispose(); + } + }); + + // 创建"查找与替换"对话框的界面 + JPanel bottomPanel = new JPanel(); + JPanel centerPanel = new JPanel(); + JPanel topPanel = new JPanel(); + + JPanel direction = new JPanel(); + direction.setBorder(BorderFactory.createTitledBorder("方向 ")); + direction.add(up); + direction.add(down); + direction.setPreferredSize(new Dimension(170, 60)); + JPanel replacePanel = new JPanel(); + replacePanel.setLayout(new GridLayout(2, 1)); + replacePanel.add(replace); + replacePanel.add(replaceAll); + + topPanel.add(searchContentLabel); + topPanel.add(findText); + topPanel.add(searchNext); + centerPanel.add(replaceContentLabel); + centerPanel.add(replaceText); + centerPanel.add(replacePanel); + bottomPanel.add(matchcase); + bottomPanel.add(direction); + bottomPanel.add(cancel); + + con.add(topPanel); + con.add(centerPanel); + con.add(bottomPanel); + + // 设置"查找与替换"对话框的大小、可更改大小(否)、位置和可见性 + findDialog.setSize(410, 210); + findDialog.setResizable(false); + findDialog.setLocation(230, 280); + findDialog.setVisible(true); + } + +} + -- Gitee From 08d86f477355c6c1bbe2514625adf2b09059e69a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E8=BF=B9s?= <澧ㄨ抗s@LAPTOP-QGOA2U92> Date: Mon, 7 Jun 2021 12:37:45 +0800 Subject: [PATCH 04/15] =?UTF-8?q?=E8=BF=9B=E4=B8=80=E6=AD=A5=E7=BB=99?= =?UTF-8?q?=E8=AE=B0=E4=BA=8B=E6=9C=AC=E5=AE=8C=E5=96=84=E4=BA=86=E5=89=AA?= =?UTF-8?q?=E5=88=87=E3=80=81=E5=A4=8D=E5=88=B6=E3=80=81=20=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E3=80=81=E6=92=A4=E9=94=80=E3=80=81=E5=85=A8=E9=80=89?= =?UTF-8?q?=E3=80=81=E6=98=BE=E7=A4=BA=E6=97=B6=E9=97=B4=E7=AD=89=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/NotepadMainFrame.java | 1110 +++------------------- 1 file changed, 155 insertions(+), 955 deletions(-) diff --git a/src/java2020spring/NotepadMainFrame.java b/src/java2020spring/NotepadMainFrame.java index 7538f5d..a5abd41 100644 --- a/src/java2020spring/NotepadMainFrame.java +++ b/src/java2020spring/NotepadMainFrame.java @@ -1,71 +1,18 @@ package java2020spring; import java.awt.BorderLayout; import java.awt.Color; -import java.awt.Component; -import java.awt.Container; -import java.awt.Dimension; -import java.awt.Event; import java.awt.EventQueue; -import java.awt.FlowLayout; -import java.awt.Font; -import java.awt.Graphics; -import java.awt.GridLayout; -import java.awt.PrintJob; import java.awt.datatransfer.Clipboard; -import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.StringSelection; -import java.awt.datatransfer.Transferable; -import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.awt.event.InputEvent; -import java.awt.event.KeyAdapter; -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.print.PageFormat; -import java.awt.print.PrinterJob; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStreamReader; import java.util.Calendar; import java.util.GregorianCalendar; - -import javax.swing.BorderFactory; -import javax.swing.ButtonGroup; -import javax.swing.JButton; -import javax.swing.JCheckBox; -import javax.swing.JCheckBoxMenuItem; -import javax.swing.JColorChooser; -import javax.swing.JDialog; -import javax.swing.JFileChooser; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JMenu; -import javax.swing.JMenuBar; -import javax.swing.JMenuItem; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JPopupMenu; -import javax.swing.JRadioButton; -import javax.swing.JScrollPane; -import javax.swing.JSeparator; -import javax.swing.JTextArea; -import javax.swing.JTextField; -import javax.swing.JToolBar; -import javax.swing.KeyStroke; -import javax.swing.UIManager; -import javax.swing.UnsupportedLookAndFeelException; -import javax.swing.border.EmptyBorder; +import javax.swing.border.*; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; import javax.swing.undo.UndoManager; +import javax.swing.*; +import javax.swing.JPopupMenu.*; public class NotepadMainFrame extends JFrame implements ActionListener{ @@ -86,11 +33,6 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ //当前文件名 String currentFileName=null; - PrintJob p=null;//声明一个要打印的对象 - Graphics g=null;//要打印的对象 - - //当前文件路径 - String currentPath=null; //背景颜色 JColorChooser jcc1=null; @@ -100,49 +42,49 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ int linenum = 1; int columnnum = 1; - //撤销管理器 - public UndoManager undoMgr = new UndoManager(); + //剪贴板 public Clipboard clipboard = new Clipboard("系统剪切板"); - private JMenuItem itemSaveAs; - private JMenuItem itemNew; - private JMenuItem itemPage; - private JSeparator separator; - private JMenuItem itemPrint; - private JMenuItem itemExit; - private JSeparator separator_1; - private JMenu itemEdit; - private JMenu itFormat; - private JMenu itemCheck; - private JMenu itemHelp; - private JMenuItem itemSearchForHelp; - private JMenuItem itemAboutNotepad; - private JMenuItem itemUndo; - private JMenuItem itemCut; - private JMenuItem itemCopy; - private JMenuItem itemPaste; - private JMenuItem itemDelete; - private JMenuItem itemFind; - private JMenuItem itemFindNext; - private JMenuItem itemReplace; - private JMenuItem itemTurnTo; - private JMenuItem itemSelectAll; - private JMenuItem itemTime; - private JMenuItem itemFont; - private JMenuItem itemColor; - private JMenuItem itemFontColor; - private JCheckBoxMenuItem itemNextLine; - private JScrollPane scrollPane; - private JCheckBoxMenuItem itemStatement; - private JToolBar toolState; - public static JLabel label1; - private JLabel label2; - private JLabel label3; - int length=0; - int sum=0; - + + private JMenuItem itemSaveAs; + private JMenuItem itemNew; + private JMenuItem itemPage; + private JSeparator separator; + private JMenuItem itemPrint; + private JMenuItem itemExit; + private JSeparator separator_1; + private JMenu itemEdit; + private JMenu itFormat; + private JMenu itemCheck; + private JMenu itemHelp; + private JMenuItem itemSearchForHelp; + private JMenuItem itemAboutNotepad; + private JMenuItem itemUndo; + private JMenuItem itemCut; + private JMenuItem itemCopy; + private JMenuItem itemPaste; + private JMenuItem itemDelete; + private JMenuItem itemFind; + private JMenuItem itemFindNext; + private JMenuItem itemReplace; + private JMenuItem itemTurnTo; + private JMenuItem itemSelectAll; + private JMenuItem itemTime; + private JMenuItem itemFont; + private JMenuItem itemColor; + private JMenuItem itemFontColor; + private JCheckBoxMenuItem itemNextLine; + private JScrollPane scrollPane; + private JCheckBoxMenuItem itemStatement; + private JToolBar toolState; + public static JLabel label1; + private JLabel label2; + private JLabel label3; + int length=0; + int sum=0; + public UndoManager undoMgr = new UndoManager(); /** * Launch the application. */ @@ -184,7 +126,6 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ private JSeparator separator_7; private JSeparator separator_8; private JMenuItem popM_Redo; - /** * Create the frame. */ @@ -200,7 +141,7 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ } catch (UnsupportedLookAndFeelException e1) { e1.printStackTrace(); } - setTitle("无标题"); + setTitle("notepad"); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); setBounds(100, 100, 521, 572); @@ -212,20 +153,17 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ menuBar.add(itemFile); itemNew = new JMenuItem("新建(N)",'N'); - itemNew.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, - java.awt.Event.CTRL_MASK)); + itemNew.setAccelerator(KeyStroke.getKeyStroke("control N ")); itemNew.addActionListener(this); itemFile.add(itemNew); itemOpen = new JMenuItem("打开(O)",'O'); - itemOpen.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, - java.awt.Event.CTRL_MASK)); + itemOpen.setAccelerator(KeyStroke.getKeyStroke("control O ")); itemOpen.addActionListener(this); itemFile.add(itemOpen); itemSave = new JMenuItem("保存(S)"); - itemSave.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, - java.awt.Event.CTRL_MASK)); + itemSave.setAccelerator(KeyStroke.getKeyStroke("control S ")); itemSave.addActionListener(this); itemFile.add(itemSave); @@ -241,8 +179,7 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ itemFile.add(itemPage); itemPrint = new JMenuItem("打印(P)...",'P'); - itemPrint.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, - java.awt.Event.CTRL_MASK)); + itemPrint.setAccelerator(KeyStroke.getKeyStroke("control P ")); itemPrint.addActionListener(this); itemFile.add(itemPrint); @@ -258,20 +195,17 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ menuBar.add(itemEdit); itemUndo = new JMenuItem("撤销(U)",'U'); - itemUndo.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, - java.awt.Event.CTRL_MASK)); + itemUndo.setAccelerator(KeyStroke.getKeyStroke("control Z")); itemUndo.addActionListener(this); itemEdit.add(itemUndo); itemRedo = new JMenuItem("恢复(R)"); - itemRedo.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, - java.awt.Event.CTRL_MASK)); + itemRedo.setAccelerator(KeyStroke.getKeyStroke("control R ")); itemRedo.addActionListener(this); itemEdit.add(itemRedo); itemCut = new JMenuItem("剪切(T)",'T'); - itemCut.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, - java.awt.Event.CTRL_MASK)); + itemCut.setAccelerator(KeyStroke.getKeyStroke("control X ")); itemCut.addActionListener(this); separator_6 = new JSeparator(); @@ -280,28 +214,24 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ itemCopy = new JMenuItem("复制(C)",'C'); itemCopy.addActionListener(this); - itemCopy.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, - java.awt.Event.CTRL_MASK)); + itemCopy.setAccelerator(KeyStroke.getKeyStroke("control C ")); itemEdit.add(itemCopy); itemPaste = new JMenuItem("粘贴(P)",'P'); itemPaste.addActionListener(this); - itemPaste.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, - java.awt.Event.CTRL_MASK)); + itemPaste.setAccelerator(KeyStroke.getKeyStroke("control V")); itemEdit.add(itemPaste); itemDelete = new JMenuItem("删除(L)",'L'); itemDelete.addActionListener(this); - itemDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, - InputEvent.CTRL_MASK)); + itemDelete.setAccelerator(KeyStroke.getKeyStroke("control D ")); itemEdit.add(itemDelete); separator_7 = new JSeparator(); itemEdit.add(separator_7); itemFind = new JMenuItem("查找(F)",'F'); - itemFind.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, - Event.CTRL_MASK)); + itemFind.setAccelerator(KeyStroke.getKeyStroke("control F ")); itemFind.addActionListener(this); itemEdit.add(itemFind); @@ -312,25 +242,21 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ itemReplace = new JMenuItem("替换(R)",'R'); itemReplace.addActionListener(this); - itemReplace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, - Event.CTRL_MASK)); + itemReplace.setAccelerator(KeyStroke.getKeyStroke("control H ")); itemEdit.add(itemReplace); itemTurnTo = new JMenuItem("转到(G)",'G'); itemTurnTo.addActionListener(this); - itemTurnTo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, - Event.CTRL_MASK)); + itemTurnTo.setAccelerator(KeyStroke.getKeyStroke("control G ")); itemEdit.add(itemTurnTo); itemSelectAll = new JMenuItem("全选(A)",'A'); - itemSelectAll.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, - java.awt.Event.CTRL_MASK)); + itemSelectAll.setAccelerator(KeyStroke.getKeyStroke("control A ")); itemSelectAll.addActionListener(this); separator_8 = new JSeparator(); itemEdit.add(separator_8); itemEdit.add(itemSelectAll); - itemTime = new JMenuItem("时间/日期(D)",'D'); itemTime.addActionListener(this); itemTime.setAccelerator(KeyStroke.getKeyStroke("F5")); @@ -376,860 +302,134 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ itemAboutNotepad.addActionListener(this); itemHelp.add(itemAboutNotepad); contentPane = new JPanel(); - contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + TitledBorder title=new TitledBorder(new LineBorder(Color.BLACK),""); + contentPane.setBorder(title); //设置边框布局 - contentPane.setLayout(new BorderLayout(0, 0)); + contentPane.setLayout(new BorderLayout(5,5)); setContentPane(contentPane); textArea = new JTextArea(); //VERTICAL垂直 HORIZONTAL水平 scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); - + //contentPane2=new JPanel(); //contentPane2.setSize(10,textArea.getSize().height); //contentPane.add(contentPane2, BorderLayout.WEST); - - - - popupMenu = new JPopupMenu(); - addPopup(textArea, popupMenu); - - popM_Undo = new JMenuItem("撤销(U)"); - popM_Undo.addActionListener(this); - popupMenu.add(popM_Undo); - - popM_Redo = new JMenuItem("恢复(R)"); - popM_Redo.addActionListener(this); - popupMenu.add(popM_Redo); - - separator_2 = new JSeparator(); - popupMenu.add(separator_2); - - popM_Cut = new JMenuItem("剪切(T)"); - popM_Cut.addActionListener(this); - popupMenu.add(popM_Cut); - - popM_Copy = new JMenuItem("复制(C)"); - popM_Copy.addActionListener(this); - popupMenu.add(popM_Copy); - - popM_Paste = new JMenuItem("粘贴(P)"); - popM_Paste.addActionListener(this); - popupMenu.add(popM_Paste); - - popM_Delete = new JMenuItem("删除(D)"); - popM_Delete.addActionListener(this); - popupMenu.add(popM_Delete); - - separator_3 = new JSeparator(); - popupMenu.add(separator_3); - - popM_SelectAll = new JMenuItem("全选(A)"); - popM_SelectAll.addActionListener(this); - popupMenu.add(popM_SelectAll); - - separator_4 = new JSeparator(); - popupMenu.add(separator_4); - - popM_toLeft = new JMenuItem("从右到左的阅读顺序(R)"); - popM_toLeft.addActionListener(this); - popupMenu.add(popM_toLeft); - - popM_showUnicode = new JMenuItem("显示Unicode控制字符(S)"); - popM_showUnicode.addActionListener(this); - popupMenu.add(popM_showUnicode); - - popM_InsertUnicode = new JMenuItem("插入Unicode控制字符(I)"); - popM_InsertUnicode.addActionListener(this); - popupMenu.add(popM_InsertUnicode); - - separator_5 = new JSeparator(); - popupMenu.add(separator_5); - - popM_closeIMe = new JMenuItem("关闭IME(L)"); - popM_closeIMe.addActionListener(this); - popupMenu.add(popM_closeIMe); - - popM_RestartSelect = new JMenuItem("汉字重选(R)"); - popM_RestartSelect.addActionListener(this); - popupMenu.add(popM_RestartSelect); - //添加到面板中【中间】 - contentPane.add(scrollPane, BorderLayout.CENTER); - - //添加撤销管理器 - textArea.getDocument().addUndoableEditListener(undoMgr); - - - toolState = new JToolBar(); - toolState.setSize(textArea.getSize().width, 10);//toolState.setLayout(new FlowLayout(FlowLayout.LEFT)); - label1 = new JLabel(" 当前系统时间:" + hour + ":" + min + ":" + second+" "); - toolState.add(label1); - toolState.addSeparator(); - label2 = new JLabel(" 第 " + linenum + " 行, 第 " + columnnum+" 列 "); - toolState.add(label2); - toolState.addSeparator(); - - label3 = new JLabel(" 一共 " +length+" 字 "); - toolState.add(label3); - textArea.addCaretListener(new CaretListener() { //记录行数和列数 - public void caretUpdate(CaretEvent e) { - //sum=0; - JTextArea editArea = (JTextArea)e.getSource(); - - try { - int caretpos = editArea.getCaretPosition(); - linenum = editArea.getLineOfOffset(caretpos); - columnnum = caretpos - textArea.getLineStartOffset(linenum); - linenum += 1; - label2.setText(" 第 " + linenum + " 行, 第 " + (columnnum+1)+" 列 "); - //sum+=columnnum+1; - //length+=sum; - length=NotepadMainFrame.this.textArea.getText().toString().length(); - label3.setText(" 一共 " +length+" 字 "); - } - catch(Exception ex) { } - }}); - - contentPane.add(toolState, BorderLayout.SOUTH); - toolState.setVisible(false); - toolState.setFloatable(false); - - - - - // 创建弹出菜单 - final JPopupMenu jp=new JPopupMenu(); //创建弹出式菜单,下面三项是菜单项 - textArea.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent e) { - if(e.getButton()==MouseEvent.BUTTON3)//只响应鼠标右键单击事件 - { - jp.show(e.getComponent(),e.getX(),e.getY());//在鼠标位置显示弹出式菜单 - } - } - }); - - isChanged(); - - this.MainFrameWidowListener(); - } - + popupMenu = new JPopupMenu(); + addPopup(textArea, popupMenu); - /*===============================1====================================*/ - /** - * 是否有变化 - */ - private void isChanged() { - textArea.addKeyListener(new KeyAdapter() { - @Override - public void keyTyped(KeyEvent e) { - //在这里我进行了对使用快捷键,但是没有输入字符却没有改变textArea中内容的判断 - Character c=e.getKeyChar(); - if(c != null && !textArea.getText().toString().equals("")){ - flag=2; - } - } - }); - } - /*===================================================================*/ + popM_Undo = new JMenuItem("撤销(U)"); + popM_Undo.addActionListener(this); + popupMenu.add(popM_Undo); + popM_Redo = new JMenuItem("恢复(R)"); + popM_Redo.addActionListener(this); + popupMenu.add(popM_Redo); - /*===============================2====================================*/ - /** - * 新建的或保存过的退出只有两个选择 - */ - private void MainFrameWidowListener() { - this.addWindowListener(new WindowAdapter(){ - @Override - public void windowClosing(WindowEvent e) { - if(flag==2 && currentPath==null){ - //这是弹出小窗口 - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - NotepadMainFrame.this.saveAs(); - }else if(result==JOptionPane.NO_OPTION){ - NotepadMainFrame.this.dispose(); - NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); - } - }else if(flag==2 && currentPath!=null){ - //这是弹出小窗口 - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - NotepadMainFrame.this.save(); - }else if(result==JOptionPane.NO_OPTION){ - NotepadMainFrame.this.dispose(); - NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); - } - }else{ - //这是弹出小窗口 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "确定关闭?", "系统提示", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - NotepadMainFrame.this.dispose(); - NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); - } - } - } - }); - } - /*===================================================================*/ + separator_2 = new JSeparator(); + popupMenu.add(separator_2); + popM_Cut = new JMenuItem("剪切(T)"); + popM_Cut.addActionListener(this); + popupMenu.add(popM_Cut); - /*==============================3=====================================*/ - /** - * 行为动作 - */ - public void actionPerformed(ActionEvent e) { - if(e.getSource()==itemOpen){ //打开 - openFile(); - }else if(e.getSource()==itemSave){ //保存 - //如果该文件是打开的,就可以直接保存 - save(); - }else if(e.getSource()==itemSaveAs){ //另存为 - saveAs(); - }else if(e.getSource()==itemNew){ //新建 - newFile(); - }else if(e.getSource()==itemExit){ //退出 - exit(); - }else if(e.getSource()==itemPage){ //页面设置 - ///页面设置,百度到的,不知道具体的用法 - PageFormat pf = new PageFormat(); - PrinterJob.getPrinterJob().pageDialog(pf); - }else if(e.getSource()==itemPrint){ //打印 - //打印机 - Print(); - }else if(e.getSource()==itemUndo || e.getSource()==popM_Undo){ //撤销 - if(undoMgr.canUndo()){ - undoMgr.undo(); - } - }else if(e.getSource()==itemRedo || e.getSource()==popM_Redo){ //恢复 - if(undoMgr.canRedo()){ - undoMgr.redo(); - } - }else if(e.getSource()==itemCut || e.getSource()==popM_Cut){ //剪切 - cut(); - }else if(e.getSource()==itemCopy || e.getSource()==popM_Copy){ //复制 - copy(); - }else if(e.getSource()==itemPaste || e.getSource()==popM_Paste){ //粘贴 - paste(); - }else if(e.getSource()==itemDelete || e.getSource()==popM_Delete){ //删除 - String tem=textArea.getText().toString(); - textArea.setText(tem.substring(0,textArea.getSelectionStart())); - }else if(e.getSource()==itemFind){ //查找 - mySearch(); - }else if(e.getSource()==itemFindNext){ //查找下一个 - mySearch(); - }else if(e.getSource()==itemReplace){ //替换 - mySearch(); - }else if(e.getSource()==itemTurnTo){ //转到 - turnTo(); - }else if(e.getSource()==itemSelectAll || e.getSource()==popM_SelectAll){ //选择全部 - textArea.selectAll(); - }else if(e.getSource()==itemTime){ //时间/日期 - textArea.append(hour+":"+min+" "+c.get(Calendar.YEAR)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DAY_OF_MONTH)); - }else if(e.getSource()==itemNextLine){ //设置自动换行 - //设置文本区的换行策略。如果设置为 true,则当行的长度大于所分配的宽度时,将换行。此属性默认为 false。 - if(itemNextLine.isSelected()){ - textArea.setLineWrap(true); - }else{ - textArea.setLineWrap(false); - } - }else if(e.getSource()==itemFont){ //设置字体大小 - // 构造字体选择器,参数字体为预设值 - // MQFontChooser fontChooser = new MQFontChooser(textArea.getFont()); - //fontChooser.showFontDialog(this); - // Font font = fontChooser.getSelectFont(); - // 将字体设置到JTextArea中 - //textArea.setFont(font); - }else if(e.getSource()==itemColor){ //设置背景颜色 - jcc1 = new JColorChooser(); - JOptionPane.showMessageDialog(this, jcc1,"选择背景颜色颜色",-1); - color = jcc1.getColor(); - textArea.setBackground(color); - }else if(e.getSource()==itemFontColor){ //设置字体颜色 - jcc1=new JColorChooser(); - JOptionPane.showMessageDialog(this, jcc1, "选择字体颜色", -1); - color = jcc1.getColor(); - //String string=textArea.getSelectedText(); - textArea.setForeground(color); - }else if(e.getSource()==itemStatement){ //设置状态 - if(itemStatement.isSelected()){ - //scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); - toolState.setVisible(true); - }else{ - //scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); - toolState.setVisible(false); - } - }else if(e.getSource()==itemSearchForHelp){ - JOptionPane.showMessageDialog(this, "HELLO","行动起来",1); - }else if(e.getSource()==itemAboutNotepad){ - JOptionPane.showMessageDialog(this, "哈哈","说明文档",1); - } - } - /*===================================================================*/ - - - private void turnTo() { - final JDialog gotoDialog = new JDialog(this, "转到下列行"); - JLabel gotoLabel = new JLabel("行数(L):"); - final JTextField linenum = new JTextField(5); - linenum.setText("1"); - linenum.selectAll(); - - JButton okButton = new JButton("确定"); - okButton.addActionListener(new ActionListener() { - - public void actionPerformed(ActionEvent e) { - int totalLine = textArea.getLineCount(); - int[] lineNumber = new int[totalLine + 1]; - String s = textArea.getText(); - int pos = 0, t = 0; - - while (true) { - pos = s.indexOf('\12', pos); - // System.out.println("引索pos:"+pos); - if (pos == -1) - break; - lineNumber[t++] = pos++; - } - - int gt = 1; - try { - gt = Integer.parseInt(linenum.getText()); - } catch (NumberFormatException efe) { - JOptionPane.showMessageDialog(null, "请输入行数!", "提示", JOptionPane.WARNING_MESSAGE); - linenum.requestFocus(true); - return; - } - - if (gt < 2 || gt >= totalLine) { - if (gt < 2) - textArea.setCaretPosition(0); - else - textArea.setCaretPosition(s.length()); - } else - textArea.setCaretPosition(lineNumber[gt - 2] + 1); - - gotoDialog.dispose(); - } - - }); - - JButton cancelButton = new JButton("取消"); - cancelButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - gotoDialog.dispose(); - } - }); - - Container con = gotoDialog.getContentPane(); - con.setLayout(new FlowLayout()); - con.add(gotoLabel); - con.add(linenum); - con.add(okButton); - con.add(cancelButton); - - gotoDialog.setSize(200, 100); - gotoDialog.setResizable(false); - gotoDialog.setLocation(300, 280); - gotoDialog.setVisible(true); - } + popM_Copy = new JMenuItem("复制(C)"); + popM_Copy.addActionListener(this); + popupMenu.add(popM_Copy); + popM_Paste = new JMenuItem("粘贴(P)"); + popM_Paste.addActionListener(this); + popupMenu.add(popM_Paste); - /*===============================8====================================*/ - /** - * 推出按钮,和窗口的红叉实现一样的功能 - */ - private void exit() { - if(flag==2 && currentPath==null){ - //这是弹出小窗口 - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - NotepadMainFrame.this.saveAs(); - }else if(result==JOptionPane.NO_OPTION){ - NotepadMainFrame.this.dispose(); - NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); - } - }else if(flag==2 && currentPath!=null){ - //这是弹出小窗口 - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - NotepadMainFrame.this.save(); - }else if(result==JOptionPane.NO_OPTION){ - NotepadMainFrame.this.dispose(); - NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); - } - }else{ - //这是弹出小窗口 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "确定关闭?", "系统提示", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - NotepadMainFrame.this.dispose(); - NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); - } - } - } - /*===================================================================*/ - - - /*===============================4====================================*/ - /** - * 新建文件,只有改过的和保存过的需要处理 - */ - private void newFile() { - if(flag==0 || flag==1){ //刚启动记事本为0,刚新建文档为1 - return; - }else if(flag==2 && this.currentPath==null){ //修改后 - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - this.saveAs(); //另存为 - }else if(result==JOptionPane.NO_OPTION){ - this.textArea.setText(""); - this.setTitle("无标题"); - flag=1; - } - return; - }else if(flag==2 && this.currentPath!=null ){ - //2、(保存的文件为3)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+this.currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - this.save(); //直接保存,有路径 - }else if(result==JOptionPane.NO_OPTION){ - this.textArea.setText(""); - this.setTitle("无标题"); - flag=1; - } - }else if(flag==3){ //保存的文件 - this.textArea.setText(""); - flag=1; - this.setTitle("无标题"); - } - } - /*===================================================================*/ + popM_Delete = new JMenuItem("删除(D)"); + popM_Delete.addActionListener(this); + popupMenu.add(popM_Delete); + separator_3 = new JSeparator(); + popupMenu.add(separator_3); - /*===============================5====================================*/ - /** - * 另存为 - */ - private void saveAs() { - //打开保存框 - JFileChooser choose=new JFileChooser(); - //选择文件 - int result=choose.showSaveDialog(this); - if(result==JFileChooser.APPROVE_OPTION){ - //取得选择的文件[文件名是自己输入的] - File file=choose.getSelectedFile(); - FileWriter fw=null; - //保存 - try { - fw=new FileWriter(file); - fw.write(textArea.getText()); - currentFileName=file.getName(); - currentPath=file.getAbsolutePath(); - //如果比较少,需要写 - fw.flush(); - this.flag=3; - this.setTitle(currentPath); - } catch (IOException e1) { - e1.printStackTrace(); - }finally{ - try { - if(fw!=null) fw.close(); - } catch (IOException e1) { - e1.printStackTrace(); - } - } - } - } - /*===================================================================*/ + popM_SelectAll = new JMenuItem("全选(A)"); + popM_SelectAll.addActionListener(this); + popupMenu.add(popM_SelectAll); - - /*===============================6====================================*/ - /** - * 保存 - */ - private void save() { - if(this.currentPath==null){ - this.saveAs(); - if(this.currentPath==null){ - return; - } - } - FileWriter fw=null; - //保存 - try { - fw=new FileWriter(new File(currentPath)); - fw.write(textArea.getText()); - //如果比较少,需要写 - fw.flush(); - flag=3; - this.setTitle(this.currentPath); - } catch (IOException e1) { - e1.printStackTrace(); - }finally{ - try { - if(fw!=null) fw.close(); - } catch (IOException e1) { - e1.printStackTrace(); - } - } - } - /*===================================================================*/ + separator_4 = new JSeparator(); + popupMenu.add(separator_4); + popM_toLeft = new JMenuItem("从右到左的阅读顺序(R)"); + popM_toLeft.addActionListener(this); + popupMenu.add(popM_toLeft); - /*================================7===================================*/ - /** - * 打开文件 - */ - private void openFile() { - if(flag==2 && this.currentPath==null){ - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - this.saveAs(); - } - }else if(flag==2 && this.currentPath!=null){ - //2、(打开的文件2,保存的文件3)条件下修改 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+this.currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - this.save(); - } - } - //打开文件选择框 - JFileChooser choose=new JFileChooser(); - //选择文件 - int result=choose.showOpenDialog(this); - if(result==JFileChooser.APPROVE_OPTION){ - //取得选择的文件 - File file=choose.getSelectedFile(); - //打开已存在的文件,提前将文件名存起来 - currentFileName=file.getName(); - //存在文件全路径 - currentPath=file.getAbsolutePath(); - flag=3; - this.setTitle(this.currentPath); - BufferedReader br=null; - try { - //建立文件流[字符流] - InputStreamReader isr=new InputStreamReader(new FileInputStream(file),"GBK"); - br=new BufferedReader(isr);//动态绑定 - //读取内容 - StringBuffer sb=new StringBuffer(); - String line=null; - while((line=br.readLine())!=null){ - sb.append(line+System.lineSeparator()); - } - //显示在文本框[多框] - textArea.setText(sb.toString()); - } catch (FileNotFoundException e1) { - e1.printStackTrace(); - } catch (IOException e1) { - e1.printStackTrace(); - } finally{ - try { - if(br!=null) br.close(); - } catch (Exception e1) { - e1.printStackTrace(); - } - } - } - } - /*================================================================*/ - + popM_showUnicode = new JMenuItem("显示Unicode控制字符(S)"); + popM_showUnicode.addActionListener(this); + popupMenu.add(popM_showUnicode); - /*=============================9===================================*/ - public void Print() - { - try{ - p = getToolkit().getPrintJob(this,"ok",null);//创建一个Printfjob 对象 p - g = p.getGraphics();//p 获取一个用于打印的 Graphics 的对象 - //g.translate(120,200);//改变组建的位置 - this.textArea.printAll(g); - p.end();//释放对象 g - } - catch(Exception a){ - - } - } - /*================================================================*/ + popM_InsertUnicode = new JMenuItem("插入Unicode控制字符(I)"); + popM_InsertUnicode.addActionListener(this); + popupMenu.add(popM_InsertUnicode); + separator_5 = new JSeparator(); + popupMenu.add(separator_5); - private static void addPopup(Component component, final JPopupMenu popup) { - component.addMouseListener(new MouseAdapter() { - public void mousePressed(MouseEvent e) { - if (e.isPopupTrigger()) { - showMenu(e); - } - } - public void mouseReleased(MouseEvent e) { - if (e.isPopupTrigger()) { - showMenu(e); - } - } - private void showMenu(MouseEvent e) { - popup.show(e.getComponent(), e.getX(), e.getY()); - } - }); - } + popM_closeIMe = new JMenuItem("关闭IME(L)"); + popM_closeIMe.addActionListener(this); + popupMenu.add(popM_closeIMe); - public void cut(){ - copy(); - //标记开始位置 - int start = this.textArea.getSelectionStart(); - //标记结束位置 - int end = this.textArea.getSelectionEnd(); - //删除所选段 - this.textArea.replaceRange("", start, end); - - } + popM_RestartSelect = new JMenuItem("汉字重选(R)"); + popM_RestartSelect.addActionListener(this); + popupMenu.add(popM_RestartSelect); + //添加到面板中【中间】 + contentPane.add(scrollPane, BorderLayout.CENTER); - public void copy(){ - //拖动选取文本 - String temp = this.textArea.getSelectedText(); - //把获取的内容复制到连续字符器,这个类继承了剪贴板接口 - StringSelection text = new StringSelection(temp); - //把内容放在剪贴板 - this.clipboard.setContents(text, null); - } + //添加撤销管理器 + textArea.getDocument().addUndoableEditListener(undoMgr); + - public void paste(){ - //Transferable接口,把剪贴板的内容转换成数据 - Transferable contents = this.clipboard.getContents(this); - //DataFalvor类判断是否能把剪贴板的内容转换成所需数据类型 - DataFlavor flavor = DataFlavor.stringFlavor; - //如果可以转换 - if(contents.isDataFlavorSupported(flavor)){ - String str; - try {//开始转换 - str=(String)contents.getTransferData(flavor); - //如果要粘贴时,鼠标已经选中了一些字符 - if(this.textArea.getSelectedText()!=null){ - //定位被选中字符的开始位置 - int start = this.textArea.getSelectionStart(); - //定位被选中字符的末尾位置 - int end = this.textArea.getSelectionEnd(); - //把粘贴的内容替换成被选中的内容 - this.textArea.replaceRange(str, start, end); - }else{ - //获取鼠标所在TextArea的位置 - int mouse = this.textArea.getCaretPosition(); - //在鼠标所在的位置粘贴内容 - this.textArea.insert(str, mouse); - } - } catch(UnsupportedFlavorException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch(IllegalArgumentException e){ - e.printStackTrace(); - } - } - } - - public void mySearch() { - final JDialog findDialog = new JDialog(this, "查找与替换", true); - Container con = findDialog.getContentPane(); - con.setLayout(new FlowLayout(FlowLayout.LEFT)); - JLabel searchContentLabel = new JLabel("查找内容(N) :"); - JLabel replaceContentLabel = new JLabel("替换为(P)  :"); - final JTextField findText = new JTextField(22); - final JTextField replaceText = new JTextField(22); - final JCheckBox matchcase = new JCheckBox("区分大小写"); - ButtonGroup bGroup = new ButtonGroup(); - final JRadioButton up = new JRadioButton("向上(U)"); - final JRadioButton down = new JRadioButton("向下(D)"); - down.setSelected(true); - bGroup.add(up); - bGroup.add(down); - JButton searchNext = new JButton("查找下一个(F)"); - JButton replace = new JButton("替换(R)"); - final JButton replaceAll = new JButton("全部替换(A)"); - searchNext.setPreferredSize(new Dimension(110, 22)); - replace.setPreferredSize(new Dimension(110, 22)); - replaceAll.setPreferredSize(new Dimension(110, 22)); - // "替换"按钮的事件处理 - replace.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()); - } - }); - - // "替换全部"按钮的事件处理 - replaceAll.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - - textArea.setCaretPosition(0); // 将光标放到编辑区开头 - int a = 0, b = 0, replaceCount = 0; - - if (findText.getText().length() == 0) { - JOptionPane.showMessageDialog(findDialog, "请填写查找内容!", "提示", JOptionPane.WARNING_MESSAGE); - findText.requestFocus(true); - return; - } - while (a > -1) { - - int FindStartPos = textArea.getCaretPosition(); - String str1, str2, str3, str4, strA, strB; - str1 = textArea.getText(); - str2 = str1.toLowerCase(); - str3 = findText.getText(); - str4 = str3.toLowerCase(); - - if (matchcase.isSelected()) { - strA = str1; - strB = str3; - } else { - strA = str2; - strB = str4; - } - - if (up.isSelected()) { - if (textArea.getSelectedText() == null) { - a = strA.lastIndexOf(strB, FindStartPos - 1); - } else { - a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1); - } - } else if (down.isSelected()) { - if (textArea.getSelectedText() == null) { - a = strA.indexOf(strB, FindStartPos); - } else { - a = strA.indexOf(strB, FindStartPos - findText.getText().length() + 1); - } - - } - - if (a > -1) { - if (up.isSelected()) { - textArea.setCaretPosition(a); - b = findText.getText().length(); - textArea.select(a, a + b); - } else if (down.isSelected()) { - textArea.setCaretPosition(a); - b = findText.getText().length(); - textArea.select(a, a + b); - } - } else { - if (replaceCount == 0) { - JOptionPane.showMessageDialog(findDialog, "找不到您查找的内容!", "记事本", JOptionPane.INFORMATION_MESSAGE); - } else { - JOptionPane.showMessageDialog(findDialog, "成功替换" + 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++; - } - }// end while - } - }); /* "替换全部"按钮的事件处理结束 */ - - // "查找下一个"按钮事件处理 - searchNext.addActionListener(new ActionListener() { - - public void actionPerformed(ActionEvent e) { - int a = 0, b = 0; - int FindStartPos = textArea.getCaretPosition(); - String str1, str2, str3, str4, strA, strB; - str1 = textArea.getText(); - str2 = str1.toLowerCase(); - str3 = findText.getText(); - str4 = str3.toLowerCase(); - // "区分大小写"的CheckBox被选中 - if (matchcase.isSelected()) { - strA = str1; - strB = str3; - } else { - strA = str2; - strB = str4; - } - - if (up.isSelected()) { - if (textArea.getSelectedText() == null) { - a = strA.lastIndexOf(strB, FindStartPos - 1); - } else { - a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1); - } - } else if (down.isSelected()) { - if (textArea.getSelectedText() == null) { - a = strA.indexOf(strB, FindStartPos); - } else { - a = strA.indexOf(strB, FindStartPos - findText.getText().length() + 1); - } - - } - if (a > -1) { - if (up.isSelected()) { - textArea.setCaretPosition(a); - b = findText.getText().length(); - textArea.select(a, a + b); - } else if (down.isSelected()) { - textArea.setCaretPosition(a); - b = findText.getText().length(); - textArea.select(a, a + b); - } - } else { - JOptionPane.showMessageDialog(null, "找不到您查找的内容!", "记事本", JOptionPane.INFORMATION_MESSAGE); - } + toolState = new JToolBar(); + toolState.setSize(textArea.getSize().width, 10);//toolState.setLayout(new FlowLayout(FlowLayout.LEFT)); + label1 = new JLabel(" 当前系统时间:" + hour + ":" + min + ":" + second+" "); + toolState.add(label1); + toolState.addSeparator(); + label2 = new JLabel(" 第 " + linenum + " 行, 第 " + columnnum+" 列 "); + toolState.add(label2); + toolState.addSeparator(); + + label3 = new JLabel(" 一共 " +length+" 字 "); + toolState.add(label3); + textArea.addCaretListener(new CaretListener() { //记录行数和列数 + public void caretUpdate(CaretEvent e) { + //sum=0; + JTextArea editArea = (JTextArea)e.getSource(); + try { + int caretpos = editArea.getCaretPosition(); + linenum = editArea.getLineOfOffset(caretpos); + columnnum = caretpos - textArea.getLineStartOffset(linenum); + linenum += 1; + label2.setText(" 第 " + linenum + " 行, 第 " + (columnnum+1)+" 列 "); + //sum+=columnnum+1; + //length+=sum; + length=NotepadMainFrame.this.textArea.getText().toString().length(); + label3.setText(" 一共 " +length+" 字 "); } - });/* "查找下一个"按钮事件处理结束 */ - // "取消"按钮及事件处理 - JButton cancel = new JButton("取消"); - cancel.setPreferredSize(new Dimension(110, 22)); - cancel.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - findDialog.dispose(); - } - }); - - // 创建"查找与替换"对话框的界面 - JPanel bottomPanel = new JPanel(); - JPanel centerPanel = new JPanel(); - JPanel topPanel = new JPanel(); - - JPanel direction = new JPanel(); - direction.setBorder(BorderFactory.createTitledBorder("方向 ")); - direction.add(up); - direction.add(down); - direction.setPreferredSize(new Dimension(170, 60)); - JPanel replacePanel = new JPanel(); - replacePanel.setLayout(new GridLayout(2, 1)); - replacePanel.add(replace); - replacePanel.add(replaceAll); - - topPanel.add(searchContentLabel); - topPanel.add(findText); - topPanel.add(searchNext); - centerPanel.add(replaceContentLabel); - centerPanel.add(replaceText); - centerPanel.add(replacePanel); - bottomPanel.add(matchcase); - bottomPanel.add(direction); - bottomPanel.add(cancel); - - con.add(topPanel); - con.add(centerPanel); - con.add(bottomPanel); + catch(Exception ex) { } + }}); + + contentPane.add(toolState, BorderLayout.SOUTH); + toolState.setVisible(false); + toolState.setFloatable(false);} + + + + private void addPopup(JTextArea textArea2, JPopupMenu popupMenu2) { + // TODO 自动生成的方法存根 + + } - // 设置"查找与替换"对话框的大小、可更改大小(否)、位置和可见性 - findDialog.setSize(410, 210); - findDialog.setResizable(false); - findDialog.setLocation(230, 280); - findDialog.setVisible(true); - } -} + @Override + public void actionPerformed(ActionEvent e) { + // TODO 自动生成的方法存根 + + }} -- Gitee From 17c56e90059be7ddf41ec3a864c7979e1e32ff4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E8=BF=B9s?= <澧ㄨ抗s@LAPTOP-QGOA2U92> Date: Mon, 7 Jun 2021 20:57:57 +0800 Subject: [PATCH 05/15] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=BC=B9=E5=87=BA?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=EF=BC=8C=E8=AE=BE=E7=BD=AE=E6=96=B0=E5=BB=BA?= =?UTF-8?q?=E7=9A=84=E6=88=96=E4=BF=9D=E5=AD=98=E8=BF=87=E7=9A=84=E9=80=80?= =?UTF-8?q?=E5=87=BA=E5=8F=AA=E6=9C=89=E4=B8=A4=E4=B8=AA=E9=80=89=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/NotepadMainFrame.java | 80 +++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/src/java2020spring/NotepadMainFrame.java b/src/java2020spring/NotepadMainFrame.java index a5abd41..526e7c0 100644 --- a/src/java2020spring/NotepadMainFrame.java +++ b/src/java2020spring/NotepadMainFrame.java @@ -417,9 +417,87 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ contentPane.add(toolState, BorderLayout.SOUTH); toolState.setVisible(false); - toolState.setFloatable(false);} + toolState.setFloatable(false); + // 创建弹出菜单 + final JPopupMenu jp=new JPopupMenu(); //创建弹出式菜单,下面三项是菜单项 + textArea.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + if(e.getButton()==MouseEvent.BUTTON3)//只响应鼠标右键单击事件 + { + jp.show(e.getComponent(),e.getX(),e.getY());//在鼠标位置显示弹出式菜单 + } + } + }); + isChanged(); + + this.MainFrameWidowListener(); +} + + +/*===============================1====================================*/ +/** + * 是否有变化 + */ +private void isChanged() { + textArea.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + //在这里我进行了对使用快捷键,但是没有输入字符却没有改变textArea中内容的判断 + Character c=e.getKeyChar(); + if(c != null && !textArea.getText().toString().equals("")){ + flag=2; + } + } + }); +} +/*===================================================================*/ + + +/*===============================2====================================*/ +/** + * 新建的或保存过的退出只有两个选择 + */ +private void MainFrameWidowListener() { + this.addWindowListener(new WindowAdapter(){ + @Override + public void windowClosing(WindowEvent e) { + if(flag==2 && currentPath==null){ + //这是弹出小窗口 + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.saveAs(); + }else if(result==JOptionPane.NO_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + }else if(flag==2 && currentPath!=null){ + //这是弹出小窗口 + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.save(); + }else if(result==JOptionPane.NO_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + }else{ + //这是弹出小窗口 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "确定关闭?", "系统提示", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + } + } + }); +} +/*===================================================================*/ + + private void addPopup(JTextArea textArea2, JPopupMenu popupMenu2) { // TODO 自动生成的方法存根 -- Gitee From 37b40203e42125f9471eb8e82c4a9f6f90278fa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E8=BF=B9s?= <澧ㄨ抗s@LAPTOP-QGOA2U92> Date: Mon, 7 Jun 2021 23:00:38 +0800 Subject: [PATCH 06/15] =?UTF-8?q?=E5=AF=B9=E8=8F=9C=E5=8D=95=E9=A1=B9?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E7=9A=84=E8=BF=9B=E4=B8=80=E6=AD=A5=E5=AE=8C?= =?UTF-8?q?=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/NotepadMainFrame.java | 92 +++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/src/java2020spring/NotepadMainFrame.java b/src/java2020spring/NotepadMainFrame.java index 526e7c0..23da0ce 100644 --- a/src/java2020spring/NotepadMainFrame.java +++ b/src/java2020spring/NotepadMainFrame.java @@ -496,7 +496,97 @@ private void MainFrameWidowListener() { }); } /*===================================================================*/ - +/*==============================3=====================================*/ +/** + * 行为动作 + */ +public void actionPerformed(ActionEvent e) { + if(e.getSource()==itemOpen){ //打开 + openFile(); + }else if(e.getSource()==itemSave){ //保存 + //如果该文件是打开的,就可以直接保存 + save(); + }else if(e.getSource()==itemSaveAs){ //另存为 + saveAs(); + }else if(e.getSource()==itemNew){ //新建 + newFile(); + }else if(e.getSource()==itemExit){ //退出 + exit(); + }else if(e.getSource()==itemPage){ //页面设置 + ///页面设置,百度到的,不知道具体的用法 + PageFormat pf = new PageFormat(); + PrinterJob.getPrinterJob().pageDialog(pf); + }else if(e.getSource()==itemPrint){ //打印 + //打印机 + Print(); + }else if(e.getSource()==itemUndo || e.getSource()==popM_Undo){ //撤销 + if(undoMgr.canUndo()){ + undoMgr.undo(); + } + }else if(e.getSource()==itemRedo || e.getSource()==popM_Redo){ //恢复 + if(undoMgr.canRedo()){ + undoMgr.redo(); + } + }else if(e.getSource()==itemCut || e.getSource()==popM_Cut){ //剪切 + cut(); + }else if(e.getSource()==itemCopy || e.getSource()==popM_Copy){ //复制 + copy(); + }else if(e.getSource()==itemPaste || e.getSource()==popM_Paste){ //粘贴 + paste(); + }else if(e.getSource()==itemDelete || e.getSource()==popM_Delete){ //删除 + String tem=textArea.getText().toString(); + textArea.setText(tem.substring(0,textArea.getSelectionStart())); + }else if(e.getSource()==itemFind){ //查找 + mySearch(); + }else if(e.getSource()==itemFindNext){ //查找下一个 + mySearch(); + }else if(e.getSource()==itemReplace){ //替换 + mySearch(); + }else if(e.getSource()==itemTurnTo){ //转到 + turnTo(); + }else if(e.getSource()==itemSelectAll || e.getSource()==popM_SelectAll){ //选择全部 + textArea.selectAll(); + }else if(e.getSource()==itemTime){ //时间/日期 + textArea.append(hour+":"+min+" "+c.get(Calendar.YEAR)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DAY_OF_MONTH)); + }else if(e.getSource()==itemNextLine){ //设置自动换行 + //设置文本区的换行策略。如果设置为 true,则当行的长度大于所分配的宽度时,将换行。此属性默认为 false。 + if(itemNextLine.isSelected()){ + textArea.setLineWrap(true); + }else{ + textArea.setLineWrap(false); + } + }else if(e.getSource()==itemFont){ //设置字体大小 + // 构造字体选择器,参数字体为预设值 + // MQFontChooser fontChooser = new MQFontChooser(textArea.getFont()); + //fontChooser.showFontDialog(this); + // Font font = fontChooser.getSelectFont(); + // 将字体设置到JTextArea中 + //textArea.setFont(font); + }else if(e.getSource()==itemColor){ //设置背景颜色 + jcc1 = new JColorChooser(); + JOptionPane.showMessageDialog(this, jcc1,"选择背景颜色颜色",-1); + color = jcc1.getColor(); + textArea.setBackground(color); + }else if(e.getSource()==itemFontColor){ //设置字体颜色 + jcc1=new JColorChooser(); + JOptionPane.showMessageDialog(this, jcc1, "选择字体颜色", -1); + color = jcc1.getColor(); + //String string=textArea.getSelectedText(); + textArea.setForeground(color); + }else if(e.getSource()==itemStatement){ //设置状态 + if(itemStatement.isSelected()){ + //scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); + toolState.setVisible(true); + }else{ + //scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + toolState.setVisible(false); + } + }else if(e.getSource()==itemSearchForHelp){ + JOptionPane.showMessageDialog(this, "HELLO","行动起来",1); + }else if(e.getSource()==itemAboutNotepad){ + JOptionPane.showMessageDialog(this, "哈哈","说明文档",1); + } +} private void addPopup(JTextArea textArea2, JPopupMenu popupMenu2) { -- Gitee From f34d30bdb0f53c13c0f963e5dfc6ae68cb84f69b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E8=BF=B9s?= <澧ㄨ抗s@LAPTOP-QGOA2U92> Date: Tue, 8 Jun 2021 10:01:30 +0800 Subject: [PATCH 07/15] =?UTF-8?q?=E8=AE=BE=E7=BD=AEturnto=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E8=BD=AC=E5=88=B0=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E8=A1=8C=EF=BC=8C=E5=BC=B9=E5=87=BA=E5=AF=B9=E8=AF=9D=E6=A1=86?= =?UTF-8?q?=EF=BC=8C=E5=AF=B9=E8=AF=A5=E5=AF=B9=E8=AF=9D=E6=A1=86=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E7=95=8C=E9=9D=A2=E8=AE=BE=E7=BD=AE=E5=92=8C=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E7=9B=91=E5=90=AC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/NotepadMainFrame.java | 77 ++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/src/java2020spring/NotepadMainFrame.java b/src/java2020spring/NotepadMainFrame.java index 23da0ce..98def7a 100644 --- a/src/java2020spring/NotepadMainFrame.java +++ b/src/java2020spring/NotepadMainFrame.java @@ -587,6 +587,74 @@ public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(this, "哈哈","说明文档",1); } } +/*===================================================================*/ + +//转到指定行,弹出对话框 +private void turnTo() { + final JDialog gotoDialog = new JDialog(this, "转到下列行"); + JLabel gotoLabel = new JLabel("行数(L):"); + final JTextField linenum = new JTextField(5); + linenum.setText("1"); + linenum.selectAll(); + + JButton okButton = new JButton("确定"); + okButton.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + int totalLine = textArea.getLineCount(); + int[] lineNumber = new int[totalLine + 1]; + String s = textArea.getText(); + int pos = 0, t = 0; + + while (true) { + pos = s.indexOf('\12', pos); + // System.out.println("引索pos:"+pos); + if (pos == -1) + break; + lineNumber[t++] = pos++; + } + + int gt = 1; + try { + gt = Integer.parseInt(linenum.getText());// 转换为基本数据类型int + } catch (NumberFormatException efe) { + JOptionPane.showMessageDialog(null, "请输入行数!", "提示", JOptionPane.WARNING_MESSAGE); + linenum.requestFocus(true); + return; + } + + if (gt < 2 || gt >= totalLine) { + if (gt < 2) + textArea.setCaretPosition(0);//将滚动条一直固定在JTextArea的顶端 + else + textArea.setCaretPosition(s.length());//将滚动条滑到JTextArea的底端 + } else + textArea.setCaretPosition(lineNumber[gt - 2] + 1); + + gotoDialog.dispose(); + } + + }); + + JButton cancelButton = new JButton("取消"); + cancelButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + gotoDialog.dispose(); + } + }); + + Container con = gotoDialog.getContentPane(); + con.setLayout(new FlowLayout()); + con.add(gotoLabel); + con.add(linenum); + con.add(okButton); + con.add(cancelButton); + + gotoDialog.setSize(200, 100); + gotoDialog.setResizable(false); + gotoDialog.setLocation(300, 280); + gotoDialog.setVisible(true); +} private void addPopup(JTextArea textArea2, JPopupMenu popupMenu2) { @@ -596,8 +664,7 @@ public void actionPerformed(ActionEvent e) { - @Override - public void actionPerformed(ActionEvent e) { - // TODO 自动生成的方法存根 - - }} + + + + } -- Gitee From c75785c5668f95129a94eb248c2caa5dd73d30d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E8=BF=B9s?= <澧ㄨ抗s@LAPTOP-QGOA2U92> Date: Tue, 8 Jun 2021 13:11:51 +0800 Subject: [PATCH 08/15] =?UTF-8?q?=E8=BF=99=E4=B8=AA=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E6=98=AF=E5=AF=B9=E6=96=87=E6=9C=AC=E5=86=85=E5=AE=B9=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E7=9A=84=E5=BC=B9=E5=87=BA=E6=A1=86=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=E8=AE=BE=E8=AE=A1=E4=BB=A5=E5=8F=8A=E5=8A=9F=E8=83=BD=E7=9A=84?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/NotepadMainFrame.java | 36 ++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/java2020spring/NotepadMainFrame.java b/src/java2020spring/NotepadMainFrame.java index 98def7a..0884b8f 100644 --- a/src/java2020spring/NotepadMainFrame.java +++ b/src/java2020spring/NotepadMainFrame.java @@ -655,8 +655,40 @@ private void turnTo() { gotoDialog.setLocation(300, 280); gotoDialog.setVisible(true); } - - +/*===============================8====================================*/ +/** + * 推出按钮,和窗口的红叉实现一样的功能 + */ +private void exit() { + if(flag==2 && currentPath==null){ + //这是弹出小窗口 + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.saveAs(); + }else if(result==JOptionPane.NO_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + }else if(flag==2 && currentPath!=null){ + //这是弹出小窗口 + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.save(); + }else if(result==JOptionPane.NO_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + }else{ + //这是弹出小窗口 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "确定关闭?", "系统提示", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + } +} private void addPopup(JTextArea textArea2, JPopupMenu popupMenu2) { // TODO 自动生成的方法存根 -- Gitee From be48dd3c80ae0dcccf4514a730450083cd01e354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E8=BF=B9s?= <澧ㄨ抗s@LAPTOP-QGOA2U92> Date: Tue, 8 Jun 2021 16:57:34 +0800 Subject: [PATCH 09/15] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E5=AF=B9=E6=94=B9=E8=BF=87=E7=9A=84=E5=92=8C=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E8=BF=87=E7=9A=84=E6=96=B0=E5=BB=BA=E6=96=87=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/NotepadMainFrame.java | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/java2020spring/NotepadMainFrame.java b/src/java2020spring/NotepadMainFrame.java index 0884b8f..9f84861 100644 --- a/src/java2020spring/NotepadMainFrame.java +++ b/src/java2020spring/NotepadMainFrame.java @@ -689,6 +689,43 @@ private void exit() { } } } +/*===============================4====================================*/ +/** + * 新建文件,只有改过的和保存过的需要处理 + */ +private void newFile() { + if(flag==0 || flag==1){ //刚启动记事本为0,刚新建文档为1 + return; + }else if(flag==2 && this.currentPath==null){ //修改后 + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + this.saveAs(); //另存为 + }else if(result==JOptionPane.NO_OPTION){ + this.textArea.setText(""); + this.setTitle("无标题"); + flag=1; + } + return; + }else if(flag==2 && this.currentPath!=null ){ + //2、(保存的文件为3)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+this.currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + this.save(); //直接保存,有路径 + }else if(result==JOptionPane.NO_OPTION){ + this.textArea.setText(""); + this.setTitle("无标题"); + flag=1; + } + }else if(flag==3){ //保存的文件 + this.textArea.setText(""); + flag=1; + this.setTitle("无标题"); + } +} +/*===================================================================*/ + + private void addPopup(JTextArea textArea2, JPopupMenu popupMenu2) { // TODO 自动生成的方法存根 -- Gitee From d03881bc6944e28fe028943069b9a9aa213da6c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E8=BF=B9s?= <澧ㄨ抗s@LAPTOP-QGOA2U92> Date: Tue, 8 Jun 2021 23:53:25 +0800 Subject: [PATCH 10/15] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=8A=B6=E6=80=81=20=E5=8F=A6=E5=AD=98?= =?UTF-8?q?=E4=B8=BA=20=E5=92=8C=20=E4=BF=9D=E5=AD=98=20=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/NotepadMainFrame.java | 68 +++++++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/src/java2020spring/NotepadMainFrame.java b/src/java2020spring/NotepadMainFrame.java index 9f84861..254fc11 100644 --- a/src/java2020spring/NotepadMainFrame.java +++ b/src/java2020spring/NotepadMainFrame.java @@ -724,8 +724,72 @@ private void newFile() { } } /*===================================================================*/ - - +/*===============================5====================================*/ +/** + * 另存为 + */ +private void saveAs() { + //打开保存框 + JFileChooser choose=new JFileChooser(); + //选择文件 + int result=choose.showSaveDialog(this); + if(result==JFileChooser.APPROVE_OPTION){ + //取得选择的文件[文件名是自己输入的] + File file=choose.getSelectedFile(); + FileWriter fw=null; + //保存 + try { + fw=new FileWriter(file); + fw.write(textArea.getText()); + currentFileName=file.getName(); + currentPath=file.getAbsolutePath(); + //如果比较少,需要写 + fw.flush(); + this.flag=3; + this.setTitle(currentPath); + } catch (IOException e1) { + e1.printStackTrace(); //在命令行打印异常信息在程序中出错的位置及原因。 + }finally{ + try { + if(fw!=null) fw.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + } +} +/*===================================================================*/ +/*===============================6====================================*/ +/** + * 保存 + */ +private void save() { + if(this.currentPath==null){ + this.saveAs(); + if(this.currentPath==null){ + return; + } + } + FileWriter fw=null; + //保存 + try { + fw=new FileWriter(new File(currentPath)); + fw.write(textArea.getText()); + //如果比较少,需要写 + fw.flush(); + flag=3; + this.setTitle(this.currentPath); + } catch (IOException e1) { + e1.printStackTrace(); + }finally{ + try { + if(fw!=null) fw.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } +} +/*===================================================================*/ private void addPopup(JTextArea textArea2, JPopupMenu popupMenu2) { // TODO 自动生成的方法存根 -- Gitee From 9a2b3a1f068b9b4d4e4f5de0ea9e3b4b74f6b3b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E8=BF=B9s?= <澧ㄨ抗s@LAPTOP-QGOA2U92> Date: Wed, 9 Jun 2021 19:35:00 +0800 Subject: [PATCH 11/15] =?UTF-8?q?=E5=BB=BA=E7=AB=8B=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=B5=81=EF=BC=8C=E5=AE=9E=E7=8E=B0=E6=89=93=E5=8D=B0=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E4=BB=A5=E5=8F=8A=E5=89=AA=E5=88=87=E3=80=81?= =?UTF-8?q?=E5=A4=8D=E5=88=B6=E3=80=81=E7=B2=98=E8=B4=B4=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/NotepadMainFrame.java | 148 +++++++++++++++++++++++ 1 file changed, 148 insertions(+) diff --git a/src/java2020spring/NotepadMainFrame.java b/src/java2020spring/NotepadMainFrame.java index 254fc11..34ccb04 100644 --- a/src/java2020spring/NotepadMainFrame.java +++ b/src/java2020spring/NotepadMainFrame.java @@ -789,6 +789,154 @@ private void save() { } } } +/*================================7===================================*/ +/** + * 打开文件 + */ +private void openFile() { + if(flag==2 && this.currentPath==null){ + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + this.saveAs(); //另存为 + } + }else if(flag==2 && this.currentPath!=null){ + //2、(打开的文件2,保存的文件3)条件下修改 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+this.currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + this.save(); //保存 + } + } + //打开文件选择框 + JFileChooser choose=new JFileChooser(); + //选择文件 + int result=choose.showOpenDialog(this); + if(result==JFileChooser.APPROVE_OPTION){ + //取得选择的文件 + File file=choose.getSelectedFile(); + //打开已存在的文件,提前将文件名存起来 + currentFileName=file.getName(); + //存在文件全路径 + currentPath=file.getAbsolutePath(); + flag=3; + this.setTitle(this.currentPath); + BufferedReader br=null; + try { + //建立文件流[字符流] + InputStreamReader isr=new InputStreamReader(new FileInputStream(file),"GBK"); + br=new BufferedReader(isr);//动态绑定 + //读取内容 + StringBuffer sb=new StringBuffer(); + String line=null; + while((line=br.readLine())!=null){ + sb.append(line+System.lineSeparator()); + } + //显示在文本框[多框] + textArea.setText(sb.toString()); + } catch (FileNotFoundException e1) { + e1.printStackTrace(); + } catch (IOException e1) { + e1.printStackTrace(); + } finally{ + try { + if(br!=null) br.close(); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + } +} +/*================================================================*/ + + +/*=============================9===================================*/ +public void Print() +{ + try{ + p = getToolkit().getPrintJob(this,"ok",null);//创建一个Printfjob 对象 p + g = p.getGraphics();//p 获取一个用于打印的 Graphics 的对象 + //g.translate(120,200);//改变组建的位置 + this.textArea.printAll(g); + p.end();//释放对象 g + } + catch(Exception a){ + + } +} +/*================================================================*/ + + +private static void addPopup(Component component, final JPopupMenu popup) { + component.addMouseListener(new MouseAdapter() { + public void mousePressed(MouseEvent e) { + if (e.isPopupTrigger()) { //判断用户是否要求弹出菜单 + showMenu(e); + } + } + public void mouseReleased(MouseEvent e) { + if (e.isPopupTrigger()) { + showMenu(e); + } + } + private void showMenu(MouseEvent e) { + popup.show(e.getComponent(), e.getX(), e.getY()); + } + }); +} + +public void cut(){ + copy(); + //标记开始位置 + int start = this.textArea.getSelectionStart(); + //标记结束位置 + int end = this.textArea.getSelectionEnd(); + //删除所选段 + this.textArea.replaceRange("", start, end); + +} + +public void copy(){ + //拖动选取文本 + String temp = this.textArea.getSelectedText(); + //把获取的内容复制到连续字符器,这个类继承了剪贴板接口 + StringSelection text = new StringSelection(temp); + //把内容放在剪贴板 + this.clipboard.setContents(text, null); +} + + public void paste(){ + //Transferable接口,把剪贴板的内容转换成数据 + Transferable contents = this.clipboard.getContents(this); + //DataFalvor类判断是否能把剪贴板的内容转换成所需数据类型 + DataFlavor flavor = DataFlavor.stringFlavor; + //如果可以转换 + if(contents.isDataFlavorSupported(flavor)){ + String str; + try {//开始转换 + str=(String)contents.getTransferData(flavor); + //如果要粘贴时,鼠标已经选中了一些字符 + if(this.textArea.getSelectedText()!=null){ + //定位被选中字符的开始位置 + int start = this.textArea.getSelectionStart(); + //定位被选中字符的末尾位置 + int end = this.textArea.getSelectionEnd(); + //把粘贴的内容替换成被选中的内容 + this.textArea.replaceRange(str, start, end); + }else{ + //获取鼠标所在TextArea的位置 + int mouse = this.textArea.getCaretPosition(); + //在鼠标所在的位置粘贴内容 + this.textArea.insert(str, mouse); + } + } catch(UnsupportedFlavorException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch(IllegalArgumentException e){ + e.printStackTrace(); + } + } + } /*===================================================================*/ private void addPopup(JTextArea textArea2, JPopupMenu popupMenu2) { // TODO 自动生成的方法存根 -- Gitee From 86a573ebb7b8d1b268b7d6f8541b2d98277df438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E8=BF=B9s?= <澧ㄨ抗s@LAPTOP-QGOA2U92> Date: Thu, 10 Jun 2021 13:31:45 +0800 Subject: [PATCH 12/15] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E4=B9=8B?= =?UTF-8?q?=E5=89=8D=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A=E8=AF=B4=E6=98=8E?= =?UTF-8?q?=EF=BC=8C=E6=94=B9=E8=BF=9B=E4=BA=86=E5=B8=AE=E5=8A=A9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=E6=96=87=E6=A1=A3=EF=BC=8C=E5=B9=B6=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E6=9F=A5=E6=89=BE=E4=B8=8E=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E7=9A=84=E5=AF=B9=E8=AF=9D=E6=A1=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/NotepadMainFrame.java | 1463 ++++++++++++---------- 1 file changed, 817 insertions(+), 646 deletions(-) diff --git a/src/java2020spring/NotepadMainFrame.java b/src/java2020spring/NotepadMainFrame.java index 34ccb04..467a489 100644 --- a/src/java2020spring/NotepadMainFrame.java +++ b/src/java2020spring/NotepadMainFrame.java @@ -1,25 +1,79 @@ -package java2020spring; +package java2020spring; import java.awt.BorderLayout; import java.awt.Color; +import java.awt.Component; +import java.awt.Container; +import java.awt.Dimension; import java.awt.EventQueue; +import java.awt.FlowLayout; +import java.awt.Font; +import java.awt.Graphics; +import java.awt.GridLayout; +import java.awt.Image; +import java.awt.PrintJob; +import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.InputEvent; +import java.awt.event.KeyAdapter; +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.print.PageFormat; +import java.awt.print.PrinterJob; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; import java.util.Calendar; import java.util.GregorianCalendar; -import javax.swing.border.*; +import javax.swing.BorderFactory; +import javax.swing.ButtonGroup; +import javax.swing.ImageIcon; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JCheckBoxMenuItem; +import javax.swing.JColorChooser; +import javax.swing.JDialog; +import javax.swing.JFileChooser; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JPopupMenu; +import javax.swing.JRadioButton; +import javax.swing.JScrollPane; +import javax.swing.JSeparator; +import javax.swing.JTextArea; +import javax.swing.JTextField; +import javax.swing.JToolBar; +import javax.swing.KeyStroke; +import javax.swing.UIManager; +import javax.swing.UnsupportedLookAndFeelException; +import javax.swing.border.EmptyBorder; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; import javax.swing.undo.UndoManager; -import javax.swing.*; -import javax.swing.JPopupMenu.*; - +//各种UndoableEdit实例通过addEdit方法加入UndoManager,通过调用UndoManager的undo/redo方法来实现Undo/Redo功能。 public class NotepadMainFrame extends JFrame implements ActionListener{ /** * 序列号 */ - private static final long serialVersionUID = 8585210209467333480L; + //private static final long serialVersionUID = 8585210209467333480L;//实现序列化接口的实体能够兼容先前版本,就需要显示的定义一个serialVersionUID private JPanel contentPane; private JTextArea textArea; private JMenuItem itemOpen; @@ -33,6 +87,11 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ //当前文件名 String currentFileName=null; + PrintJob p=null;//声明一个要打印的对象 + Graphics g=null;//要打印的对象 + + //当前文件路径 + String currentPath=null; //背景颜色 JColorChooser jcc1=null; @@ -42,53 +101,55 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ int linenum = 1; int columnnum = 1; - + //撤销管理器 + public UndoManager undoMgr = new UndoManager(); //剪贴板 public Clipboard clipboard = new Clipboard("系统剪切板"); - - private JMenuItem itemSaveAs; - private JMenuItem itemNew; - private JMenuItem itemPage; - private JSeparator separator; - private JMenuItem itemPrint; - private JMenuItem itemExit; - private JSeparator separator_1; - private JMenu itemEdit; - private JMenu itFormat; - private JMenu itemCheck; - private JMenu itemHelp; - private JMenuItem itemSearchForHelp; - private JMenuItem itemAboutNotepad; - private JMenuItem itemUndo; - private JMenuItem itemCut; - private JMenuItem itemCopy; - private JMenuItem itemPaste; - private JMenuItem itemDelete; - private JMenuItem itemFind; - private JMenuItem itemFindNext; - private JMenuItem itemReplace; - private JMenuItem itemTurnTo; - private JMenuItem itemSelectAll; - private JMenuItem itemTime; - private JMenuItem itemFont; - private JMenuItem itemColor; - private JMenuItem itemFontColor; - private JCheckBoxMenuItem itemNextLine; - private JScrollPane scrollPane; - private JCheckBoxMenuItem itemStatement; - private JToolBar toolState; - public static JLabel label1; - private JLabel label2; - private JLabel label3; - int length=0; - int sum=0; - public UndoManager undoMgr = new UndoManager(); + private JMenuItem itemSaveAs; + private JMenuItem itemNew; + private JMenuItem itemPage; + private JSeparator separator; + private JMenuItem itemPrint; + private JMenuItem itemExit; + private JSeparator separator_1; + private JMenu itemEdit; + private JMenu itFormat; + private JMenu itemCheck; + private JMenu itemHelp; + private JMenuItem itemSearchForHelp; + private JMenuItem itemAboutNotepad; + private JMenuItem itemUndo; + private JMenuItem itemCut; + private JMenuItem itemCopy; + private JMenuItem itemPaste; + private JMenuItem itemDelete; + private JMenuItem itemFind; + private JMenuItem itemFindNext; + private JMenuItem itemReplace; + private JMenuItem itemTurnTo; + private JMenuItem itemSelectAll; + private JMenuItem itemTime; + private JMenuItem itemFont; + private JMenuItem itemColor; + private JMenuItem itemFontColor; + private JCheckBoxMenuItem itemNextLine; + private JScrollPane scrollPane; + private JCheckBoxMenuItem itemStatement; + private JToolBar toolState; + public static JLabel label1; + private JLabel label2; + private JLabel label3; + int length=0; + int sum=0; + /** * Launch the application. */ - public static void main(String[] args) { + public static void main(String[] args) { + //使事件派发线程上的可运行对象排队。当可运行对象排在事件派发队列的队首时,就调用其run方法。 + //其效果是允许事件派发线程调用另一个线程中的任意一个代码块,只有从事件派发线程才能更新组件。 EventQueue.invokeLater(new Runnable() { public void run() { try { @@ -101,11 +162,11 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ }); } - GregorianCalendar c=new GregorianCalendar(); + GregorianCalendar c=new GregorianCalendar(); //构造一个具有给定日期和时间设置的 GregorianCalendar int hour=c.get(Calendar.HOUR_OF_DAY); int min=c.get(Calendar.MINUTE); int second=c.get(Calendar.SECOND); - private JPopupMenu popupMenu; + private JPopupMenu popupMenu;//弹出式菜单、右键菜单 private JMenuItem popM_Undo; private JMenuItem popM_Cut; private JMenuItem popM_Copy; @@ -117,7 +178,7 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ private JMenuItem popM_closeIMe; private JMenuItem popM_InsertUnicode; private JMenuItem popM_RestartSelect; - private JSeparator separator_2; + private JSeparator separator_2; //添加一条分隔符 private JSeparator separator_3; private JSeparator separator_4; private JSeparator separator_5; @@ -126,12 +187,15 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ private JSeparator separator_7; private JSeparator separator_8; private JMenuItem popM_Redo; + + + /** * Create the frame. */ public NotepadMainFrame() { try { - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());//把图形界面外观设置成用户所使用的平台的外观 } catch (ClassNotFoundException e1) { e1.printStackTrace(); } catch (InstantiationException e1) { @@ -141,29 +205,34 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ } catch (UnsupportedLookAndFeelException e1) { e1.printStackTrace(); } - setTitle("notepad"); + setTitle("notepad"); + + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); setBounds(100, 100, 521, 572); - JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); + + + + //第一个菜单 JMenu itemFile = new JMenu("文件(F)"); - itemFile.setMnemonic('F'); + itemFile.setMnemonic('F'); //设置键盘助记符 menuBar.add(itemFile); itemNew = new JMenuItem("新建(N)",'N'); - itemNew.setAccelerator(KeyStroke.getKeyStroke("control N ")); + itemNew.setAccelerator(KeyStroke.getKeyStroke("control N")); itemNew.addActionListener(this); itemFile.add(itemNew); itemOpen = new JMenuItem("打开(O)",'O'); - itemOpen.setAccelerator(KeyStroke.getKeyStroke("control O ")); + itemOpen.setAccelerator(KeyStroke.getKeyStroke("control O")); itemOpen.addActionListener(this); itemFile.add(itemOpen); itemSave = new JMenuItem("保存(S)"); - itemSave.setAccelerator(KeyStroke.getKeyStroke("control S ")); + itemSave.setAccelerator(KeyStroke.getKeyStroke("control S")); itemSave.addActionListener(this); itemFile.add(itemSave); @@ -179,7 +248,7 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ itemFile.add(itemPage); itemPrint = new JMenuItem("打印(P)...",'P'); - itemPrint.setAccelerator(KeyStroke.getKeyStroke("control P ")); + itemPrint.setAccelerator(KeyStroke.getKeyStroke("control P")); itemPrint.addActionListener(this); itemFile.add(itemPrint); @@ -189,7 +258,7 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ itemExit = new JMenuItem("退出(X)",'X'); itemExit.addActionListener(this); itemFile.add(itemExit); - + //第二个菜单 itemEdit = new JMenu("编辑(E)"); itemEdit.setMnemonic('E'); menuBar.add(itemEdit); @@ -200,12 +269,12 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ itemEdit.add(itemUndo); itemRedo = new JMenuItem("恢复(R)"); - itemRedo.setAccelerator(KeyStroke.getKeyStroke("control R ")); + itemRedo.setAccelerator(KeyStroke.getKeyStroke("control R")); itemRedo.addActionListener(this); itemEdit.add(itemRedo); itemCut = new JMenuItem("剪切(T)",'T'); - itemCut.setAccelerator(KeyStroke.getKeyStroke("control X ")); + itemCut.setAccelerator(KeyStroke.getKeyStroke("control X")); itemCut.addActionListener(this); separator_6 = new JSeparator(); @@ -214,7 +283,7 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ itemCopy = new JMenuItem("复制(C)",'C'); itemCopy.addActionListener(this); - itemCopy.setAccelerator(KeyStroke.getKeyStroke("control C ")); + itemCopy.setAccelerator(KeyStroke.getKeyStroke("control C")); itemEdit.add(itemCopy); itemPaste = new JMenuItem("粘贴(P)",'P'); @@ -224,14 +293,14 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ itemDelete = new JMenuItem("删除(L)",'L'); itemDelete.addActionListener(this); - itemDelete.setAccelerator(KeyStroke.getKeyStroke("control D ")); + itemDelete.setAccelerator(KeyStroke.getKeyStroke("control D")); itemEdit.add(itemDelete); separator_7 = new JSeparator(); itemEdit.add(separator_7); itemFind = new JMenuItem("查找(F)",'F'); - itemFind.setAccelerator(KeyStroke.getKeyStroke("control F ")); + itemFind.setAccelerator(KeyStroke.getKeyStroke("control F")); itemFind.addActionListener(this); itemEdit.add(itemFind); @@ -242,26 +311,32 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ itemReplace = new JMenuItem("替换(R)",'R'); itemReplace.addActionListener(this); - itemReplace.setAccelerator(KeyStroke.getKeyStroke("control H ")); + itemReplace.setAccelerator(KeyStroke.getKeyStroke("control H")); itemEdit.add(itemReplace); itemTurnTo = new JMenuItem("转到(G)",'G'); itemTurnTo.addActionListener(this); - itemTurnTo.setAccelerator(KeyStroke.getKeyStroke("control G ")); + itemTurnTo.setAccelerator(KeyStroke.getKeyStroke("control G")); itemEdit.add(itemTurnTo); itemSelectAll = new JMenuItem("全选(A)",'A'); - itemSelectAll.setAccelerator(KeyStroke.getKeyStroke("control A ")); + itemSelectAll.setAccelerator(KeyStroke.getKeyStroke("control A")); itemSelectAll.addActionListener(this); separator_8 = new JSeparator(); itemEdit.add(separator_8); itemEdit.add(itemSelectAll); + + + + + itemTime = new JMenuItem("时间/日期(D)",'D'); itemTime.addActionListener(this); itemTime.setAccelerator(KeyStroke.getKeyStroke("F5")); itemEdit.add(itemTime); + //第三个菜单 itFormat = new JMenu("格式(O)"); itFormat.setMnemonic('O'); menuBar.add(itFormat); @@ -282,6 +357,7 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ itemFontColor.addActionListener(this); itFormat.add(itemFontColor); + //第四个菜单 itemCheck = new JMenu("查看(V)"); itemCheck.setMnemonic('V'); menuBar.add(itemCheck); @@ -289,7 +365,8 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ itemStatement = new JCheckBoxMenuItem("状态栏(S)"); itemStatement.addActionListener(this); itemCheck.add(itemStatement); - + + //第五个菜单 itemHelp = new JMenu("帮助(H)"); itemHelp.setMnemonic('H'); menuBar.add(itemHelp); @@ -302,453 +379,554 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ itemAboutNotepad.addActionListener(this); itemHelp.add(itemAboutNotepad); contentPane = new JPanel(); - TitledBorder title=new TitledBorder(new LineBorder(Color.BLACK),""); - contentPane.setBorder(title); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + //设置边框布局 - contentPane.setLayout(new BorderLayout(5,5)); + contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); textArea = new JTextArea(); - //VERTICAL垂直 HORIZONTAL水平 + //设置滚动条 VERTICAL垂直 HORIZONTAL水平 scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); - + //contentPane2=new JPanel(); //contentPane2.setSize(10,textArea.getSize().height); //contentPane.add(contentPane2, BorderLayout.WEST); - popupMenu = new JPopupMenu(); - addPopup(textArea, popupMenu); - - popM_Undo = new JMenuItem("撤销(U)"); - popM_Undo.addActionListener(this); - popupMenu.add(popM_Undo); - - popM_Redo = new JMenuItem("恢复(R)"); - popM_Redo.addActionListener(this); - popupMenu.add(popM_Redo); - - separator_2 = new JSeparator(); - popupMenu.add(separator_2); - - popM_Cut = new JMenuItem("剪切(T)"); - popM_Cut.addActionListener(this); - popupMenu.add(popM_Cut); - - popM_Copy = new JMenuItem("复制(C)"); - popM_Copy.addActionListener(this); - popupMenu.add(popM_Copy); - - popM_Paste = new JMenuItem("粘贴(P)"); - popM_Paste.addActionListener(this); - popupMenu.add(popM_Paste); - - popM_Delete = new JMenuItem("删除(D)"); - popM_Delete.addActionListener(this); - popupMenu.add(popM_Delete); - - separator_3 = new JSeparator(); - popupMenu.add(separator_3); - - popM_SelectAll = new JMenuItem("全选(A)"); - popM_SelectAll.addActionListener(this); - popupMenu.add(popM_SelectAll); - - separator_4 = new JSeparator(); - popupMenu.add(separator_4); - - popM_toLeft = new JMenuItem("从右到左的阅读顺序(R)"); - popM_toLeft.addActionListener(this); - popupMenu.add(popM_toLeft); - - popM_showUnicode = new JMenuItem("显示Unicode控制字符(S)"); - popM_showUnicode.addActionListener(this); - popupMenu.add(popM_showUnicode); - - popM_InsertUnicode = new JMenuItem("插入Unicode控制字符(I)"); - popM_InsertUnicode.addActionListener(this); - popupMenu.add(popM_InsertUnicode); - - separator_5 = new JSeparator(); - popupMenu.add(separator_5); + + + //设置右键弹出的菜单 + popupMenu = new JPopupMenu(); + addPopup(textArea, popupMenu); + + + popM_Undo = new JMenuItem("撤销(U)"); + popM_Undo.addActionListener(this); + popupMenu.add(popM_Undo); + + popM_Redo = new JMenuItem("恢复(R)"); + popM_Redo.addActionListener(this); + popupMenu.add(popM_Redo); + + separator_2 = new JSeparator(); + popupMenu.add(separator_2); + + popM_Cut = new JMenuItem("剪切(T)"); + popM_Cut.addActionListener(this); + popupMenu.add(popM_Cut); + + popM_Copy = new JMenuItem("复制(C)"); + popM_Copy.addActionListener(this); + popupMenu.add(popM_Copy); + + popM_Paste = new JMenuItem("粘贴(P)"); + popM_Paste.addActionListener(this); + popupMenu.add(popM_Paste); + + popM_Delete = new JMenuItem("删除(D)"); + popM_Delete.addActionListener(this); + popupMenu.add(popM_Delete); + + separator_3 = new JSeparator(); + popupMenu.add(separator_3); + + popM_SelectAll = new JMenuItem("全选(A)"); + popM_SelectAll.addActionListener(this); + popupMenu.add(popM_SelectAll); + + separator_4 = new JSeparator(); + popupMenu.add(separator_4); + + popM_toLeft = new JMenuItem("从右到左的阅读顺序(R)"); + popM_toLeft.addActionListener(this); + popupMenu.add(popM_toLeft); + + popM_showUnicode = new JMenuItem("显示Unicode控制字符(S)"); + popM_showUnicode.addActionListener(this); + popupMenu.add(popM_showUnicode); + + popM_InsertUnicode = new JMenuItem("插入Unicode控制字符(I)"); + popM_InsertUnicode.addActionListener(this); + popupMenu.add(popM_InsertUnicode); + + separator_5 = new JSeparator(); + popupMenu.add(separator_5); + + popM_closeIMe = new JMenuItem("关闭IME(L)"); + popM_closeIMe.addActionListener(this); + popupMenu.add(popM_closeIMe); + + popM_RestartSelect = new JMenuItem("汉字重选(R)"); + popM_RestartSelect.addActionListener(this); + popupMenu.add(popM_RestartSelect); + //添加到面板中【中间】 + contentPane.add(scrollPane, BorderLayout.CENTER); + + //添加撤销管理器 + textArea.getDocument().addUndoableEditListener(undoMgr); + + + //创建一个工具栏 toolBar + toolState = new JToolBar(); + toolState.setSize(textArea.getSize().width, 10);//toolState.setLayout(new FlowLayout(FlowLayout.LEFT)); + label1 = new JLabel(" 当前系统时间:" + hour + ":" + min + ":" + second+" "); + toolState.add(label1); + toolState.addSeparator(); + label2 = new JLabel(" 第 " + linenum + " 行, 第 " + columnnum+" 列 "); + toolState.add(label2); + toolState.addSeparator(); + + label3 = new JLabel(" 一共 " +length+" 字 "); + toolState.add(label3); + textArea.addCaretListener(new CaretListener() { //记录行数和列数 + public void caretUpdate(CaretEvent e) { + //sum=0; + JTextArea editArea = (JTextArea)e.getSource(); + + try { + int caretpos = editArea.getCaretPosition(); + linenum = editArea.getLineOfOffset(caretpos); + columnnum = caretpos - textArea.getLineStartOffset(linenum); + linenum += 1; + label2.setText(" 第 " + linenum + " 行, 第 " + (columnnum+1)+" 列 "); + //sum+=columnnum+1; + //length+=sum; + length=NotepadMainFrame.this.textArea.getText().toString().length(); + label3.setText(" 一共 " +length+" 字 "); + } + catch(Exception ex) { } + }}); + + contentPane.add(toolState, BorderLayout.SOUTH); + toolState.setVisible(false); + toolState.setFloatable(false); + + + + + // 创建弹出菜单 + final JPopupMenu jp=new JPopupMenu(); //创建弹出式菜单,下面三项是菜单项 + textArea.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + if(e.getButton()==MouseEvent.BUTTON3)//只响应鼠标右键单击事件 + { + jp.show(e.getComponent(),e.getX(),e.getY());//在鼠标位置显示弹出式菜单 + } + } + }); + + isChanged(); + + this.MainFrameWidowListener(); + } + - popM_closeIMe = new JMenuItem("关闭IME(L)"); - popM_closeIMe.addActionListener(this); - popupMenu.add(popM_closeIMe); + /*===============================1====================================*/ + /** + * 是否有变化 + */ + private void isChanged() { + textArea.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + //在这里我进行了对使用快捷键,但是没有输入字符却没有改变textArea中内容的判断 + Character c=e.getKeyChar(); + if(c != null && !textArea.getText().toString().equals("")){ + flag=2; + } + } + }); + } + /*===================================================================*/ - popM_RestartSelect = new JMenuItem("汉字重选(R)"); - popM_RestartSelect.addActionListener(this); - popupMenu.add(popM_RestartSelect); - //添加到面板中【中间】 - contentPane.add(scrollPane, BorderLayout.CENTER); - //添加撤销管理器 - textArea.getDocument().addUndoableEditListener(undoMgr); - + /*===============================2====================================*/ + /** + * 新建的或保存过的退出只有两个选择 + */ + private void MainFrameWidowListener() { + this.addWindowListener(new WindowAdapter(){ + @Override + public void windowClosing(WindowEvent e) { + if(flag==2 && currentPath==null){ + //这是弹出小窗口 + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.saveAs(); + }else if(result==JOptionPane.NO_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + }else if(flag==2 && currentPath!=null){ + //这是弹出小窗口 + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.save(); + }else if(result==JOptionPane.NO_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + }else{ + //这是弹出小窗口 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "确定关闭?", "系统提示", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + } + } + }); + } + /*===================================================================*/ - toolState = new JToolBar(); - toolState.setSize(textArea.getSize().width, 10);//toolState.setLayout(new FlowLayout(FlowLayout.LEFT)); - label1 = new JLabel(" 当前系统时间:" + hour + ":" + min + ":" + second+" "); - toolState.add(label1); - toolState.addSeparator(); - label2 = new JLabel(" 第 " + linenum + " 行, 第 " + columnnum+" 列 "); - toolState.add(label2); - toolState.addSeparator(); - label3 = new JLabel(" 一共 " +length+" 字 "); - toolState.add(label3); - textArea.addCaretListener(new CaretListener() { //记录行数和列数 - public void caretUpdate(CaretEvent e) { - //sum=0; - JTextArea editArea = (JTextArea)e.getSource(); - - try { - int caretpos = editArea.getCaretPosition(); - linenum = editArea.getLineOfOffset(caretpos); - columnnum = caretpos - textArea.getLineStartOffset(linenum); - linenum += 1; - label2.setText(" 第 " + linenum + " 行, 第 " + (columnnum+1)+" 列 "); - //sum+=columnnum+1; - //length+=sum; - length=NotepadMainFrame.this.textArea.getText().toString().length(); - label3.setText(" 一共 " +length+" 字 "); + /*==============================3=====================================*/ + /** + * 行为动作 + */ + public void actionPerformed(ActionEvent e) { + if(e.getSource()==itemOpen){ //打开 + openFile(); + }else if(e.getSource()==itemSave){ //保存 + //如果该文件是打开的,就可以直接保存 + save(); + }else if(e.getSource()==itemSaveAs){ //另存为 + saveAs(); + }else if(e.getSource()==itemNew){ //新建 + newFile(); + }else if(e.getSource()==itemExit){ //退出 + exit(); + }else if(e.getSource()==itemPage){ //页面设置 + ///页面设置,百度到的,不知道具体的用法 + PageFormat pf = new PageFormat(); + PrinterJob.getPrinterJob().pageDialog(pf); + }else if(e.getSource()==itemPrint){ //打印 + //打印机 + Print(); + }else if(e.getSource()==itemUndo || e.getSource()==popM_Undo){ //撤销 + if(undoMgr.canUndo()){ + undoMgr.undo(); } - catch(Exception ex) { } - }}); - - contentPane.add(toolState, BorderLayout.SOUTH); - toolState.setVisible(false); - toolState.setFloatable(false); - - // 创建弹出菜单 - final JPopupMenu jp=new JPopupMenu(); //创建弹出式菜单,下面三项是菜单项 - textArea.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent e) { - if(e.getButton()==MouseEvent.BUTTON3)//只响应鼠标右键单击事件 - { - jp.show(e.getComponent(),e.getX(),e.getY());//在鼠标位置显示弹出式菜单 + }else if(e.getSource()==itemRedo || e.getSource()==popM_Redo){ //恢复 + if(undoMgr.canRedo()){ + undoMgr.redo(); } - } - }); - - isChanged(); - - this.MainFrameWidowListener(); -} - - -/*===============================1====================================*/ -/** - * 是否有变化 - */ -private void isChanged() { - textArea.addKeyListener(new KeyAdapter() { - @Override - public void keyTyped(KeyEvent e) { - //在这里我进行了对使用快捷键,但是没有输入字符却没有改变textArea中内容的判断 - Character c=e.getKeyChar(); - if(c != null && !textArea.getText().toString().equals("")){ - flag=2; + }else if(e.getSource()==itemCut || e.getSource()==popM_Cut){ //剪切 + cut(); + }else if(e.getSource()==itemCopy || e.getSource()==popM_Copy){ //复制 + copy(); + }else if(e.getSource()==itemPaste || e.getSource()==popM_Paste){ //粘贴 + paste(); + }else if(e.getSource()==itemDelete || e.getSource()==popM_Delete){ //删除 + String tem=textArea.getText().toString(); + textArea.setText(tem.substring(0,textArea.getSelectionStart())); + }else if(e.getSource()==itemFind){ //查找 + mySearch(); + }else if(e.getSource()==itemFindNext){ //查找下一个 + mySearch(); + }else if(e.getSource()==itemReplace){ //替换 + mySearch(); + }else if(e.getSource()==itemTurnTo){ //转到 + turnTo(); + }else if(e.getSource()==itemSelectAll || e.getSource()==popM_SelectAll){ //选择全部 + textArea.selectAll(); + }else if(e.getSource()==itemTime){ //时间/日期 + textArea.append(hour+":"+min+" "+c.get(Calendar.YEAR)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DAY_OF_MONTH)); + }else if(e.getSource()==itemNextLine){ //设置自动换行 + //设置文本区的换行策略。如果设置为 true,则当行的长度大于所分配的宽度时,将换行。此属性默认为 false。 + if(itemNextLine.isSelected()){ + textArea.setLineWrap(true); + }else{ + textArea.setLineWrap(false); } - } - }); -} -/*===================================================================*/ - - -/*===============================2====================================*/ -/** - * 新建的或保存过的退出只有两个选择 - */ -private void MainFrameWidowListener() { - this.addWindowListener(new WindowAdapter(){ - @Override - public void windowClosing(WindowEvent e) { - if(flag==2 && currentPath==null){ - //这是弹出小窗口 - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - NotepadMainFrame.this.saveAs(); - }else if(result==JOptionPane.NO_OPTION){ - NotepadMainFrame.this.dispose(); - NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); - } - }else if(flag==2 && currentPath!=null){ - //这是弹出小窗口 - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - NotepadMainFrame.this.save(); - }else if(result==JOptionPane.NO_OPTION){ - NotepadMainFrame.this.dispose(); - NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); - } + }else if(e.getSource()==itemFont){ //设置字体大小 + + // 构造字体选择器,参数字体为预设值 + //MQFontChooser fontChooser = new MQFontChooser(textArea.getFont()); + //fontChooser.showFontDialog(this); + //Font font = fontChooser.getSelectFont(); + //将字体设置到JTextArea中 + //textArea.setFont(font); + } + + + + else if(e.getSource()==itemColor){ //设置背景颜色 + jcc1 = new JColorChooser(); + JOptionPane.showMessageDialog(this, jcc1,"选择背景颜色颜色",-1); + color = jcc1.getColor(); + textArea.setBackground(color); + }else if(e.getSource()==itemFontColor){ //设置字体颜色 + jcc1=new JColorChooser(); + JOptionPane.showMessageDialog(this, jcc1, "选择字体颜色", -1); + color = jcc1.getColor(); + //String string=textArea.getSelectedText(); + textArea.setForeground(color); + }else if(e.getSource()==itemStatement){ //设置状态 + if(itemStatement.isSelected()){ + //scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); + toolState.setVisible(true); }else{ - //这是弹出小窗口 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "确定关闭?", "系统提示", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - NotepadMainFrame.this.dispose(); - NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); - } + //scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + toolState.setVisible(false); } - } - }); -} -/*===================================================================*/ -/*==============================3=====================================*/ -/** - * 行为动作 - */ -public void actionPerformed(ActionEvent e) { - if(e.getSource()==itemOpen){ //打开 - openFile(); - }else if(e.getSource()==itemSave){ //保存 - //如果该文件是打开的,就可以直接保存 - save(); - }else if(e.getSource()==itemSaveAs){ //另存为 - saveAs(); - }else if(e.getSource()==itemNew){ //新建 - newFile(); - }else if(e.getSource()==itemExit){ //退出 - exit(); - }else if(e.getSource()==itemPage){ //页面设置 - ///页面设置,百度到的,不知道具体的用法 - PageFormat pf = new PageFormat(); - PrinterJob.getPrinterJob().pageDialog(pf); - }else if(e.getSource()==itemPrint){ //打印 - //打印机 - Print(); - }else if(e.getSource()==itemUndo || e.getSource()==popM_Undo){ //撤销 - if(undoMgr.canUndo()){ - undoMgr.undo(); - } - }else if(e.getSource()==itemRedo || e.getSource()==popM_Redo){ //恢复 - if(undoMgr.canRedo()){ - undoMgr.redo(); - } - }else if(e.getSource()==itemCut || e.getSource()==popM_Cut){ //剪切 - cut(); - }else if(e.getSource()==itemCopy || e.getSource()==popM_Copy){ //复制 - copy(); - }else if(e.getSource()==itemPaste || e.getSource()==popM_Paste){ //粘贴 - paste(); - }else if(e.getSource()==itemDelete || e.getSource()==popM_Delete){ //删除 - String tem=textArea.getText().toString(); - textArea.setText(tem.substring(0,textArea.getSelectionStart())); - }else if(e.getSource()==itemFind){ //查找 - mySearch(); - }else if(e.getSource()==itemFindNext){ //查找下一个 - mySearch(); - }else if(e.getSource()==itemReplace){ //替换 - mySearch(); - }else if(e.getSource()==itemTurnTo){ //转到 - turnTo(); - }else if(e.getSource()==itemSelectAll || e.getSource()==popM_SelectAll){ //选择全部 - textArea.selectAll(); - }else if(e.getSource()==itemTime){ //时间/日期 - textArea.append(hour+":"+min+" "+c.get(Calendar.YEAR)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DAY_OF_MONTH)); - }else if(e.getSource()==itemNextLine){ //设置自动换行 - //设置文本区的换行策略。如果设置为 true,则当行的长度大于所分配的宽度时,将换行。此属性默认为 false。 - if(itemNextLine.isSelected()){ - textArea.setLineWrap(true); - }else{ - textArea.setLineWrap(false); - } - }else if(e.getSource()==itemFont){ //设置字体大小 - // 构造字体选择器,参数字体为预设值 - // MQFontChooser fontChooser = new MQFontChooser(textArea.getFont()); - //fontChooser.showFontDialog(this); - // Font font = fontChooser.getSelectFont(); - // 将字体设置到JTextArea中 - //textArea.setFont(font); - }else if(e.getSource()==itemColor){ //设置背景颜色 - jcc1 = new JColorChooser(); - JOptionPane.showMessageDialog(this, jcc1,"选择背景颜色颜色",-1); - color = jcc1.getColor(); - textArea.setBackground(color); - }else if(e.getSource()==itemFontColor){ //设置字体颜色 - jcc1=new JColorChooser(); - JOptionPane.showMessageDialog(this, jcc1, "选择字体颜色", -1); - color = jcc1.getColor(); - //String string=textArea.getSelectedText(); - textArea.setForeground(color); - }else if(e.getSource()==itemStatement){ //设置状态 - if(itemStatement.isSelected()){ - //scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); - toolState.setVisible(true); - }else{ - //scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); - toolState.setVisible(false); - } - }else if(e.getSource()==itemSearchForHelp){ - JOptionPane.showMessageDialog(this, "HELLO","行动起来",1); - }else if(e.getSource()==itemAboutNotepad){ - JOptionPane.showMessageDialog(this, "哈哈","说明文档",1); - } -} -/*===================================================================*/ + }else if(e.getSource()==itemSearchForHelp){ + JOptionPane.showMessageDialog(this, "HELLO","行动起来",1); + }else if(e.getSource()==itemAboutNotepad){ + + class HDialog extends JDialog{ + public HDialog() { + this.setBounds(510,70, 577, 508); + this.setTitle("关于记事本"); + this.setResizable(false); + init(); + setVisible(true); + this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); + } + public void init() { + getContentPane().setLayout(null); + getContentPane().setLayout(null); + JSeparator separator = new JSeparator(); + separator.setBounds(30, 162, 511, 7); + getContentPane().add(separator); + JLabel version = new JLabel("Version:1.0"); + version.setForeground(Color.RED); + version.setFont(new Font("宋体", Font.PLAIN, 26)); + version.setBounds(133, 188, 172, 37); + getContentPane().add(version); + JLabel build = new JLabel("Build:2021-06-10"); + build.setForeground(Color.RED); + build.setFont(new Font("宋体", Font.PLAIN, 26)); + build.setBounds(133, 235, 218, 37); + getContentPane().add(build); + + JLabel author = new JLabel("20信管1班龚茵莎"); + author.setForeground(Color.DARK_GRAY); + author.setFont(new Font("宋体", Font.PLAIN, 26)); + author.setBounds(133, 276, 330, 54); + getContentPane().add(author); + + JLabel xueHao = new JLabel("202025710107"); + xueHao.setFont(new Font("宋体", Font.PLAIN, 26)); + xueHao.setBounds(133, 316, 238, 46); + getContentPane().add(xueHao); + + JLabel title = new JLabel("Notepad"); + title.setForeground(Color.BLACK); + title.setFont(new Font("宋体", Font.PLAIN, 35)); + title.setBounds(150, 36, 166, 92); + getContentPane().add(title); + } + } + HDialog h=new HDialog(); + } + } + /*===================================================================*/ -//转到指定行,弹出对话框 -private void turnTo() { - final JDialog gotoDialog = new JDialog(this, "转到下列行"); - JLabel gotoLabel = new JLabel("行数(L):"); - final JTextField linenum = new JTextField(5); - linenum.setText("1"); - linenum.selectAll(); + //转到指定行,弹出对话框 + private void turnTo() { + final JDialog gotoDialog = new JDialog(this, "转到下列行"); + JLabel gotoLabel = new JLabel("行数(L):"); + final JTextField linenum = new JTextField(5); + linenum.setText("1"); + linenum.selectAll(); - JButton okButton = new JButton("确定"); - okButton.addActionListener(new ActionListener() { + JButton okButton = new JButton("确定"); + okButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - int totalLine = textArea.getLineCount(); - int[] lineNumber = new int[totalLine + 1]; - String s = textArea.getText(); - int pos = 0, t = 0; + public void actionPerformed(ActionEvent e) { + int totalLine = textArea.getLineCount(); + int[] lineNumber = new int[totalLine + 1]; + String s = textArea.getText(); + int pos = 0, t = 0; - while (true) { - pos = s.indexOf('\12', pos); - // System.out.println("引索pos:"+pos); - if (pos == -1) - break; - lineNumber[t++] = pos++; - } + while (true) { + pos = s.indexOf('\12', pos); + // System.out.println("引索pos:"+pos); + if (pos == -1) + break; + lineNumber[t++] = pos++; + } - int gt = 1; - try { - gt = Integer.parseInt(linenum.getText());// 转换为基本数据类型int - } catch (NumberFormatException efe) { - JOptionPane.showMessageDialog(null, "请输入行数!", "提示", JOptionPane.WARNING_MESSAGE); - linenum.requestFocus(true); - return; - } + int gt = 1; + try { + gt = Integer.parseInt(linenum.getText());// 转换为基本数据类型int + } catch (NumberFormatException efe) { + JOptionPane.showMessageDialog(null, "请输入行数!", "提示", JOptionPane.WARNING_MESSAGE); + linenum.requestFocus(true); + return; + } - if (gt < 2 || gt >= totalLine) { - if (gt < 2) - textArea.setCaretPosition(0);//将滚动条一直固定在JTextArea的顶端 - else - textArea.setCaretPosition(s.length());//将滚动条滑到JTextArea的底端 - } else - textArea.setCaretPosition(lineNumber[gt - 2] + 1); + if (gt < 2 || gt >= totalLine) { + if (gt < 2) + textArea.setCaretPosition(0);//将滚动条一直固定在JTextArea的顶端 + else + textArea.setCaretPosition(s.length());//将滚动条滑到JTextArea的底端 + } else + textArea.setCaretPosition(lineNumber[gt - 2] + 1); - gotoDialog.dispose(); - } + gotoDialog.dispose(); + } - }); + }); - JButton cancelButton = new JButton("取消"); - cancelButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - gotoDialog.dispose(); - } - }); + JButton cancelButton = new JButton("取消"); + cancelButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + gotoDialog.dispose(); + } + }); - Container con = gotoDialog.getContentPane(); - con.setLayout(new FlowLayout()); - con.add(gotoLabel); - con.add(linenum); - con.add(okButton); - con.add(cancelButton); + Container con = gotoDialog.getContentPane(); + con.setLayout(new FlowLayout()); + con.add(gotoLabel); + con.add(linenum); + con.add(okButton); + con.add(cancelButton); - gotoDialog.setSize(200, 100); - gotoDialog.setResizable(false); - gotoDialog.setLocation(300, 280); - gotoDialog.setVisible(true); -} -/*===============================8====================================*/ -/** - * 推出按钮,和窗口的红叉实现一样的功能 - */ -private void exit() { - if(flag==2 && currentPath==null){ - //这是弹出小窗口 - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - NotepadMainFrame.this.saveAs(); - }else if(result==JOptionPane.NO_OPTION){ - NotepadMainFrame.this.dispose(); - NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); - } - }else if(flag==2 && currentPath!=null){ - //这是弹出小窗口 - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - NotepadMainFrame.this.save(); - }else if(result==JOptionPane.NO_OPTION){ - NotepadMainFrame.this.dispose(); - NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); - } - }else{ - //这是弹出小窗口 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "确定关闭?", "系统提示", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - NotepadMainFrame.this.dispose(); - NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + gotoDialog.setSize(200, 100); + gotoDialog.setResizable(false); + gotoDialog.setLocation(300, 280); + gotoDialog.setVisible(true); + } + + + /*===============================8====================================*/ + /** + * 推出按钮,和窗口的红叉实现一样的功能 + */ + private void exit() { + if(flag==2 && currentPath==null){ + //这是弹出小窗口 + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.saveAs(); + }else if(result==JOptionPane.NO_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + }else if(flag==2 && currentPath!=null){ + //这是弹出小窗口 + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.save(); + }else if(result==JOptionPane.NO_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + }else{ + //这是弹出小窗口 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "确定关闭?", "系统提示", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + NotepadMainFrame.this.dispose(); + NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } } } -} -/*===============================4====================================*/ -/** - * 新建文件,只有改过的和保存过的需要处理 - */ -private void newFile() { - if(flag==0 || flag==1){ //刚启动记事本为0,刚新建文档为1 - return; - }else if(flag==2 && this.currentPath==null){ //修改后 - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - this.saveAs(); //另存为 - }else if(result==JOptionPane.NO_OPTION){ + /*===================================================================*/ + + + /*===============================4====================================*/ + /** + * 新建文件,只有改过的和保存过的需要处理 + */ + private void newFile() { + if(flag==0 || flag==1){ //刚启动记事本为0,刚新建文档为1 + return; + }else if(flag==2 && this.currentPath==null){ //修改后 + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + this.saveAs(); //另存为 + }else if(result==JOptionPane.NO_OPTION){ + this.textArea.setText(""); + this.setTitle("无标题"); + flag=1; + } + return; + }else if(flag==2 && this.currentPath!=null ){ + //2、(保存的文件为3)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+this.currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + this.save(); //直接保存,有路径 + }else if(result==JOptionPane.NO_OPTION){ + this.textArea.setText(""); + this.setTitle("无标题"); + flag=1; + } + }else if(flag==3){ //保存的文件 this.textArea.setText(""); - this.setTitle("无标题"); flag=1; - } - return; - }else if(flag==2 && this.currentPath!=null ){ - //2、(保存的文件为3)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+this.currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - this.save(); //直接保存,有路径 - }else if(result==JOptionPane.NO_OPTION){ - this.textArea.setText(""); this.setTitle("无标题"); - flag=1; } - }else if(flag==3){ //保存的文件 - this.textArea.setText(""); - flag=1; - this.setTitle("无标题"); } -} -/*===================================================================*/ -/*===============================5====================================*/ -/** - * 另存为 - */ -private void saveAs() { - //打开保存框 - JFileChooser choose=new JFileChooser(); - //选择文件 - int result=choose.showSaveDialog(this); - if(result==JFileChooser.APPROVE_OPTION){ - //取得选择的文件[文件名是自己输入的] - File file=choose.getSelectedFile(); + /*===================================================================*/ + + + /*===============================5====================================*/ + /** + * 另存为 + */ + private void saveAs() { + //打开保存框 + JFileChooser choose=new JFileChooser(); + //选择文件 + int result=choose.showSaveDialog(this); + if(result==JFileChooser.APPROVE_OPTION){ + //取得选择的文件[文件名是自己输入的] + File file=choose.getSelectedFile(); + FileWriter fw=null; + //保存 + try { + fw=new FileWriter(file); + fw.write(textArea.getText()); + currentFileName=file.getName(); + currentPath=file.getAbsolutePath(); + //如果比较少,需要写 + fw.flush(); + this.flag=3; + this.setTitle(currentPath); + } catch (IOException e1) { + e1.printStackTrace(); //在命令行打印异常信息在程序中出错的位置及原因。 + }finally{ + try { + if(fw!=null) fw.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + } + } + /*===================================================================*/ + + + /*===============================6====================================*/ + /** + * 保存 + */ + private void save() { + if(this.currentPath==null){ + this.saveAs(); + if(this.currentPath==null){ + return; + } + } FileWriter fw=null; //保存 try { - fw=new FileWriter(file); + fw=new FileWriter(new File(currentPath)); fw.write(textArea.getText()); - currentFileName=file.getName(); - currentPath=file.getAbsolutePath(); //如果比较少,需要写 fw.flush(); - this.flag=3; - this.setTitle(currentPath); + flag=3; + this.setTitle(this.currentPath); } catch (IOException e1) { - e1.printStackTrace(); //在命令行打印异常信息在程序中出错的位置及原因。 + e1.printStackTrace(); }finally{ try { if(fw!=null) fw.close(); @@ -757,195 +935,188 @@ private void saveAs() { } } } -} -/*===================================================================*/ -/*===============================6====================================*/ -/** - * 保存 - */ -private void save() { - if(this.currentPath==null){ - this.saveAs(); - if(this.currentPath==null){ - return; - } - } - FileWriter fw=null; - //保存 - try { - fw=new FileWriter(new File(currentPath)); - fw.write(textArea.getText()); - //如果比较少,需要写 - fw.flush(); - flag=3; - this.setTitle(this.currentPath); - } catch (IOException e1) { - e1.printStackTrace(); - }finally{ - try { - if(fw!=null) fw.close(); - } catch (IOException e1) { - e1.printStackTrace(); - } - } -} -/*================================7===================================*/ -/** - * 打开文件 - */ -private void openFile() { - if(flag==2 && this.currentPath==null){ - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - this.saveAs(); //另存为 - } - }else if(flag==2 && this.currentPath!=null){ - //2、(打开的文件2,保存的文件3)条件下修改 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+this.currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); - if(result==JOptionPane.OK_OPTION){ - this.save(); //保存 - } - } - //打开文件选择框 - JFileChooser choose=new JFileChooser(); - //选择文件 - int result=choose.showOpenDialog(this); - if(result==JFileChooser.APPROVE_OPTION){ - //取得选择的文件 - File file=choose.getSelectedFile(); - //打开已存在的文件,提前将文件名存起来 - currentFileName=file.getName(); - //存在文件全路径 - currentPath=file.getAbsolutePath(); - flag=3; - this.setTitle(this.currentPath); - BufferedReader br=null; - try { - //建立文件流[字符流] - InputStreamReader isr=new InputStreamReader(new FileInputStream(file),"GBK"); - br=new BufferedReader(isr);//动态绑定 - //读取内容 - StringBuffer sb=new StringBuffer(); - String line=null; - while((line=br.readLine())!=null){ - sb.append(line+System.lineSeparator()); + /*===================================================================*/ + + + /*================================7===================================*/ + /** + * 打开文件 + */ + private void openFile() { + if(flag==2 && this.currentPath==null){ + //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + this.saveAs(); //另存为 } - //显示在文本框[多框] - textArea.setText(sb.toString()); - } catch (FileNotFoundException e1) { - e1.printStackTrace(); - } catch (IOException e1) { - e1.printStackTrace(); - } finally{ + }else if(flag==2 && this.currentPath!=null){ + //2、(打开的文件2,保存的文件3)条件下修改 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+this.currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + if(result==JOptionPane.OK_OPTION){ + this.save(); //保存 + } + } + //打开文件选择框 + JFileChooser choose=new JFileChooser(); + //选择文件 + int result=choose.showOpenDialog(this); + if(result==JFileChooser.APPROVE_OPTION){ + //取得选择的文件 + File file=choose.getSelectedFile(); + //打开已存在的文件,提前将文件名存起来 + currentFileName=file.getName(); + //存在文件全路径 + currentPath=file.getAbsolutePath(); + flag=3; + this.setTitle(this.currentPath); + BufferedReader br=null; try { - if(br!=null) br.close(); - } catch (Exception e1) { + //建立文件流[字符流] + InputStreamReader isr=new InputStreamReader(new FileInputStream(file),"GBK"); + br=new BufferedReader(isr);//动态绑定 + //读取内容 + StringBuffer sb=new StringBuffer(); + String line=null; + while((line=br.readLine())!=null){ + sb.append(line+System.lineSeparator()); //append追加字符串 + } + //显示在文本框[多框] + textArea.setText(sb.toString()); + } catch (FileNotFoundException e1) { + e1.printStackTrace(); + } catch (IOException e1) { e1.printStackTrace(); + } finally{ + try { + if(br!=null) br.close(); + } catch (Exception e1) { + e1.printStackTrace(); + } } } } -} -/*================================================================*/ + /*================================================================*/ + + /*=============================9===================================*/ + public void Print() + { + try{ + p = getToolkit().getPrintJob(this,"ok",null);//创建一个Printfjob 对象 p + g = p.getGraphics();//p 获取一个用于打印的 Graphics 的对象 + //g.translate(120,200);//改变组建的位置 + this.textArea.printAll(g); + p.end();//释放对象 g + } + catch(Exception a){ -/*=============================9===================================*/ -public void Print() -{ - try{ - p = getToolkit().getPrintJob(this,"ok",null);//创建一个Printfjob 对象 p - g = p.getGraphics();//p 获取一个用于打印的 Graphics 的对象 - //g.translate(120,200);//改变组建的位置 - this.textArea.printAll(g); - p.end();//释放对象 g + } } - catch(Exception a){ - - } -} -/*================================================================*/ - - -private static void addPopup(Component component, final JPopupMenu popup) { - component.addMouseListener(new MouseAdapter() { - public void mousePressed(MouseEvent e) { - if (e.isPopupTrigger()) { //判断用户是否要求弹出菜单 - showMenu(e); + /*================================================================*/ + + + private static void addPopup(Component component, final JPopupMenu popup) { + component.addMouseListener(new MouseAdapter() { + public void mousePressed(MouseEvent e) { + if (e.isPopupTrigger()) { //判断用户是否要求弹出菜单 + showMenu(e); + } } - } - public void mouseReleased(MouseEvent e) { - if (e.isPopupTrigger()) { - showMenu(e); + public void mouseReleased(MouseEvent e) { + if (e.isPopupTrigger()) { + showMenu(e); + } } - } - private void showMenu(MouseEvent e) { - popup.show(e.getComponent(), e.getX(), e.getY()); - } - }); -} - -public void cut(){ - copy(); - //标记开始位置 - int start = this.textArea.getSelectionStart(); - //标记结束位置 - int end = this.textArea.getSelectionEnd(); - //删除所选段 - this.textArea.replaceRange("", start, end); - -} - -public void copy(){ - //拖动选取文本 - String temp = this.textArea.getSelectedText(); - //把获取的内容复制到连续字符器,这个类继承了剪贴板接口 - StringSelection text = new StringSelection(temp); - //把内容放在剪贴板 - this.clipboard.setContents(text, null); -} - - public void paste(){ - //Transferable接口,把剪贴板的内容转换成数据 - Transferable contents = this.clipboard.getContents(this); - //DataFalvor类判断是否能把剪贴板的内容转换成所需数据类型 - DataFlavor flavor = DataFlavor.stringFlavor; - //如果可以转换 - if(contents.isDataFlavorSupported(flavor)){ - String str; - try {//开始转换 - str=(String)contents.getTransferData(flavor); - //如果要粘贴时,鼠标已经选中了一些字符 - if(this.textArea.getSelectedText()!=null){ - //定位被选中字符的开始位置 - int start = this.textArea.getSelectionStart(); - //定位被选中字符的末尾位置 - int end = this.textArea.getSelectionEnd(); - //把粘贴的内容替换成被选中的内容 - this.textArea.replaceRange(str, start, end); - }else{ - //获取鼠标所在TextArea的位置 - int mouse = this.textArea.getCaretPosition(); - //在鼠标所在的位置粘贴内容 - this.textArea.insert(str, mouse); + private void showMenu(MouseEvent e) { + popup.show(e.getComponent(), e.getX(), e.getY()); } - } catch(UnsupportedFlavorException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch(IllegalArgumentException e){ - e.printStackTrace(); + }); + } + + public void cut(){ + copy(); + //标记开始位置 + int start = this.textArea.getSelectionStart(); + //标记结束位置 + int end = this.textArea.getSelectionEnd(); + //删除所选段 + this.textArea.replaceRange("", start, end); + + } + + public void copy(){ + //拖动选取文本 + String temp = this.textArea.getSelectedText(); + //把获取的内容复制到连续字符器,这个类继承了剪贴板接口 + StringSelection text = new StringSelection(temp); + //把内容放在剪贴板 + this.clipboard.setContents(text, null); + } + + public void paste(){ + //Transferable接口,把剪贴板的内容转换成数据 + Transferable contents = this.clipboard.getContents(this); + //DataFalvor类判断是否能把剪贴板的内容转换成所需数据类型 + DataFlavor flavor = DataFlavor.stringFlavor; + //如果可以转换 + if(contents.isDataFlavorSupported(flavor)){ + String str; + try {//开始转换 + str=(String)contents.getTransferData(flavor); + //如果要粘贴时,鼠标已经选中了一些字符 + if(this.textArea.getSelectedText()!=null){ + //定位被选中字符的开始位置 + int start = this.textArea.getSelectionStart(); + //定位被选中字符的末尾位置 + int end = this.textArea.getSelectionEnd(); + //把粘贴的内容替换成被选中的内容 + this.textArea.replaceRange(str, start, end); + }else{ + //获取鼠标所在TextArea的位置 + int mouse = this.textArea.getCaretPosition(); + //在鼠标所在的位置粘贴内容 + this.textArea.insert(str, mouse); + } + } catch(UnsupportedFlavorException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch(IllegalArgumentException e){ + e.printStackTrace(); + } } } - } -/*===================================================================*/ - private void addPopup(JTextArea textArea2, JPopupMenu popupMenu2) { - // TODO 自动生成的方法存根 - - } + //设置查找与替换的对话框 + public void mySearch() { + final JDialog findDialog = new JDialog(this, "查找与替换", true); + Container con = findDialog.getContentPane(); + con.setLayout(new FlowLayout(FlowLayout.LEFT)); + JLabel searchContentLabel = new JLabel("查找内容(N) :"); + JLabel replaceContentLabel = new JLabel("替换为(P)  :"); + final JTextField findText = new JTextField(22); + final JTextField replaceText = new JTextField(22); + final JCheckBox matchcase = new JCheckBox("区分大小写"); + ButtonGroup bGroup = new ButtonGroup(); + final JRadioButton up = new JRadioButton("向上(U)"); + final JRadioButton down = new JRadioButton("向下(D)"); + down.setSelected(true); + bGroup.add(up); + bGroup.add(down); + JButton searchNext = new JButton("查找下一个(F)"); + JButton replace = new JButton("替换(R)"); + final JButton replaceAll = new JButton("全部替换(A)"); + searchNext.setPreferredSize(new Dimension(110, 22)); + replace.setPreferredSize(new Dimension(110, 22)); + replaceAll.setPreferredSize(new Dimension(110, 22)); + // "替换"按钮的事件处理 + replace.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()); + } + }); + }} \ No newline at end of file -- Gitee From f9c367184ed2d612d189a4df23a07992e745e8f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E8=BF=B9s?= <澧ㄨ抗s@LAPTOP-QGOA2U92> Date: Thu, 10 Jun 2021 23:40:11 +0800 Subject: [PATCH 13/15] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=E4=B8=8E=E6=9B=BF=E6=8D=A2=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0"=E6=9B=BF=E6=8D=A2=E5=85=A8=E9=83=A8"?= =?UTF-8?q?=E6=8C=89=E9=92=AE=E7=9A=84=E4=BA=8B=E4=BB=B6=E5=A4=84=E7=90=86?= =?UTF-8?q?=EF=BC=8C"=E6=9F=A5=E6=89=BE=E4=B8=8B=E4=B8=80=E4=B8=AA"?= =?UTF-8?q?=E6=8C=89=E9=92=AE=E4=BA=8B=E4=BB=B6=E5=A4=84=E7=90=86=EF=BC=8C?= =?UTF-8?q?=20"=E5=8F=96=E6=B6=88"=E6=8C=89=E9=92=AE=E5=8F=8A=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E5=A4=84=E7=90=86=EF=BC=8C=E4=BB=A5=E5=8F=8A"?= =?UTF-8?q?=E6=9F=A5=E6=89=BE=E4=B8=8E=E6=9B=BF=E6=8D=A2"=E5=AF=B9?= =?UTF-8?q?=E8=AF=9D=E6=A1=86=E7=9A=84=E7=95=8C=E9=9D=A2=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E6=B7=BB=E5=8A=A0=E4=BA=86=E8=AE=B0=E4=BA=8B?= =?UTF-8?q?=E6=9C=AC=E5=9B=BE=E6=A0=87=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- new.jpg | Bin 0 -> 31092 bytes src/java2020spring/NotepadMainFrame.java | 657 ++++++++++++++--------- 2 files changed, 406 insertions(+), 251 deletions(-) create mode 100644 new.jpg diff --git a/new.jpg b/new.jpg new file mode 100644 index 0000000000000000000000000000000000000000..668f23f63d94bf04e957b1a1f92cbf2362a5227d GIT binary patch literal 31092 zcmeEu^;?!(w>44%(%sS_-Hmi3`Ow`BqS7gdG}0m6-6BeNcY{bsNQ;1g)L9Sg{l4e> z?fV0c7hcz9%X8mr%{60;G3O#o^@S`N3NZ={3=EpQoRm5Y46F|HFA^g7Kdn|N-(X-U zVC1F5H9d^>GajelN;kbz8H*Fgl)}Wsf*&xo>KDgER?t!t`;e|ChT88uyAm1@PNRk^ zEk#3*FhJy1WSyM;>cx{M*h$L=SJT(KJTO8zO<%qLPM=)-T2Y*HZ#}B8`{UsLCJ~kw zo8r-bz6_BWzRH-h|L1$~ALAx3u|L2d|ML|v3U5>W8gbBtMhy1fub>`*|9%52h?Cth zNyN|<`=8Iipy6--_Z#G&N&$$9*4e1C|9Jo`w@y0TzfXgu$>w@Q%$yO(N&4?gVUWQh z(Eo1{P<#BJCH{4#|FgvZpCvGn0|yhC_>>eCnI31u2H3$Uz{pK=v#6;hNWpk>D=%d; z49+GbB(Qwsbri%4Bflb+3_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8>$v54E?= zt_!F@Ro?VWL0O2=*$X5gdR7!xRXy~e`TorHlK>gArW$8tK>QpzT@n_jm!Dh++StRN zE5pE6h^JihBi9!lWZeNvdoN%z5JC&TgYc@dJ=c_bw3{G*oIMT0>$?Y5E3-@Kj%yJ8VP5WwdGl+gFvgYtN<{(D0T1V>vmjTYyG6u~vnhPmyb z86{#&uu5jgr$4Jf^$QCNqO%45gP1uPWH-amrJxcEwzs!;JH1>>y1d{?=ltl6jg8#e zve1OP_75`HU3g*bQ)Du+U6@WEY-R)TH7di1#Kc-Kov;}3W)6seFk%>Tf{IHAa^wKARk3xL* z3j;|2N2X_FVzT4qsfq4tVY@whB%UZ7W|;1S-330{gwe6F=4XZ$CJxCEbQO4}K6sGe zZDU3ty1jEb$FNO`{7BA%(L~l`He5WiMJCsDf`_CovB8n41O-5gaQsibQa3YT!};sWluR_m zsFPZGe5>znQ71|JaXdsF>Dnfd0Un;vgLRkUyCr6kLaBPCQ6!NPaPXG}3M4y$lldnF zl1c--;^O5xJV{26mU0izDBSBgI%xa_cx|=-o1k3Mi)Bk8m!DTv1sG~*Xru(35HVK* zzbH`j5!cbNV5lW*AQ%rJDguphv~DF_F4r+yQbdU%|6>St=vh$W-jR@k#Q)Fm|Jgc_ gjQ`KY=75SIFyC3UdF=uGn)0!8S{F3S&f4GnFDv)i>Hq)$ literal 0 HcmV?d00001 diff --git a/src/java2020spring/NotepadMainFrame.java b/src/java2020spring/NotepadMainFrame.java index 467a489..f7d06b2 100644 --- a/src/java2020spring/NotepadMainFrame.java +++ b/src/java2020spring/NotepadMainFrame.java @@ -39,6 +39,8 @@ import java.util.Calendar; import java.util.GregorianCalendar; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; +import javax.swing.DefaultButtonModel; +import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; @@ -67,45 +69,41 @@ import javax.swing.border.EmptyBorder; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; import javax.swing.undo.UndoManager; -//各种UndoableEdit实例通过addEdit方法加入UndoManager,通过调用UndoManager的undo/redo方法来实现Undo/Redo功能。 +//鍚勭UndoableEdit瀹炰緥閫氳繃addEdit鏂规硶鍔犲叆UndoManager锛岄氳繃璋冪敤UndoManager鐨剈ndo/redo鏂规硶鏉ュ疄鐜癠ndo/Redo鍔熻兘銆 public class NotepadMainFrame extends JFrame implements ActionListener{ - /** - * 序列号 - */ - //private static final long serialVersionUID = 8585210209467333480L;//实现序列化接口的实体能够兼容先前版本,就需要显示的定义一个serialVersionUID private JPanel contentPane; private JTextArea textArea; private JMenuItem itemOpen; private JMenuItem itemSave; - - //1:新建 - //2:修改过 - //3:保存过的 + NotepadMainFrame frame=this; + //1锛氭柊寤 + //2锛氫慨鏀硅繃 + //3锛氫繚瀛樿繃鐨 int flag=0; - //当前文件名 + //褰撳墠鏂囦欢鍚 String currentFileName=null; - PrintJob p=null;//声明一个要打印的对象 - Graphics g=null;//要打印的对象 + PrintJob p=null;//澹版槑涓涓鎵撳嵃鐨勫璞 + Graphics g=null;//瑕佹墦鍗扮殑瀵硅薄 - //当前文件路径 + //褰撳墠鏂囦欢璺緞 String currentPath=null; - //背景颜色 + //鑳屾櫙棰滆壊 JColorChooser jcc1=null; Color color=Color.BLACK; - //文本的行数和列数 + //鏂囨湰鐨勮鏁板拰鍒楁暟 int linenum = 1; int columnnum = 1; - //撤销管理器 + //鎾ら攢绠$悊鍣 public UndoManager undoMgr = new UndoManager(); - //剪贴板 - public Clipboard clipboard = new Clipboard("系统剪切板"); + //鍓创鏉 + public Clipboard clipboard = new Clipboard("绯荤粺鍓垏鏉"); private JMenuItem itemSaveAs; private JMenuItem itemNew; @@ -148,25 +146,14 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ * Launch the application. */ public static void main(String[] args) { - //使事件派发线程上的可运行对象排队。当可运行对象排在事件派发队列的队首时,就调用其run方法。 - //其效果是允许事件派发线程调用另一个线程中的任意一个代码块,只有从事件派发线程才能更新组件。 - EventQueue.invokeLater(new Runnable() { - public void run() { - try { NotepadMainFrame frame = new NotepadMainFrame(); frame.setVisible(true); - } catch (Exception e) { - e.printStackTrace(); } - } - }); - } - - GregorianCalendar c=new GregorianCalendar(); //构造一个具有给定日期和时间设置的 GregorianCalendar + GregorianCalendar c=new GregorianCalendar(); //鏋勯犱竴涓叿鏈夌粰瀹氭棩鏈熷拰鏃堕棿璁剧疆鐨 GregorianCalendar int hour=c.get(Calendar.HOUR_OF_DAY); int min=c.get(Calendar.MINUTE); int second=c.get(Calendar.SECOND); - private JPopupMenu popupMenu;//弹出式菜单、右键菜单 + private JPopupMenu popupMenu;//寮瑰嚭寮忚彍鍗曘佸彸閿彍鍗 private JMenuItem popM_Undo; private JMenuItem popM_Cut; private JMenuItem popM_Copy; @@ -178,7 +165,7 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ private JMenuItem popM_closeIMe; private JMenuItem popM_InsertUnicode; private JMenuItem popM_RestartSelect; - private JSeparator separator_2; //添加一条分隔符 + private JSeparator separator_2; //娣诲姞涓鏉″垎闅旂 private JSeparator separator_3; private JSeparator separator_4; private JSeparator separator_5; @@ -195,7 +182,7 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ */ public NotepadMainFrame() { try { - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());//把图形界面外观设置成用户所使用的平台的外观 + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());//鎶婂浘褰㈢晫闈㈠瑙傝缃垚鐢ㄦ埛鎵浣跨敤鐨勫钩鍙扮殑澶栬 } catch (ClassNotFoundException e1) { e1.printStackTrace(); } catch (InstantiationException e1) { @@ -206,48 +193,49 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ e1.printStackTrace(); } setTitle("notepad"); - + Toolkit toolkit=Toolkit.getDefaultToolkit(); // 鑾峰彇Toolkit瀵硅薄 + Image icon = toolkit.getImage("new.jpg"); // 鑾峰彇鍥剧墖瀵硅薄 + frame.setIconImage(icon); // 璁剧疆鍥炬爣 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); setBounds(100, 100, 521, 572); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); - - - - //第一个菜单 - JMenu itemFile = new JMenu("文件(F)"); - itemFile.setMnemonic('F'); //设置键盘助记符 + + //绗竴涓彍鍗 + + JMenu itemFile = new JMenu("鏂囦欢(F)"); + itemFile.setMnemonic('F'); //璁剧疆閿洏鍔╄绗 menuBar.add(itemFile); - itemNew = new JMenuItem("新建(N)",'N'); + itemNew = new JMenuItem("鏂板缓(N)",'N'); itemNew.setAccelerator(KeyStroke.getKeyStroke("control N")); itemNew.addActionListener(this); itemFile.add(itemNew); - itemOpen = new JMenuItem("打开(O)",'O'); + itemOpen = new JMenuItem("鎵撳紑(O)",'O'); itemOpen.setAccelerator(KeyStroke.getKeyStroke("control O")); itemOpen.addActionListener(this); itemFile.add(itemOpen); - itemSave = new JMenuItem("保存(S)"); + itemSave = new JMenuItem("淇濆瓨(S)"); itemSave.setAccelerator(KeyStroke.getKeyStroke("control S")); itemSave.addActionListener(this); itemFile.add(itemSave); - itemSaveAs = new JMenuItem("另存为(A)"); + itemSaveAs = new JMenuItem("鍙﹀瓨涓(A)"); itemSaveAs.addActionListener(this); itemFile.add(itemSaveAs); separator = new JSeparator(); itemFile.add(separator); - itemPage = new JMenuItem("页面设置(U)",'U'); + itemPage = new JMenuItem("椤甸潰璁剧疆(U)",'U'); itemPage.addActionListener(this); itemFile.add(itemPage); - itemPrint = new JMenuItem("打印(P)...",'P'); + itemPrint = new JMenuItem("鎵撳嵃(P)...",'P'); itemPrint.setAccelerator(KeyStroke.getKeyStroke("control P")); itemPrint.addActionListener(this); itemFile.add(itemPrint); @@ -255,25 +243,25 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ separator_1 = new JSeparator(); itemFile.add(separator_1); - itemExit = new JMenuItem("退出(X)",'X'); + itemExit = new JMenuItem("閫鍑(X)",'X'); itemExit.addActionListener(this); itemFile.add(itemExit); - //第二个菜单 - itemEdit = new JMenu("编辑(E)"); + //绗簩涓彍鍗 + itemEdit = new JMenu("缂栬緫(E)"); itemEdit.setMnemonic('E'); menuBar.add(itemEdit); - itemUndo = new JMenuItem("撤销(U)",'U'); + itemUndo = new JMenuItem("鎾ら攢(U)",'U'); itemUndo.setAccelerator(KeyStroke.getKeyStroke("control Z")); itemUndo.addActionListener(this); itemEdit.add(itemUndo); - itemRedo = new JMenuItem("恢复(R)"); + itemRedo = new JMenuItem("鎭㈠(R)"); itemRedo.setAccelerator(KeyStroke.getKeyStroke("control R")); itemRedo.addActionListener(this); itemEdit.add(itemRedo); - itemCut = new JMenuItem("剪切(T)",'T'); + itemCut = new JMenuItem("鍓垏(T)",'T'); itemCut.setAccelerator(KeyStroke.getKeyStroke("control X")); itemCut.addActionListener(this); @@ -281,17 +269,17 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ itemEdit.add(separator_6); itemEdit.add(itemCut); - itemCopy = new JMenuItem("复制(C)",'C'); + itemCopy = new JMenuItem("澶嶅埗(C)",'C'); itemCopy.addActionListener(this); itemCopy.setAccelerator(KeyStroke.getKeyStroke("control C")); itemEdit.add(itemCopy); - itemPaste = new JMenuItem("粘贴(P)",'P'); + itemPaste = new JMenuItem("绮樿创(P)",'P'); itemPaste.addActionListener(this); itemPaste.setAccelerator(KeyStroke.getKeyStroke("control V")); itemEdit.add(itemPaste); - itemDelete = new JMenuItem("删除(L)",'L'); + itemDelete = new JMenuItem("鍒犻櫎(L)",'L'); itemDelete.addActionListener(this); itemDelete.setAccelerator(KeyStroke.getKeyStroke("control D")); itemEdit.add(itemDelete); @@ -299,95 +287,91 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ separator_7 = new JSeparator(); itemEdit.add(separator_7); - itemFind = new JMenuItem("查找(F)",'F'); + itemFind = new JMenuItem("鏌ユ壘(F)",'F'); itemFind.setAccelerator(KeyStroke.getKeyStroke("control F")); itemFind.addActionListener(this); itemEdit.add(itemFind); - itemFindNext = new JMenuItem("查找下一个(N)",'N'); + itemFindNext = new JMenuItem("鏌ユ壘涓嬩竴涓(N)",'N'); itemFindNext.setAccelerator(KeyStroke.getKeyStroke("F3")); itemFindNext.addActionListener(this); itemEdit.add(itemFindNext); - itemReplace = new JMenuItem("替换(R)",'R'); + itemReplace = new JMenuItem("鏇挎崲(R)",'R'); itemReplace.addActionListener(this); itemReplace.setAccelerator(KeyStroke.getKeyStroke("control H")); itemEdit.add(itemReplace); - itemTurnTo = new JMenuItem("转到(G)",'G'); + itemTurnTo = new JMenuItem("杞埌(G)",'G'); itemTurnTo.addActionListener(this); itemTurnTo.setAccelerator(KeyStroke.getKeyStroke("control G")); itemEdit.add(itemTurnTo); - itemSelectAll = new JMenuItem("全选(A)",'A'); + itemSelectAll = new JMenuItem("鍏ㄩ(A)",'A'); itemSelectAll.setAccelerator(KeyStroke.getKeyStroke("control A")); itemSelectAll.addActionListener(this); separator_8 = new JSeparator(); itemEdit.add(separator_8); itemEdit.add(itemSelectAll); - - - - - - itemTime = new JMenuItem("时间/日期(D)",'D'); + + itemTime = new JMenuItem("鏃堕棿/鏃ユ湡(D)",'D'); itemTime.addActionListener(this); itemTime.setAccelerator(KeyStroke.getKeyStroke("F5")); itemEdit.add(itemTime); - //第三个菜单 - itFormat = new JMenu("格式(O)"); + //绗笁涓彍鍗 + itFormat = new JMenu("鏍煎紡(O)"); itFormat.setMnemonic('O'); menuBar.add(itFormat); - itemNextLine = new JCheckBoxMenuItem("自动换行(W)"); + itemNextLine = new JCheckBoxMenuItem("鑷姩鎹㈣(W)"); itemNextLine.addActionListener(this); itFormat.add(itemNextLine); - itemFont = new JMenuItem("字体大小(F)..."); + itemFont = new JMenuItem("瀛椾綋澶у皬(F)..."); itemFont.addActionListener(this); itFormat.add(itemFont); - itemColor = new JMenuItem("背景颜色(C)..."); + itemColor = new JMenuItem("鑳屾櫙棰滆壊(C)..."); itemColor.addActionListener(this); itFormat.add(itemColor); - itemFontColor = new JMenuItem("字体颜色(I)..."); + itemFontColor = new JMenuItem("瀛椾綋棰滆壊(I)..."); itemFontColor.addActionListener(this); itFormat.add(itemFontColor); - //第四个菜单 - itemCheck = new JMenu("查看(V)"); + //绗洓涓彍鍗 + itemCheck = new JMenu("鏌ョ湅(V)"); itemCheck.setMnemonic('V'); menuBar.add(itemCheck); - itemStatement = new JCheckBoxMenuItem("状态栏(S)"); + itemStatement = new JCheckBoxMenuItem("鐘舵佹爮(S)"); itemStatement.addActionListener(this); itemCheck.add(itemStatement); - //第五个菜单 - itemHelp = new JMenu("帮助(H)"); + //绗簲涓彍鍗 + itemHelp = new JMenu("甯姪(H)"); itemHelp.setMnemonic('H'); menuBar.add(itemHelp); - itemSearchForHelp = new JMenuItem("查看帮助(H)",'H'); + itemSearchForHelp = new JMenuItem("鏌ョ湅甯姪(H)",'H'); itemSearchForHelp.addActionListener(this); itemHelp.add(itemSearchForHelp); - itemAboutNotepad = new JMenuItem("关于记事本(A)",'A'); + itemAboutNotepad = new JMenuItem("鍏充簬璁颁簨鏈(A)",'A'); itemAboutNotepad.addActionListener(this); itemHelp.add(itemAboutNotepad); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); - //设置边框布局 + //璁剧疆杈规甯冨眬 contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); textArea = new JTextArea(); - //设置滚动条 VERTICAL垂直 HORIZONTAL水平 + //璁剧疆婊氬姩鏉 VERTICAL鍨傜洿 HORIZONTAL姘村钩 scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); //contentPane2=new JPanel(); @@ -395,90 +379,90 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ //contentPane.add(contentPane2, BorderLayout.WEST); - //设置右键弹出的菜单 + //璁剧疆鍙抽敭寮瑰嚭鐨勮彍鍗 popupMenu = new JPopupMenu(); addPopup(textArea, popupMenu); - popM_Undo = new JMenuItem("撤销(U)"); + popM_Undo = new JMenuItem("鎾ら攢(U)"); popM_Undo.addActionListener(this); popupMenu.add(popM_Undo); - popM_Redo = new JMenuItem("恢复(R)"); + popM_Redo = new JMenuItem("鎭㈠(R)"); popM_Redo.addActionListener(this); popupMenu.add(popM_Redo); separator_2 = new JSeparator(); popupMenu.add(separator_2); - popM_Cut = new JMenuItem("剪切(T)"); + popM_Cut = new JMenuItem("鍓垏(T)"); popM_Cut.addActionListener(this); popupMenu.add(popM_Cut); - popM_Copy = new JMenuItem("复制(C)"); + popM_Copy = new JMenuItem("澶嶅埗(C)"); popM_Copy.addActionListener(this); popupMenu.add(popM_Copy); - popM_Paste = new JMenuItem("粘贴(P)"); + popM_Paste = new JMenuItem("绮樿创(P)"); popM_Paste.addActionListener(this); popupMenu.add(popM_Paste); - popM_Delete = new JMenuItem("删除(D)"); + popM_Delete = new JMenuItem("鍒犻櫎(D)"); popM_Delete.addActionListener(this); popupMenu.add(popM_Delete); separator_3 = new JSeparator(); popupMenu.add(separator_3); - popM_SelectAll = new JMenuItem("全选(A)"); + popM_SelectAll = new JMenuItem("鍏ㄩ(A)"); popM_SelectAll.addActionListener(this); popupMenu.add(popM_SelectAll); separator_4 = new JSeparator(); popupMenu.add(separator_4); - popM_toLeft = new JMenuItem("从右到左的阅读顺序(R)"); + popM_toLeft = new JMenuItem("浠庡彸鍒板乏鐨勯槄璇婚『搴(R)"); popM_toLeft.addActionListener(this); popupMenu.add(popM_toLeft); - popM_showUnicode = new JMenuItem("显示Unicode控制字符(S)"); + popM_showUnicode = new JMenuItem("鏄剧ずUnicode鎺у埗瀛楃(S)"); popM_showUnicode.addActionListener(this); popupMenu.add(popM_showUnicode); - popM_InsertUnicode = new JMenuItem("插入Unicode控制字符(I)"); + popM_InsertUnicode = new JMenuItem("鎻掑叆Unicode鎺у埗瀛楃(I)"); popM_InsertUnicode.addActionListener(this); popupMenu.add(popM_InsertUnicode); separator_5 = new JSeparator(); popupMenu.add(separator_5); - popM_closeIMe = new JMenuItem("关闭IME(L)"); + popM_closeIMe = new JMenuItem("鍏抽棴IME(L)"); popM_closeIMe.addActionListener(this); popupMenu.add(popM_closeIMe); - popM_RestartSelect = new JMenuItem("汉字重选(R)"); + popM_RestartSelect = new JMenuItem("姹夊瓧閲嶉(R)"); popM_RestartSelect.addActionListener(this); popupMenu.add(popM_RestartSelect); - //添加到面板中【中间】 + //娣诲姞鍒伴潰鏉夸腑銆愪腑闂淬 contentPane.add(scrollPane, BorderLayout.CENTER); - //添加撤销管理器 + //娣诲姞鎾ら攢绠$悊鍣 textArea.getDocument().addUndoableEditListener(undoMgr); - //创建一个工具栏 toolBar + //鍒涘缓涓涓伐鍏锋爮 toolBar toolState = new JToolBar(); toolState.setSize(textArea.getSize().width, 10);//toolState.setLayout(new FlowLayout(FlowLayout.LEFT)); - label1 = new JLabel(" 当前系统时间:" + hour + ":" + min + ":" + second+" "); + label1 = new JLabel(" 褰撳墠绯荤粺鏃堕棿锛" + hour + ":" + min + ":" + second+" "); toolState.add(label1); toolState.addSeparator(); - label2 = new JLabel(" 第 " + linenum + " 行, 第 " + columnnum+" 列 "); + label2 = new JLabel(" 绗 " + linenum + " 琛, 绗 " + columnnum+" 鍒 "); toolState.add(label2); toolState.addSeparator(); - label3 = new JLabel(" 一共 " +length+" 字 "); + label3 = new JLabel(" 涓鍏 " +length+" 瀛 "); toolState.add(label3); - textArea.addCaretListener(new CaretListener() { //记录行数和列数 + textArea.addCaretListener(new CaretListener() { //璁板綍琛屾暟鍜屽垪鏁 public void caretUpdate(CaretEvent e) { //sum=0; JTextArea editArea = (JTextArea)e.getSource(); @@ -488,11 +472,11 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ linenum = editArea.getLineOfOffset(caretpos); columnnum = caretpos - textArea.getLineStartOffset(linenum); linenum += 1; - label2.setText(" 第 " + linenum + " 行, 第 " + (columnnum+1)+" 列 "); + label2.setText(" 绗 " + linenum + " 琛, 绗 " + (columnnum+1)+" 鍒 "); //sum+=columnnum+1; //length+=sum; length=NotepadMainFrame.this.textArea.getText().toString().length(); - label3.setText(" 一共 " +length+" 字 "); + label3.setText(" 涓鍏 " +length+" 瀛 "); } catch(Exception ex) { } }}); @@ -504,14 +488,14 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ - // 创建弹出菜单 - final JPopupMenu jp=new JPopupMenu(); //创建弹出式菜单,下面三项是菜单项 + // 鍒涘缓寮瑰嚭鑿滃崟 + final JPopupMenu jp=new JPopupMenu(); //鍒涘缓寮瑰嚭寮忚彍鍗曪紝涓嬮潰涓夐」鏄彍鍗曢」 textArea.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { - if(e.getButton()==MouseEvent.BUTTON3)//只响应鼠标右键单击事件 + if(e.getButton()==MouseEvent.BUTTON3)//鍙搷搴旈紶鏍囧彸閿崟鍑讳簨浠 { - jp.show(e.getComponent(),e.getX(),e.getY());//在鼠标位置显示弹出式菜单 + jp.show(e.getComponent(),e.getX(),e.getY());//鍦ㄩ紶鏍囦綅缃樉绀哄脊鍑哄紡鑿滃崟 } } }); @@ -524,13 +508,13 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ /*===============================1====================================*/ /** - * 是否有变化 + * 鏄惁鏈夊彉鍖 */ private void isChanged() { textArea.addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { - //在这里我进行了对使用快捷键,但是没有输入字符却没有改变textArea中内容的判断 + //鍦ㄨ繖閲屾垜杩涜浜嗗浣跨敤蹇嵎閿紝浣嗘槸娌℃湁杈撳叆瀛楃鍗存病鏈夋敼鍙榯extArea涓唴瀹圭殑鍒ゆ柇 Character c=e.getKeyChar(); if(c != null && !textArea.getText().toString().equals("")){ flag=2; @@ -543,16 +527,16 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ /*===============================2====================================*/ /** - * 新建的或保存过的退出只有两个选择 + * 鏂板缓鐨勬垨淇濆瓨杩囩殑閫鍑哄彧鏈変袱涓夋嫨 */ private void MainFrameWidowListener() { this.addWindowListener(new WindowAdapter(){ @Override public void windowClosing(WindowEvent e) { if(flag==2 && currentPath==null){ - //这是弹出小窗口 - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + //杩欐槸寮瑰嚭灏忕獥鍙 + //1銆侊紙鍒氬惎鍔ㄨ浜嬫湰涓0锛屽垰鏂板缓鏂囨。涓1锛夋潯浠朵笅淇敼鍚 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "鏄惁灏嗘洿鏀逛繚瀛樺埌鏃犳爣棰?", "璁颁簨鏈", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if(result==JOptionPane.OK_OPTION){ NotepadMainFrame.this.saveAs(); }else if(result==JOptionPane.NO_OPTION){ @@ -560,9 +544,9 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); } }else if(flag==2 && currentPath!=null){ - //这是弹出小窗口 - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + //杩欐槸寮瑰嚭灏忕獥鍙 + //1銆侊紙鍒氬惎鍔ㄨ浜嬫湰涓0锛屽垰鏂板缓鏂囨。涓1锛夋潯浠朵笅淇敼鍚 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "鏄惁灏嗘洿鏀逛繚瀛樺埌"+currentPath+"?", "璁颁簨鏈", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if(result==JOptionPane.OK_OPTION){ NotepadMainFrame.this.save(); }else if(result==JOptionPane.NO_OPTION){ @@ -570,8 +554,8 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); } }else{ - //这是弹出小窗口 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "确定关闭?", "系统提示", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); + //杩欐槸寮瑰嚭灏忕獥鍙 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "纭畾鍏抽棴锛", "绯荤粺鎻愮ず", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); if(result==JOptionPane.OK_OPTION){ NotepadMainFrame.this.dispose(); NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); @@ -585,87 +569,87 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ /*==============================3=====================================*/ /** - * 行为动作 + * 琛屼负鍔ㄤ綔 */ public void actionPerformed(ActionEvent e) { - if(e.getSource()==itemOpen){ //打开 + if(e.getSource()==itemOpen){ //鎵撳紑 openFile(); - }else if(e.getSource()==itemSave){ //保存 - //如果该文件是打开的,就可以直接保存 + }else if(e.getSource()==itemSave){ //淇濆瓨 + //濡傛灉璇ユ枃浠舵槸鎵撳紑鐨勶紝灏卞彲浠ョ洿鎺ヤ繚瀛 save(); - }else if(e.getSource()==itemSaveAs){ //另存为 + }else if(e.getSource()==itemSaveAs){ //鍙﹀瓨涓 saveAs(); - }else if(e.getSource()==itemNew){ //新建 + }else if(e.getSource()==itemNew){ //鏂板缓 newFile(); - }else if(e.getSource()==itemExit){ //退出 + }else if(e.getSource()==itemExit){ //閫鍑 exit(); - }else if(e.getSource()==itemPage){ //页面设置 - ///页面设置,百度到的,不知道具体的用法 + }else if(e.getSource()==itemPage){ //椤甸潰璁剧疆 + ///椤甸潰璁剧疆锛岀櫨搴﹀埌鐨勶紝涓嶇煡閬撳叿浣撶殑鐢ㄦ硶 PageFormat pf = new PageFormat(); PrinterJob.getPrinterJob().pageDialog(pf); - }else if(e.getSource()==itemPrint){ //打印 - //打印机 + }else if(e.getSource()==itemPrint){ //鎵撳嵃 + //鎵撳嵃鏈 Print(); - }else if(e.getSource()==itemUndo || e.getSource()==popM_Undo){ //撤销 + }else if(e.getSource()==itemUndo || e.getSource()==popM_Undo){ //鎾ら攢 if(undoMgr.canUndo()){ undoMgr.undo(); } - }else if(e.getSource()==itemRedo || e.getSource()==popM_Redo){ //恢复 + }else if(e.getSource()==itemRedo || e.getSource()==popM_Redo){ //鎭㈠ if(undoMgr.canRedo()){ undoMgr.redo(); } - }else if(e.getSource()==itemCut || e.getSource()==popM_Cut){ //剪切 + }else if(e.getSource()==itemCut || e.getSource()==popM_Cut){ //鍓垏 cut(); - }else if(e.getSource()==itemCopy || e.getSource()==popM_Copy){ //复制 + }else if(e.getSource()==itemCopy || e.getSource()==popM_Copy){ //澶嶅埗 copy(); - }else if(e.getSource()==itemPaste || e.getSource()==popM_Paste){ //粘贴 + }else if(e.getSource()==itemPaste || e.getSource()==popM_Paste){ //绮樿创 paste(); - }else if(e.getSource()==itemDelete || e.getSource()==popM_Delete){ //删除 + }else if(e.getSource()==itemDelete || e.getSource()==popM_Delete){ //鍒犻櫎 String tem=textArea.getText().toString(); textArea.setText(tem.substring(0,textArea.getSelectionStart())); - }else if(e.getSource()==itemFind){ //查找 + }else if(e.getSource()==itemFind){ //鏌ユ壘 mySearch(); - }else if(e.getSource()==itemFindNext){ //查找下一个 + }else if(e.getSource()==itemFindNext){ //鏌ユ壘涓嬩竴涓 mySearch(); - }else if(e.getSource()==itemReplace){ //替换 + }else if(e.getSource()==itemReplace){ //鏇挎崲 mySearch(); - }else if(e.getSource()==itemTurnTo){ //转到 + }else if(e.getSource()==itemTurnTo){ //杞埌 turnTo(); - }else if(e.getSource()==itemSelectAll || e.getSource()==popM_SelectAll){ //选择全部 + }else if(e.getSource()==itemSelectAll || e.getSource()==popM_SelectAll){ //閫夋嫨鍏ㄩ儴 textArea.selectAll(); - }else if(e.getSource()==itemTime){ //时间/日期 + }else if(e.getSource()==itemTime){ //鏃堕棿/鏃ユ湡 textArea.append(hour+":"+min+" "+c.get(Calendar.YEAR)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DAY_OF_MONTH)); - }else if(e.getSource()==itemNextLine){ //设置自动换行 - //设置文本区的换行策略。如果设置为 true,则当行的长度大于所分配的宽度时,将换行。此属性默认为 false。 + }else if(e.getSource()==itemNextLine){ //璁剧疆鑷姩鎹㈣ + //璁剧疆鏂囨湰鍖虹殑鎹㈣绛栫暐銆傚鏋滆缃负 true锛屽垯褰撹鐨勯暱搴﹀ぇ浜庢墍鍒嗛厤鐨勫搴︽椂锛屽皢鎹㈣銆傛灞炴ч粯璁や负 false銆 if(itemNextLine.isSelected()){ textArea.setLineWrap(true); }else{ textArea.setLineWrap(false); } - }else if(e.getSource()==itemFont){ //设置字体大小 + }else if(e.getSource()==itemFont){ //璁剧疆瀛椾綋澶у皬 - // 构造字体选择器,参数字体为预设值 + // 鏋勯犲瓧浣撻夋嫨鍣紝鍙傛暟瀛椾綋涓洪璁惧 //MQFontChooser fontChooser = new MQFontChooser(textArea.getFont()); //fontChooser.showFontDialog(this); //Font font = fontChooser.getSelectFont(); - //将字体设置到JTextArea中 + //灏嗗瓧浣撹缃埌JTextArea涓 //textArea.setFont(font); } - else if(e.getSource()==itemColor){ //设置背景颜色 + else if(e.getSource()==itemColor){ //璁剧疆鑳屾櫙棰滆壊 jcc1 = new JColorChooser(); - JOptionPane.showMessageDialog(this, jcc1,"选择背景颜色颜色",-1); + JOptionPane.showMessageDialog(this, jcc1,"閫夋嫨鑳屾櫙棰滆壊棰滆壊",-1); color = jcc1.getColor(); textArea.setBackground(color); - }else if(e.getSource()==itemFontColor){ //设置字体颜色 + }else if(e.getSource()==itemFontColor){ //璁剧疆瀛椾綋棰滆壊 jcc1=new JColorChooser(); - JOptionPane.showMessageDialog(this, jcc1, "选择字体颜色", -1); + JOptionPane.showMessageDialog(this, jcc1, "閫夋嫨瀛椾綋棰滆壊", -1); color = jcc1.getColor(); //String string=textArea.getSelectedText(); textArea.setForeground(color); - }else if(e.getSource()==itemStatement){ //设置状态 + }else if(e.getSource()==itemStatement){ //璁剧疆鐘舵 if(itemStatement.isSelected()){ //scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); toolState.setVisible(true); @@ -674,13 +658,13 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ toolState.setVisible(false); } }else if(e.getSource()==itemSearchForHelp){ - JOptionPane.showMessageDialog(this, "HELLO","行动起来",1); + JOptionPane.showMessageDialog(this, "HELLO","琛屽姩璧锋潵",1); }else if(e.getSource()==itemAboutNotepad){ class HDialog extends JDialog{ public HDialog() { this.setBounds(510,70, 577, 508); - this.setTitle("关于记事本"); + this.setTitle("鍏充簬璁颁簨鏈"); this.setResizable(false); init(); setVisible(true); @@ -694,29 +678,29 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ getContentPane().add(separator); JLabel version = new JLabel("Version:1.0"); version.setForeground(Color.RED); - version.setFont(new Font("宋体", Font.PLAIN, 26)); + version.setFont(new Font("瀹嬩綋", Font.PLAIN, 26)); version.setBounds(133, 188, 172, 37); getContentPane().add(version); JLabel build = new JLabel("Build:2021-06-10"); build.setForeground(Color.RED); - build.setFont(new Font("宋体", Font.PLAIN, 26)); + build.setFont(new Font("瀹嬩綋", Font.PLAIN, 26)); build.setBounds(133, 235, 218, 37); getContentPane().add(build); - JLabel author = new JLabel("20信管1班龚茵莎"); + JLabel author = new JLabel("20淇$1鐝練鑼佃帋"); author.setForeground(Color.DARK_GRAY); - author.setFont(new Font("宋体", Font.PLAIN, 26)); + author.setFont(new Font("瀹嬩綋", Font.PLAIN, 26)); author.setBounds(133, 276, 330, 54); getContentPane().add(author); JLabel xueHao = new JLabel("202025710107"); - xueHao.setFont(new Font("宋体", Font.PLAIN, 26)); + xueHao.setFont(new Font("瀹嬩綋", Font.PLAIN, 26)); xueHao.setBounds(133, 316, 238, 46); getContentPane().add(xueHao); JLabel title = new JLabel("Notepad"); title.setForeground(Color.BLACK); - title.setFont(new Font("宋体", Font.PLAIN, 35)); + title.setFont(new Font("瀹嬩綋", Font.PLAIN, 35)); title.setBounds(150, 36, 166, 92); getContentPane().add(title); } @@ -726,15 +710,15 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ } /*===================================================================*/ - //转到指定行,弹出对话框 + //杞埌鎸囧畾琛岋紝寮瑰嚭瀵硅瘽妗 private void turnTo() { - final JDialog gotoDialog = new JDialog(this, "转到下列行"); - JLabel gotoLabel = new JLabel("行数(L):"); + final JDialog gotoDialog = new JDialog(this, "杞埌涓嬪垪琛"); + JLabel gotoLabel = new JLabel("琛屾暟(L):"); final JTextField linenum = new JTextField(5); linenum.setText("1"); linenum.selectAll(); - JButton okButton = new JButton("确定"); + JButton okButton = new JButton("纭畾"); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -745,7 +729,7 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ while (true) { pos = s.indexOf('\12', pos); - // System.out.println("引索pos:"+pos); + // System.out.println("寮曠储pos:"+pos); if (pos == -1) break; lineNumber[t++] = pos++; @@ -753,18 +737,18 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ int gt = 1; try { - gt = Integer.parseInt(linenum.getText());// 转换为基本数据类型int + gt = Integer.parseInt(linenum.getText());// 杞崲涓哄熀鏈暟鎹被鍨媔nt } catch (NumberFormatException efe) { - JOptionPane.showMessageDialog(null, "请输入行数!", "提示", JOptionPane.WARNING_MESSAGE); + JOptionPane.showMessageDialog(null, "璇疯緭鍏ヨ鏁!", "鎻愮ず", JOptionPane.WARNING_MESSAGE); linenum.requestFocus(true); return; } if (gt < 2 || gt >= totalLine) { if (gt < 2) - textArea.setCaretPosition(0);//将滚动条一直固定在JTextArea的顶端 + textArea.setCaretPosition(0);//灏嗘粴鍔ㄦ潯涓鐩村浐瀹氬湪JTextArea鐨勯《绔 else - textArea.setCaretPosition(s.length());//将滚动条滑到JTextArea的底端 + textArea.setCaretPosition(s.length());//灏嗘粴鍔ㄦ潯婊戝埌JTextArea鐨勫簳绔 } else textArea.setCaretPosition(lineNumber[gt - 2] + 1); @@ -773,7 +757,7 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ }); - JButton cancelButton = new JButton("取消"); + JButton cancelButton = new JButton("鍙栨秷"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { gotoDialog.dispose(); @@ -796,13 +780,13 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ /*===============================8====================================*/ /** - * 推出按钮,和窗口的红叉实现一样的功能 + * 閫鍑烘寜閽紝鍜岀獥鍙g殑绾㈠弶瀹炵幇涓鏍风殑鍔熻兘 */ private void exit() { if(flag==2 && currentPath==null){ - //这是弹出小窗口 - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + //杩欐槸寮瑰嚭灏忕獥鍙 + //1銆侊紙鍒氬惎鍔ㄨ浜嬫湰涓0锛屽垰鏂板缓鏂囨。涓1锛夋潯浠朵笅淇敼鍚 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "鏄惁灏嗘洿鏀逛繚瀛樺埌鏃犳爣棰?", "璁颁簨鏈", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if(result==JOptionPane.OK_OPTION){ NotepadMainFrame.this.saveAs(); }else if(result==JOptionPane.NO_OPTION){ @@ -810,9 +794,9 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); } }else if(flag==2 && currentPath!=null){ - //这是弹出小窗口 - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + //杩欐槸寮瑰嚭灏忕獥鍙 + //1銆侊紙鍒氬惎鍔ㄨ浜嬫湰涓0锛屽垰鏂板缓鏂囨。涓1锛夋潯浠朵笅淇敼鍚 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "鏄惁灏嗘洿鏀逛繚瀛樺埌"+currentPath+"?", "璁颁簨鏈", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if(result==JOptionPane.OK_OPTION){ NotepadMainFrame.this.save(); }else if(result==JOptionPane.NO_OPTION){ @@ -820,8 +804,8 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); } }else{ - //这是弹出小窗口 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "确定关闭?", "系统提示", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); + //杩欐槸寮瑰嚭灏忕獥鍙 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "纭畾鍏抽棴锛", "绯荤粺鎻愮ず", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); if(result==JOptionPane.OK_OPTION){ NotepadMainFrame.this.dispose(); NotepadMainFrame.this.setDefaultCloseOperation(EXIT_ON_CLOSE); @@ -833,36 +817,36 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ /*===============================4====================================*/ /** - * 新建文件,只有改过的和保存过的需要处理 + * 鏂板缓鏂囦欢锛屽彧鏈夋敼杩囩殑鍜屼繚瀛樿繃鐨勯渶瑕佸鐞 */ private void newFile() { - if(flag==0 || flag==1){ //刚启动记事本为0,刚新建文档为1 + if(flag==0 || flag==1){ //鍒氬惎鍔ㄨ浜嬫湰涓0锛屽垰鏂板缓鏂囨。涓1 return; - }else if(flag==2 && this.currentPath==null){ //修改后 - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + }else if(flag==2 && this.currentPath==null){ //淇敼鍚 + //1銆侊紙鍒氬惎鍔ㄨ浜嬫湰涓0锛屽垰鏂板缓鏂囨。涓1锛夋潯浠朵笅淇敼鍚 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "鏄惁灏嗘洿鏀逛繚瀛樺埌鏃犳爣棰?", "璁颁簨鏈", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if(result==JOptionPane.OK_OPTION){ - this.saveAs(); //另存为 + this.saveAs(); //鍙﹀瓨涓 }else if(result==JOptionPane.NO_OPTION){ this.textArea.setText(""); - this.setTitle("无标题"); + this.setTitle("鏃犳爣棰"); flag=1; } return; }else if(flag==2 && this.currentPath!=null ){ - //2、(保存的文件为3)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+this.currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + //2銆侊紙淇濆瓨鐨勬枃浠朵负3锛夋潯浠朵笅淇敼鍚 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "鏄惁灏嗘洿鏀逛繚瀛樺埌"+this.currentPath+"?", "璁颁簨鏈", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if(result==JOptionPane.OK_OPTION){ - this.save(); //直接保存,有路径 + this.save(); //鐩存帴淇濆瓨锛屾湁璺緞 }else if(result==JOptionPane.NO_OPTION){ this.textArea.setText(""); - this.setTitle("无标题"); + this.setTitle("鏃犳爣棰"); flag=1; } - }else if(flag==3){ //保存的文件 + }else if(flag==3){ //淇濆瓨鐨勬枃浠 this.textArea.setText(""); flag=1; - this.setTitle("无标题"); + this.setTitle("鏃犳爣棰"); } } /*===================================================================*/ @@ -870,29 +854,29 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ /*===============================5====================================*/ /** - * 另存为 + * 鍙﹀瓨涓 */ private void saveAs() { - //打开保存框 + //鎵撳紑淇濆瓨妗 JFileChooser choose=new JFileChooser(); - //选择文件 + //閫夋嫨鏂囦欢 int result=choose.showSaveDialog(this); if(result==JFileChooser.APPROVE_OPTION){ - //取得选择的文件[文件名是自己输入的] + //鍙栧緱閫夋嫨鐨勬枃浠禰鏂囦欢鍚嶆槸鑷繁杈撳叆鐨刔 File file=choose.getSelectedFile(); FileWriter fw=null; - //保存 + //淇濆瓨 try { fw=new FileWriter(file); fw.write(textArea.getText()); currentFileName=file.getName(); currentPath=file.getAbsolutePath(); - //如果比较少,需要写 + //濡傛灉姣旇緝灏戯紝闇瑕佸啓 fw.flush(); this.flag=3; this.setTitle(currentPath); } catch (IOException e1) { - e1.printStackTrace(); //在命令行打印异常信息在程序中出错的位置及原因。 + e1.printStackTrace(); //鍦ㄥ懡浠よ鎵撳嵃寮傚父淇℃伅鍦ㄧ▼搴忎腑鍑洪敊鐨勪綅缃強鍘熷洜銆 }finally{ try { if(fw!=null) fw.close(); @@ -907,7 +891,7 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ /*===============================6====================================*/ /** - * 保存 + * 淇濆瓨 */ private void save() { if(this.currentPath==null){ @@ -917,11 +901,11 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ } } FileWriter fw=null; - //保存 + //淇濆瓨 try { fw=new FileWriter(new File(currentPath)); fw.write(textArea.getText()); - //如果比较少,需要写 + //濡傛灉姣旇緝灏戯紝闇瑕佸啓 fw.flush(); flag=3; this.setTitle(this.currentPath); @@ -940,47 +924,47 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ /*================================7===================================*/ /** - * 打开文件 + * 鎵撳紑鏂囦欢 */ private void openFile() { if(flag==2 && this.currentPath==null){ - //1、(刚启动记事本为0,刚新建文档为1)条件下修改后 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + //1銆侊紙鍒氬惎鍔ㄨ浜嬫湰涓0锛屽垰鏂板缓鏂囨。涓1锛夋潯浠朵笅淇敼鍚 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "鏄惁灏嗘洿鏀逛繚瀛樺埌鏃犳爣棰?", "璁颁簨鏈", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if(result==JOptionPane.OK_OPTION){ - this.saveAs(); //另存为 + this.saveAs(); //鍙﹀瓨涓 } }else if(flag==2 && this.currentPath!=null){ - //2、(打开的文件2,保存的文件3)条件下修改 - int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "是否将更改保存到"+this.currentPath+"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); + //2銆侊紙鎵撳紑鐨勬枃浠2锛屼繚瀛樼殑鏂囦欢3锛夋潯浠朵笅淇敼 + int result=JOptionPane.showConfirmDialog(NotepadMainFrame.this, "鏄惁灏嗘洿鏀逛繚瀛樺埌"+this.currentPath+"?", "璁颁簨鏈", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if(result==JOptionPane.OK_OPTION){ - this.save(); //保存 + this.save(); //淇濆瓨 } } - //打开文件选择框 + //鎵撳紑鏂囦欢閫夋嫨妗 JFileChooser choose=new JFileChooser(); - //选择文件 + //閫夋嫨鏂囦欢 int result=choose.showOpenDialog(this); if(result==JFileChooser.APPROVE_OPTION){ - //取得选择的文件 + //鍙栧緱閫夋嫨鐨勬枃浠 File file=choose.getSelectedFile(); - //打开已存在的文件,提前将文件名存起来 + //鎵撳紑宸插瓨鍦ㄧ殑鏂囦欢锛屾彁鍓嶅皢鏂囦欢鍚嶅瓨璧锋潵 currentFileName=file.getName(); - //存在文件全路径 + //瀛樺湪鏂囦欢鍏ㄨ矾寰 currentPath=file.getAbsolutePath(); flag=3; this.setTitle(this.currentPath); BufferedReader br=null; try { - //建立文件流[字符流] + //寤虹珛鏂囦欢娴乕瀛楃娴乚 InputStreamReader isr=new InputStreamReader(new FileInputStream(file),"GBK"); - br=new BufferedReader(isr);//动态绑定 - //读取内容 + br=new BufferedReader(isr);//鍔ㄦ佺粦瀹 + //璇诲彇鍐呭 StringBuffer sb=new StringBuffer(); String line=null; while((line=br.readLine())!=null){ - sb.append(line+System.lineSeparator()); //append追加字符串 + sb.append(line+System.lineSeparator()); //append杩藉姞瀛楃涓 } - //显示在文本框[多框] + //鏄剧ず鍦ㄦ枃鏈[澶氭] textArea.setText(sb.toString()); } catch (FileNotFoundException e1) { e1.printStackTrace(); @@ -1002,11 +986,11 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ public void Print() { try{ - p = getToolkit().getPrintJob(this,"ok",null);//创建一个Printfjob 对象 p - g = p.getGraphics();//p 获取一个用于打印的 Graphics 的对象 - //g.translate(120,200);//改变组建的位置 + p = getToolkit().getPrintJob(this,"ok",null);//鍒涘缓涓涓狿rintfjob 瀵硅薄 p + g = p.getGraphics();//p 鑾峰彇涓涓敤浜庢墦鍗扮殑 Graphics 鐨勫璞 + //g.translate(120,200);//鏀瑰彉缁勫缓鐨勪綅缃 this.textArea.printAll(g); - p.end();//释放对象 g + p.end();//閲婃斁瀵硅薄 g } catch(Exception a){ @@ -1018,7 +1002,7 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ private static void addPopup(Component component, final JPopupMenu popup) { component.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { - if (e.isPopupTrigger()) { //判断用户是否要求弹出菜单 + if (e.isPopupTrigger()) { //鍒ゆ柇鐢ㄦ埛鏄惁瑕佹眰寮瑰嚭鑿滃崟 showMenu(e); } } @@ -1035,46 +1019,46 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ public void cut(){ copy(); - //标记开始位置 + //鏍囪寮濮嬩綅缃 int start = this.textArea.getSelectionStart(); - //标记结束位置 + //鏍囪缁撴潫浣嶇疆 int end = this.textArea.getSelectionEnd(); - //删除所选段 + //鍒犻櫎鎵閫夋 this.textArea.replaceRange("", start, end); } public void copy(){ - //拖动选取文本 + //鎷栧姩閫夊彇鏂囨湰 String temp = this.textArea.getSelectedText(); - //把获取的内容复制到连续字符器,这个类继承了剪贴板接口 + //鎶婅幏鍙栫殑鍐呭澶嶅埗鍒拌繛缁瓧绗﹀櫒锛岃繖涓被缁ф壙浜嗗壀璐存澘鎺ュ彛 StringSelection text = new StringSelection(temp); - //把内容放在剪贴板 + //鎶婂唴瀹规斁鍦ㄥ壀璐存澘 this.clipboard.setContents(text, null); } public void paste(){ - //Transferable接口,把剪贴板的内容转换成数据 + //Transferable鎺ュ彛锛屾妸鍓创鏉跨殑鍐呭杞崲鎴愭暟鎹 Transferable contents = this.clipboard.getContents(this); - //DataFalvor类判断是否能把剪贴板的内容转换成所需数据类型 + //DataFalvor绫诲垽鏂槸鍚﹁兘鎶婂壀璐存澘鐨勫唴瀹硅浆鎹㈡垚鎵闇鏁版嵁绫诲瀷 DataFlavor flavor = DataFlavor.stringFlavor; - //如果可以转换 + //濡傛灉鍙互杞崲 if(contents.isDataFlavorSupported(flavor)){ String str; - try {//开始转换 + try {//寮濮嬭浆鎹 str=(String)contents.getTransferData(flavor); - //如果要粘贴时,鼠标已经选中了一些字符 + //濡傛灉瑕佺矘璐存椂锛岄紶鏍囧凡缁忛変腑浜嗕竴浜涘瓧绗 if(this.textArea.getSelectedText()!=null){ - //定位被选中字符的开始位置 + //瀹氫綅琚変腑瀛楃鐨勫紑濮嬩綅缃 int start = this.textArea.getSelectionStart(); - //定位被选中字符的末尾位置 + //瀹氫綅琚変腑瀛楃鐨勬湯灏句綅缃 int end = this.textArea.getSelectionEnd(); - //把粘贴的内容替换成被选中的内容 + //鎶婄矘璐寸殑鍐呭鏇挎崲鎴愯閫変腑鐨勫唴瀹 this.textArea.replaceRange(str, start, end); }else{ - //获取鼠标所在TextArea的位置 + //鑾峰彇榧犳爣鎵鍦═extArea鐨勪綅缃 int mouse = this.textArea.getCaretPosition(); - //在鼠标所在的位置粘贴内容 + //鍦ㄩ紶鏍囨墍鍦ㄧ殑浣嶇疆绮樿创鍐呭 this.textArea.insert(str, mouse); } } catch(UnsupportedFlavorException e) { @@ -1087,29 +1071,29 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ } } - //设置查找与替换的对话框 + //璁剧疆鏌ユ壘涓庢浛鎹㈢殑瀵硅瘽妗 public void mySearch() { - final JDialog findDialog = new JDialog(this, "查找与替换", true); + final JDialog findDialog = new JDialog(this, "鏌ユ壘涓庢浛鎹", true); Container con = findDialog.getContentPane(); con.setLayout(new FlowLayout(FlowLayout.LEFT)); - JLabel searchContentLabel = new JLabel("查找内容(N) :"); - JLabel replaceContentLabel = new JLabel("替换为(P)  :"); + JLabel searchContentLabel = new JLabel("鏌ユ壘鍐呭(N) :"); + JLabel replaceContentLabel = new JLabel("鏇挎崲涓(P)銆 :"); final JTextField findText = new JTextField(22); final JTextField replaceText = new JTextField(22); - final JCheckBox matchcase = new JCheckBox("区分大小写"); + final JCheckBox matchcase = new JCheckBox("鍖哄垎澶у皬鍐"); ButtonGroup bGroup = new ButtonGroup(); - final JRadioButton up = new JRadioButton("向上(U)"); - final JRadioButton down = new JRadioButton("向下(D)"); + final JRadioButton up = new JRadioButton("鍚戜笂(U)"); + final JRadioButton down = new JRadioButton("鍚戜笅(D)"); down.setSelected(true); bGroup.add(up); bGroup.add(down); - JButton searchNext = new JButton("查找下一个(F)"); - JButton replace = new JButton("替换(R)"); - final JButton replaceAll = new JButton("全部替换(A)"); + JButton searchNext = new JButton("鏌ユ壘涓嬩竴涓(F)"); + JButton replace = new JButton("鏇挎崲(R)"); + final JButton replaceAll = new JButton("鍏ㄩ儴鏇挎崲(A)"); searchNext.setPreferredSize(new Dimension(110, 22)); replace.setPreferredSize(new Dimension(110, 22)); replaceAll.setPreferredSize(new Dimension(110, 22)); - // "替换"按钮的事件处理 + // "鏇挎崲"鎸夐挳鐨勪簨浠跺鐞 replace.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -1119,4 +1103,175 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ textArea.replaceSelection(replaceText.getText()); } }); - }} \ No newline at end of file + + +// "鏇挎崲鍏ㄩ儴"鎸夐挳鐨勪簨浠跺鐞 +replaceAll.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + textArea.setCaretPosition(0); // 灏嗗厜鏍囨斁鍒扮紪杈戝尯寮澶 + int a = 0, b = 0, replaceCount = 0; + + if (findText.getText().length() == 0) { + JOptionPane.showMessageDialog(findDialog, "璇峰~鍐欐煡鎵惧唴瀹!", "鎻愮ず", JOptionPane.WARNING_MESSAGE); + findText.requestFocus(true); + return; + } + while (a > -1) { + + int FindStartPos = textArea.getCaretPosition(); + String str1, str2, str3, str4, strA, strB; + str1 = textArea.getText(); + str2 = str1.toLowerCase(); + str3 = findText.getText(); + str4 = str3.toLowerCase(); + + if (matchcase.isSelected()) { + strA = str1; + strB = str3; + } else { + strA = str2; + strB = str4; + } + + if (up.isSelected()) { + if (textArea.getSelectedText() == null) { + a = strA.lastIndexOf(strB, FindStartPos - 1); + } else { + a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1); + } + } else if (down.isSelected()) { + if (textArea.getSelectedText() == null) { + a = strA.indexOf(strB, FindStartPos); + } else { + a = strA.indexOf(strB, FindStartPos - findText.getText().length() + 1); + } + + } + + if (a > -1) { + if (up.isSelected()) { + textArea.setCaretPosition(a); + b = findText.getText().length(); + textArea.select(a, a + b); + } else if (down.isSelected()) { + textArea.setCaretPosition(a); + b = findText.getText().length(); + textArea.select(a, a + b); + } + } else { + if (replaceCount == 0) { + JOptionPane.showMessageDialog(findDialog, "鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹!", "璁颁簨鏈", JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(findDialog, "鎴愬姛鏇挎崲" + 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++; + } + }// end while + } +}); /* "鏇挎崲鍏ㄩ儴"鎸夐挳鐨勪簨浠跺鐞嗙粨鏉 */ + +// "鏌ユ壘涓嬩竴涓"鎸夐挳浜嬩欢澶勭悊 +searchNext.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + int a = 0, b = 0; + int FindStartPos = textArea.getCaretPosition(); + String str1, str2, str3, str4, strA, strB; + str1 = textArea.getText(); + str2 = str1.toLowerCase(); //杩斿洖杞崲鍚庡瓧绗︾殑灏忓啓褰㈠紡 + str3 = findText.getText(); + str4 = str3.toLowerCase(); + // "鍖哄垎澶у皬鍐"鐨凜heckBox琚変腑 + if (matchcase.isSelected()) { + strA = str1; + strB = str3; + } else { + strA = str2; + strB = str4; + } + + if (up.isSelected()) { + if (textArea.getSelectedText() == null) { + a = strA.lastIndexOf(strB, FindStartPos - 1); + } else { + a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1); + } + } else if (down.isSelected()) { + if (textArea.getSelectedText() == null) { + a = strA.indexOf(strB, FindStartPos);//杩斿洖浠 FindStartPos 浣嶇疆寮濮嬫煡鎵炬寚瀹氬瓧绗﹀湪瀛楃涓蹭腑绗竴娆″嚭鐜板鐨勭储寮曪紝濡傛灉姝ゅ瓧绗︿覆涓病鏈夎繖鏍风殑瀛楃锛屽垯杩斿洖 -1銆 + } else { + a = strA.indexOf(strB, FindStartPos - findText.getText().length() + 1); + } + + } + if (a > -1) { + if (up.isSelected()) { + textArea.setCaretPosition(a); + b = findText.getText().length(); + textArea.select(a, a + b); + } else if (down.isSelected()) { + textArea.setCaretPosition(a); + b = findText.getText().length(); + textArea.select(a, a + b); + } + } else { + JOptionPane.showMessageDialog(null, "鎵句笉鍒版偍鏌ユ壘鐨勫唴瀹!", "璁颁簨鏈", JOptionPane.INFORMATION_MESSAGE); + } + + } + });/* "鏌ユ壘涓嬩竴涓"鎸夐挳浜嬩欢澶勭悊缁撴潫 */ + // "鍙栨秷"鎸夐挳鍙婁簨浠跺鐞 + JButton cancel = new JButton("鍙栨秷"); + cancel.setPreferredSize(new Dimension(110, 22)); + cancel.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + findDialog.dispose(); + } + }); + +// 璁捐"鏌ユ壘涓庢浛鎹"瀵硅瘽妗嗙殑鐣岄潰 + JPanel bottomPanel = new JPanel(); + JPanel centerPanel = new JPanel(); + JPanel topPanel = new JPanel(); + + JPanel direction = new JPanel(); + direction.setBorder(BorderFactory.createTitledBorder("鏂瑰悜 ")); + direction.add(up); + direction.add(down); + direction.setPreferredSize(new Dimension(170, 60)); + JPanel replacePanel = new JPanel(); + replacePanel.setLayout(new GridLayout(2, 1)); + replacePanel.add(replace); + replacePanel.add(replaceAll); + + topPanel.add(searchContentLabel); + topPanel.add(findText); + topPanel.add(searchNext); + centerPanel.add(replaceContentLabel); + centerPanel.add(replaceText); + centerPanel.add(replacePanel); + bottomPanel.add(matchcase); + bottomPanel.add(direction); + bottomPanel.add(cancel); + + con.add(topPanel); + con.add(centerPanel); + con.add(bottomPanel); + +// 璁剧疆"鏌ユ壘涓庢浛鎹"瀵硅瘽妗嗙殑澶у皬銆佸彲鏇存敼澶у皬(鍚)銆佷綅缃拰鍙鎬 + findDialog.setSize(410, 210); + findDialog.setResizable(false); + findDialog.setLocation(230, 280); + findDialog.setVisible(true); +} +} + + -- Gitee From 6f1217397cdf8107d794ca714f4687dc7168165a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E8=BF=B9s?= <澧ㄨ抗s@LAPTOP-QGOA2U92> Date: Fri, 11 Jun 2021 01:57:26 +0800 Subject: [PATCH 14/15] =?UTF-8?q?=E8=BF=99=E5=BA=94=E8=AF=A5=E6=98=AF?= =?UTF-8?q?=E6=9C=80=E5=90=8E=E7=89=88=E6=9C=AC=E4=BA=86=EF=BC=81=EF=BC=81?= =?UTF-8?q?=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/NotepadMainFrame.java | 65 ++++++++++++------------ 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/src/java2020spring/NotepadMainFrame.java b/src/java2020spring/NotepadMainFrame.java index f7d06b2..1f007f3 100644 --- a/src/java2020spring/NotepadMainFrame.java +++ b/src/java2020spring/NotepadMainFrame.java @@ -77,19 +77,16 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ private JMenuItem itemOpen; private JMenuItem itemSave; NotepadMainFrame frame=this; - //1锛氭柊寤 - //2锛氫慨鏀硅繃 - //3锛氫繚瀛樿繃鐨 - int flag=0; + + int flag=0; //flag=1锛氭柊寤 锛宖lag=2锛氫慨鏀硅繃锛宖lag=3锛氫繚瀛樿繃鐨 - //褰撳墠鏂囦欢鍚 - String currentFileName=null; + String currentFileName=null;//褰撳墠鏂囦欢鍚 PrintJob p=null;//澹版槑涓涓鎵撳嵃鐨勫璞 Graphics g=null;//瑕佹墦鍗扮殑瀵硅薄 - //褰撳墠鏂囦欢璺緞 - String currentPath=null; + + String currentPath=null;//褰撳墠鏂囦欢璺緞 //鑳屾櫙棰滆壊 JColorChooser jcc1=null; @@ -142,13 +139,24 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ int length=0; int sum=0; - /** - * Launch the application. - */ + + // Launch the application. + public static void main(String[] args) { - NotepadMainFrame frame = new NotepadMainFrame(); - frame.setVisible(true); - } + //浣夸簨浠舵淳鍙戠嚎绋嬩笂鐨勫彲杩愯瀵硅薄鎺掗槦銆傚綋鍙繍琛屽璞℃帓鍦ㄤ簨浠舵淳鍙戦槦鍒楃殑闃熼鏃讹紝灏辫皟鐢ㄥ叾run鏂规硶銆傚叾鏁堟灉鏄厑璁镐簨浠舵淳鍙戠嚎绋嬭皟鐢ㄥ彟涓涓嚎绋嬩腑鐨勪换鎰忎竴涓唬鐮佸潡銆 + //鍙湁浠庝簨浠舵淳鍙戠嚎绋嬫墠鑳芥洿鏂扮粍浠躲 + EventQueue.invokeLater(new Runnable() { + public void run() { + try { +NotepadMainFrame frame = new NotepadMainFrame(); +frame.setVisible(true); +} catch (Exception e) { +e.printStackTrace(); +} +} +}); +} + GregorianCalendar c=new GregorianCalendar(); //鏋勯犱竴涓叿鏈夌粰瀹氭棩鏈熷拰鏃堕棿璁剧疆鐨 GregorianCalendar int hour=c.get(Calendar.HOUR_OF_DAY); int min=c.get(Calendar.MINUTE); @@ -177,9 +185,8 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ - /** - * Create the frame. - */ + //Create the frame. + public NotepadMainFrame() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());//鎶婂浘褰㈢晫闈㈠瑙傝缃垚鐢ㄦ埛鎵浣跨敤鐨勫钩鍙扮殑澶栬 @@ -200,11 +207,9 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); setBounds(100, 100, 521, 572); JMenuBar menuBar = new JMenuBar(); - setJMenuBar(menuBar); - + setJMenuBar(menuBar); - //绗竴涓彍鍗 - + //绗竴涓彍鍗 JMenu itemFile = new JMenu("鏂囦欢(F)"); itemFile.setMnemonic('F'); //璁剧疆閿洏鍔╄绗 menuBar.add(itemFile); @@ -268,7 +273,7 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ separator_6 = new JSeparator(); itemEdit.add(separator_6); itemEdit.add(itemCut); - + itemCopy = new JMenuItem("澶嶅埗(C)",'C'); itemCopy.addActionListener(this); itemCopy.setAccelerator(KeyStroke.getKeyStroke("control C")); @@ -367,16 +372,12 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ //璁剧疆杈规甯冨眬 contentPane.setLayout(new BorderLayout(0, 0)); - setContentPane(contentPane); - + setContentPane(contentPane); textArea = new JTextArea(); //璁剧疆婊氬姩鏉 VERTICAL鍨傜洿 HORIZONTAL姘村钩 scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); - - //contentPane2=new JPanel(); - //contentPane2.setSize(10,textArea.getSize().height); - //contentPane.add(contentPane2, BorderLayout.WEST); + //璁剧疆鍙抽敭寮瑰嚭鐨勮彍鍗 @@ -443,11 +444,11 @@ public class NotepadMainFrame extends JFrame implements ActionListener{ popM_RestartSelect = new JMenuItem("姹夊瓧閲嶉(R)"); popM_RestartSelect.addActionListener(this); popupMenu.add(popM_RestartSelect); - //娣诲姞鍒伴潰鏉夸腑銆愪腑闂淬 - contentPane.add(scrollPane, BorderLayout.CENTER); + + contentPane.add(scrollPane, BorderLayout.CENTER); //娣诲姞鍒伴潰鏉夸腑蹇 + - //娣诲姞鎾ら攢绠$悊鍣 - textArea.getDocument().addUndoableEditListener(undoMgr); + textArea.getDocument().addUndoableEditListener(undoMgr);//娣诲姞鎾ら攢瀵硅薄 //鍒涘缓涓涓伐鍏锋爮 toolBar -- Gitee From 574cef918d36cbd083d9b695ebc770d8707bad68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E8=BF=B9s?= <澧ㄨ抗s@LAPTOP-QGOA2U92> Date: Fri, 11 Jun 2021 13:23:57 +0800 Subject: [PATCH 15/15] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E6=9C=80=E7=BB=88?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/NotepadMainFrame.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/java2020spring/NotepadMainFrame.java b/src/java2020spring/NotepadMainFrame.java index 1f007f3..aba684b 100644 --- a/src/java2020spring/NotepadMainFrame.java +++ b/src/java2020spring/NotepadMainFrame.java @@ -207,13 +207,12 @@ e.printStackTrace(); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); setBounds(100, 100, 521, 572); JMenuBar menuBar = new JMenuBar(); - setJMenuBar(menuBar); - + setJMenuBar(menuBar); //绗竴涓彍鍗 JMenu itemFile = new JMenu("鏂囦欢(F)"); itemFile.setMnemonic('F'); //璁剧疆閿洏鍔╄绗 menuBar.add(itemFile); - + itemNew = new JMenuItem("鏂板缓(N)",'N'); itemNew.setAccelerator(KeyStroke.getKeyStroke("control N")); itemNew.addActionListener(this); -- Gitee