Java记事本源代码.docx_第1页
Java记事本源代码.docx_第2页
Java记事本源代码.docx_第3页
Java记事本源代码.docx_第4页
Java记事本源代码.docx_第5页
已阅读5页,还剩62页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

import java.awt.*;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.Transferable;import java.awt.dnd.DnDConstants;import java.awt.dnd.DropTarget;import java.awt.dnd.DropTargetAdapter;import java.awt.dnd.DropTargetDropEvent;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.AdjustmentEvent;import java.awt.event.AdjustmentListener;import java.awt.event.InputEvent;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.event.MouseWheelEvent;import java.awt.event.MouseWheelListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import java.awt.print.PageFormat;import java.awt.print.Printable;import java.awt.print.PrinterException;import java.awt.print.PrinterJob;import java.io.*;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.Stack;import java.util.List;import javax.swing.*;import javax.swing.event.CaretEvent;import javax.swing.event.CaretListener;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.event.TreeSelectionEvent;import javax.swing.event.TreeSelectionListener;import javax.swing.text.BadLocationException;import javax.swing.text.Document;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.TreeSelectionModel;/文件-新建N CTRL+Nclass 新建 implements ActionListener private JTextArea editText;private boolean choose;public JFrame frame;public static String path1;/在打开的事件中path1=;public String str;新建() this.editText = JLinePrint.editText;path1=;NewMenu();/新建public void NewMenu()/建立询问框,设置大小位置,设为自己布局frame=new JFrame(记事本);frame.setBounds(424, 269, 355, 181);frame.setLayout(null);/建立两个标签JLabel label1=new JLabel(文件的内容已经改变);label1.setFont(new Font(新宋体,Font.PLAIN,14);JLabel label2=new JLabel(想保存文件吗?:);label2.setFont(new Font(新宋体,Font.PLAIN,14);label1.setBounds(113,23,180, 23);label2.setBounds(113,46,180,23);frame.add(label1);frame.add(label2);/建立是按钮JButton button_Yes=new JButton(是(Y);button_Yes.setMnemonic(KeyEvent.VK_Y);/按ALT+Y选择/给是按钮加上事件监听button_Yes.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e1)保存.path=;/保存类的路径清空保存.FirstSave=false;/保存类恢复未保存状态/与另存为相同choose=false;/条件未满足,不保存/弹框设定路径FileDialog filedialog=new FileDialog(frame,另存为,FileDialog.SAVE); filedialog.setVisible(true);path1=filedialog.getDirectory()+filedialog.getFile()+.txt;/如果为选择路径(文件名为null或nullnull),则直接返回 if(path1.equals(nullnull.txt)|path1.equals(filedialog.getDirectory()+null.txt) return; /if final File file=new File(path1); choose=true;/满足条件,设为能保存状态 if( file.exists()/如果文件重名 choose=false; /有条件不满足,重设为不能保存状态 final JFrame f=new JFrame(另存为); f.setBounds(435,276,path1.length()*7+200,125); f.setVisible(true); f.setLayout(null); JLabel label=new JLabel(path1+已存在。+要替换它吗?); label.setBounds(20,10,path1.length()*7+130,30); f.add(label); JButton button_Yes=new JButton(是(Y); button_Yes.setMnemonic(KeyEvent.VK_Y);/按ALT+Y选择 button_Yes.setBounds(43,58,100,30); button_Yes.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) try /选择是以后直接保存 /若有文件重名时候的文件输出流,输出后直接退出新建窗口 FileWriter out; out = new FileWriter(file); BufferedWriter out_ = new BufferedWriter(out); String temp=str.split(n); if (file.canRead()|file.getName()!=null) int i=-1; for(;+itemp.length-1;) out_.write(tempi); out_.newLine(); out_.write(tempi); out_.close(); /if editText.setText(null);/把文本框清空 /try catch (IOException e3) /catch f.dispose(); /actionperformed );/ActionListener f.add(button_Yes); JButton button_No=new JButton(否(N); button_No.setMnemonic(KeyEvent.VK_N);/按ALT+N选择 button_No.setBounds(143+path1.length()*4,58,100,30); button_No.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) f.dispose();editText.setText(str); ); f.add(button_No);/if/文件不重名时候的文件输出流if(choose)/如果前面的条件都满足则执行保存命令/文件输出流 try FileWriter out;out = new FileWriter(file); BufferedWriter out_ = new BufferedWriter(out); String temp=str.split(n); if (file.canRead()|file.getName()!=null) int i=-1; for(;+i打开O CTRL+Oclass 打开 implements ActionListener private FileDialog filedialog;private JLinePrint lineprint;打开(JFrame frame,JLinePrint lineprint) this.lineprint=lineprint;filedialog = new FileDialog(frame, 打开, FileDialog.LOAD);/打开 public void actionPerformed(ActionEvent e) Open();/actionPerformedpublic void Open()String regex=.txt;/正则表达式filedialog.setVisible(true);if(filedialog.getDirectory()=null)return;/按取消的时候String Filepath=filedialog.getDirectory(); /获取文件路径,最后面带String FileName=filedialog.getFile(); /获取文件名字(带.txt)String path=Filepath + FileName; /文件路径+文件名字FileName=FileName.replaceAll(regex, ); /获取文件名字(不带.txt)File file = new File(path);try FileReader in = new FileReader(file);BufferedReader in_ = new BufferedReader(in);String s = in_.readLine();String temp = s;s=in_.readLine();if (file.canRead() while (s != null) temp +=(rn+s); /存在一个String里面,到时候一次性赋给JTextAreas = in_.readLine(); /一行一行读取/whileJLinePrint.editText.setText(temp);in_.close();/读入即时存档文件for(int i=0;+i保存|另存为class 保存 implements ActionListener/囊括了保存和另存为private JTextArea editText;private FileDialog filedialog;public static String path;/path用于保存菜单项;在即时存档中检测有无保存;在新建中在选择过后path恢复为;在打开的时候直接获取path,同时控制在保存一次后不再弹出保存对话框private String path1;/;path1用于另存为菜单项public static boolean FirstSave;/用于在保存菜单项中判断是否为第一次保存;在新建中保存后FirstSave赋值为false;在打开类中打开文件后FirstSave赋值为trueprivate boolean choose;/文件重名时候的选择为true or falsepublic static String filepath;/filepath文件路径名,用于即时存档; 在打开中获取文件路径public static String fileName;/fileName文件名,用于即时存档;在打开中获取文件名保存(JFrame frame)this.editText=JLinePrint.editText;filedialog=new FileDialog(frame,保存,FileDialog.SAVE);path=;FirstSave=false;/保存public void actionPerformed(ActionEvent e)choose=false;if(JMenuItem)e.getSource().getActionCommand().equals(保存(S)Save();/if(保存(S)else if(JMenuItem)e.getSource().getActionCommand().equals(另存为(A).)Resave(); /另存为(A). /actionPerformedpublic void Save()if(path.equals()/如果文件路径为空则弹框获得文件路径filedialog.setVisible(true);filepath=filedialog.getDirectory();System.out.println(filepath);fileName=filedialog.getFile();System.out.println(fileName);path=filepath+fileName+.txt;/ifif(path.equals(nullnull.txt)|path.equals(filedialog.getDirectory()+null.txt)path=;return;/ifchoose=true;File file=new File(path);if(!FirstSave & file.exists()/如果不是第一次保存同时文件重名final JFrame f=new JFrame(另存为); f.setBounds(435,276,path.length()*7+200,125); f.setVisible(true); f.setLayout(null); JLabel label=new JLabel(path+已存在。+要替换它吗?); label.setBounds(20,10,path.length()*7+130,30); f.add(label); JButton button_Yes=new JButton(是(Y); button_Yes.setMnemonic(KeyEvent.VK_Y);/按ALT+Y选择 button_Yes.setBounds(43,58,100,30); button_Yes.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)choose=true;FirstSave=true;f.dispose(); );/ActionListener f.add(button_Yes); JButton button_No=new JButton(否(N); button_No.setMnemonic(KeyEvent.VK_N);/按ALT+N选择 button_No.setBounds(143+path.length()*4,58,100,30); button_No.addActionListener(new ActionListener() /添加事件,在确认是否覆盖的时候选择否,不保存,重置第一次存储,路径重置 public void actionPerformed(ActionEvent e) choose=false; FirstSave=false; path=; f.dispose(); ); f.add(button_No);/ifif(choose)/文件输出流try FirstSave=true;FileWriter out;out = new FileWriter(file); BufferedWriter out_ = new BufferedWriter(out); String s=editText.getText(); String temp=s.split(n); if (file.canRead()|file.getName()!=null) int i=-1; for(;+itemp.length-1;) out_.write(tempi); out_.newLine(); out_.write(tempi); out_.close(); /if /try catch (IOException e1) /catch/if/Savepublic void Resave()/另存为没有保存那么麻烦,保存存储一次后不再弹打开框,直接以原路径保存;另存为需要不断弹出打开框来进行保存。filedialog.setVisible(true);path1=filedialog.getDirectory()+filedialog.getFile()+.txt; if(path1.equals(nullnull.txt)|path1.equals(filedialog.getDirectory()+null.txt) return; /if File file=new File(path1); choose=true; if(!FirstSave & file.exists()/如果不是第一次保存同时文件重名 final JFrame f=new JFrame(另存为); f.setBounds(435,276,path1.length()*7+200,125); f.setVisible(true); f.setLayout(null); JLabel label=new JLabel(path1+已存在。+要替换它吗?); label.setBounds(20,10,path1.length()*7+130,30); f.add(label); JButton button_Yes=new JButton(是(Y); button_Yes.setMnemonic(KeyEvent.VK_Y);/按ALT+Y选择 button_Yes.setBounds(43,58,100,30); button_Yes.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) choose=true; f.dispose(); );/ActionListener f.add(button_Yes); JButton button_No=new JButton(否(N); button_No.setMnemonic(KeyEvent.VK_N);/按ALT+N选择 button_No.setBounds(143+path1.length()*4,58,100,30); button_No.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) choose=false; f.dispose(); ); f.add(button_No); /if if(choose) /文件输出流 try FirstSave=true; FileWriter out; out = new FileWriter(file); BufferedWriter out_ = new BufferedWriter(out); String s=editText.getText(); String temp=s.split(n); if (file.canRead()|file.getName()!=null) int i=-1; for(;+i页面设置class 页面设置 implements ActionListener/页面设置1private JFrame frame;页面设置()frame=new JFrame(页面设置);frame.setBounds(419,244,472,323); frame.setLayout(new GridLayout(5,1);JPanel Pane=new JPanel();/设置每一页打印的行数final JLabel LineEveryPage_set=new JLabel(每一页输出 +打印.LineEverypage+ 行);LineEveryPage_set.setForeground(Color.blue);LineEveryPage_set.setFont(new Font(华文行楷,Font.BOLD,24);final JTextField LineEverypage_set1=new JTextField(5);final JLabel LineEveryPage_status=new JLabel(默认);LineEverypage_set1.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)int s=Integer.parseInt(e.getActionCommand();if(s100)LineEverypage_set1.setText(null);LineEveryPage_status.setText(限制在1-100之间);return;打印.LineEverypage=s;LineEveryPage_set.setText(每一页输出 +打印.LineEverypage+ 行);if(打印.LineEverypage=80)LineEveryPage_status.setText(默认);/ifelseLineEveryPage_status.setText(已修改);/elseLineEverypage_set1.setText(null);/actionPerformed);/addActionListenerPane.add(LineEveryPage_set);Pane.add(LineEverypage_set1);Pane.add(LineEveryPage_status);Pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(修改打印时每一页打印的行数),BorderFactory.createEmptyBorder();frame.add(Pane);/设置x的坐标偏移量JPanel Pane1=new JPanel();final JLabel set_x=new JLabel(x坐标的偏移量 x=+Print.start_x);set_x.setFont(new Font(华文楷体,Font.BOLD,24);set_x.setForeground(Color.green);final JTextField set_x1=new JTextField(5);final JLabel set_x_status=new JLabel(默认);set_x1.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)double s=Double.parseDouble(e.getActionCommand();if(s100)set_x1.setText(null);set_x_status.setText(限制在0-100之间);return;Print.start_x=s;set_x.setText(x坐标的偏移量 x=+Print.start_x);if(Print.start_x=50.0)set_x_status.setText(默认);/ifelseset_x_status.setText(已修改);/elseset_x1.setText(null););Pane1.add(set_x);Pane1.add(set_x1);Pane1.add(set_x_status);Pane1.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(修改打印时起点坐标在x轴的偏移量),BorderFactory.createEmptyBorder();frame.add(Pane1);/设置y的坐标偏移量JPanel Pane2=new JPanel();final JLabel set_y=new JLabel(y坐标的偏移量 y=+Print.start_y);set_y.setFont(new Font(方正舒体,Font.BOLD,24);set_y.setForeground(Color.red);final JTextField set_y1=new JTextField(5);final JLabel set_y_status=new JLabel(默认);set_y1.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)double s=Double.parseDouble(e.getActionCommand();if(s100)s

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论