diff --git a/new.jpg b/new.jpg new file mode 100644 index 0000000000000000000000000000000000000000..668f23f63d94bf04e957b1a1f92cbf2362a5227d Binary files /dev/null and b/new.jpg differ diff --git a/src/java2020spring/CalendarBean.java b/src/java2020spring/CalendarBean.java new file mode 100644 index 0000000000000000000000000000000000000000..2dec80b87cbc71d1af342ee79224e27a313aa33c --- /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 0000000000000000000000000000000000000000..c6836582b04968981e801ae74422bb819f73b764 --- /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/MainClass.java b/src/java2020spring/MainClass.java new file mode 100644 index 0000000000000000000000000000000000000000..05112c8c73c36b1321e1123ebd10491ffcb55063 --- /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/MyButton.java b/src/java2020spring/MyButton.java new file mode 100644 index 0000000000000000000000000000000000000000..7454281befc3b4104e03511140a28429aad984e8 --- /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/NotepadMainFrame.java b/src/java2020spring/NotepadMainFrame.java new file mode 100644 index 0000000000000000000000000000000000000000..aba684bd6f4a78080ee1cd1540fc0d3ad5242a6c --- /dev/null +++ b/src/java2020spring/NotepadMainFrame.java @@ -0,0 +1,1277 @@ +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.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; +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; +//鍚勭UndoableEdit瀹炰緥閫氳繃addEdit鏂规硶鍔犲叆UndoManager锛岄氳繃璋冪敤UndoManager鐨剈ndo/redo鏂规硶鏉ュ疄鐜癠ndo/Redo鍔熻兘銆 + +public class NotepadMainFrame extends JFrame implements ActionListener{ + private JPanel contentPane; + private JTextArea textArea; + private JMenuItem itemOpen; + private JMenuItem itemSave; + NotepadMainFrame frame=this; + + int flag=0; //flag=1锛氭柊寤 锛宖lag=2锛氫慨鏀硅繃锛宖lag=3锛氫繚瀛樿繃鐨 + + 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) { + //浣夸簨浠舵淳鍙戠嚎绋嬩笂鐨勫彲杩愯瀵硅薄鎺掗槦銆傚綋鍙繍琛屽璞℃帓鍦ㄤ簨浠舵淳鍙戦槦鍒楃殑闃熼鏃讹紝灏辫皟鐢ㄥ叾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); + 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("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'); //璁剧疆閿洏鍔╄绗 + menuBar.add(itemFile); + + itemNew = new JMenuItem("鏂板缓(N)",'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.addActionListener(this); + itemFile.add(itemOpen); + + itemSave = new JMenuItem("淇濆瓨(S)"); + itemSave.setAccelerator(KeyStroke.getKeyStroke("control S")); + 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("control P")); + 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("control Z")); + itemUndo.addActionListener(this); + itemEdit.add(itemUndo); + + itemRedo = new JMenuItem("鎭㈠(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.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("control C")); + itemEdit.add(itemCopy); + + itemPaste = new JMenuItem("绮樿创(P)",'P'); + itemPaste.addActionListener(this); + itemPaste.setAccelerator(KeyStroke.getKeyStroke("control V")); + itemEdit.add(itemPaste); + + itemDelete = new JMenuItem("鍒犻櫎(L)",'L'); + itemDelete.addActionListener(this); + 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.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("control H")); + itemEdit.add(itemReplace); + + itemTurnTo = new JMenuItem("杞埌(G)",'G'); + itemTurnTo.addActionListener(this); + itemTurnTo.setAccelerator(KeyStroke.getKeyStroke("control G")); + itemEdit.add(itemTurnTo); + + 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.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); + + + + //璁剧疆鍙抽敭寮瑰嚭鐨勮彍鍗 + 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(); + } + + + /*===============================1====================================*/ + /** + * 鏄惁鏈夊彉鍖 + */ + private void isChanged() { + textArea.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + //鍦ㄨ繖閲屾垜杩涜浜嗗浣跨敤蹇嵎閿紝浣嗘槸娌℃湁杈撳叆瀛楃鍗存病鏈夋敼鍙榯extArea涓唴瀹圭殑鍒ゆ柇 + 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){ + + 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(); + + 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());// 杞崲涓哄熀鏈暟鎹被鍨媔nt + } 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); + } + + + /*===============================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); + 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()); //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);//鍒涘缓涓涓狿rintfjob 瀵硅薄 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{ + //鑾峰彇榧犳爣鎵鍦═extArea鐨勪綅缃 + 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(); + // "鍖哄垎澶у皬鍐"鐨凜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); +} +} + + diff --git a/src/java2020spring/TodolistFrame.java b/src/java2020spring/TodolistFrame.java new file mode 100644 index 0000000000000000000000000000000000000000..8651d064481624b2e2994802c4ba3b2f241a73b6 --- /dev/null +++ b/src/java2020spring/TodolistFrame.java @@ -0,0 +1,46 @@ +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; + JMenu 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("日历视图"); + 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); + menu1.addSeparator(); + menu1.add(item2); + menubar.add(menu1); + menubar.add(menu2); + setJMenuBar(menubar); + } +}