天天看點

JAVA SWING開發記事本 可插入圖檔 -java小應用

1.基于java se10 ,支援撤銷和反撤銷

2.可以插入圖檔,支援文字圖檔混合編輯,是一個小點子,是我的大二課程設計

package pack;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FileDialog;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.undo.UndoManager;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.SystemColor;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Notepad extends JFrame{
   
	
	//初始化
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;//容器聲明
	jiantin jian = new jiantin();//監聽聲明
	JMenuBar menuBar = new JMenuBar();
	JFileChooser jfc = new JFileChooser();//檔案選擇器
	JMenu file = new JMenu("\u6587\u4EF6(F)");
	  JMenuItem newfile = new JMenuItem("  \u65B0\u5EFA(N)");
	  JMenuItem openhun = new JMenuItem("  打開混合");
	  JMenuItem savehun = new JMenuItem("  儲存混合");
	  JMenuItem inpic = new JMenuItem("  插入圖檔(Y)");
	  JMenuItem open = new JMenuItem("  \u6253\u5F00(O)");
	  JMenuItem osave = new JMenuItem("  \u53E6\u5B58\u4E3A(A)...");
	  JMenuItem save = new JMenuItem("  \u4FDD\u5B58(S)");
	  JMenuItem end = new JMenuItem("  退出(X)");
	JMenu help = new JMenu("\u5E2E\u52A9(H)");
	  final JMenuItem money = new JMenuItem("  打賞作者");
	  final JMenuItem about = new JMenuItem("  \u5173\u4E8E");
	  final JMenuItem lookhelp = new JMenuItem("  \u67E5\u770B\u5E2E\u52A9");
	JMenu bianji = new JMenu("編輯(E)");
	  JMenuItem cexiao = new JMenuItem("  撤銷(Z)");
	  JMenuItem huifu = new JMenuItem("  恢複(R)");
	  JMenuItem jianqie = new JMenuItem("  剪切(X)");
	  JMenuItem copy = new JMenuItem("  複制(C)");
	  JMenuItem paste = new JMenuItem("  黏貼(P)");
	  JMenuItem del = new JMenuItem("  删除(L)");
	  JMenuItem chazhao = new JMenuItem("  查找(F)");
	  JMenuItem tihuan = new JMenuItem("  替換(R)");
	  JMenuItem allsel = new JMenuItem("  全選(A)");
	  JMenuItem time = new JMenuItem("  時間日期(D)");
	JMenu geshi = new JMenu("格式(U)");
	  JMenuItem ziti = new JMenuItem("  字型(P)");
	static JTextArea txt = new JTextArea();
	  int txtlength;//文本長度聲明
	 String sp;//檔案路徑
	 String iconpath;
	 JFrame th;
	 //注冊撤銷管理者
	 UndoManager um=new UndoManager();
	 String filePath;
	 FileInputStream fis;
	 byte[] content;
	 //多項輸入對話框
	 TDialog td;
	 ImageIcon icon1;
	 FileInputStream iconin;
	 byte[] conicon;
	 String iconstr;
	 //圖檔想x坐标,y坐标,文本長度,儲存到檔案
	 int iconx;
	 int icony;
	 byte[] changdu=new byte[3];
	
     

	 
	 
	 class FJF1 extends JFrame {
			private static final long serialVersionUID = 1L;
			private JPanel contentPane;
			private JComboBox daxiaobox;
			private JComboBox xingbox;
			private JComboBox fontbox;
		    String[] arr1={"初号","一号","小一","二号","小二","三号","小三","四号","小四","五号","小五","六号","小六"}; 
		    String[] arr2={"正常","傾斜","粗體","傾斜  粗體"}; 
		    String[] arr3={"微軟雅黑","宋體","楷體","華文行楷","黑體"}; 


			
			public FJF1() {
				setIconImage(Toolkit.getDefaultToolkit().getImage(".\\Penguins.jpg"));
				setFont(new Font("微軟雅黑", Font.PLAIN, 18));
				setTitle("\u5B57  \u4F53");
				//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				setBounds(400, 300, 333, 218);
				contentPane = new JPanel();
				contentPane.setToolTipText("\u4E0B\u62C9\u6846");
				contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
				setContentPane(contentPane);
				contentPane.setLayout(null);
				
				JLabel labelfont = new JLabel("\u5B57\u4F53\uFF1A");
				labelfont.setBounds(20, 10, 54, 15);
				contentPane.add(labelfont);
				
				JLabel fontxing = new JLabel("\u5B57\u5F62\uFF1A");
				fontxing.setBounds(131, 10, 54, 15);
				contentPane.add(fontxing);
				
				JLabel fontdaxiao = new JLabel("\u5927\u5C0F\uFF1A");
				fontdaxiao.setBounds(230, 10, 54, 15);
				contentPane.add(fontdaxiao);
				
				daxiaobox = new JComboBox();
				daxiaobox.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						int x=daxiaobox.getSelectedIndex();
						
						switch (x){
						case 0:   txt.setFont(new Font("微軟雅黑", Font.PLAIN, 50)); break;
						case 1:  txt.setFont(new Font("微軟雅黑", Font.PLAIN, 48)); break;
						case 2:   txt.setFont(new Font("微軟雅黑", Font.PLAIN, 46)); break;
						case 3:    txt.setFont(new Font("微軟雅黑", Font.PLAIN, 44)); break;
						case 4:    txt.setFont(new Font("微軟雅黑", Font.PLAIN, 42)); break;
						case 5:    txt.setFont(new Font("微軟雅黑", Font.PLAIN, 40)); break;
						case 6:    txt.setFont(new Font("微軟雅黑", Font.PLAIN, 38)); break;
						case 7:    txt.setFont(new Font("微軟雅黑", Font.PLAIN, 36)); break;
						case 8:    txt.setFont(new Font("微軟雅黑", Font.PLAIN, 34)); break;
						case 9:    txt.setFont(new Font("微軟雅黑", Font.PLAIN, 32)); break;
						case 10:    txt.setFont(new Font("微軟雅黑", Font.PLAIN, 28)); break;
						case 11:    txt.setFont(new Font("微軟雅黑", Font.PLAIN, 24)); break;
						case 12:    txt.setFont(new Font("微軟雅黑", Font.PLAIN, 18)); break;

						
						}
						
					}
				});
				daxiaobox.setToolTipText("\u4E0B\u62C9\u6846");
				daxiaobox.setEditable(true);
				daxiaobox.setBounds(230, 35, 85, 21);
				contentPane.add(daxiaobox);
				for(int i=0;i<arr1.length;i++){
				   daxiaobox.addItem(arr1[i]);
				    }
				xingbox = new JComboBox();
				xingbox.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
		                 int x=xingbox.getSelectedIndex();
						
						switch (x){
						case 0:   txt.setFont(new Font("微軟雅黑", Font.PLAIN, 12)); break;
						case 2:    txt.setFont(new Font("微軟雅黑", Font.BOLD , 12)); break;
						case 1:    txt.setFont(new Font("微軟雅黑", Font.ITALIC , 12)); break;
						case 3:     txt.setFont(new Font("微軟雅黑", Font.ITALIC +Font.BOLD , 12)); break;
						
						}
					}
				});
				xingbox.setEditable(true);
				xingbox.setBounds(131, 35, 79, 21);
				contentPane.add(xingbox);
				for(int i=0;i<arr2.length;i++){
					   xingbox.addItem(arr2[i]);
					    }
				
				fontbox = new JComboBox();
				fontbox.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
		             int x=fontbox.getSelectedIndex();
						
						switch (x){//字型名字需要修改
						case 0:   txt.setFont(new Font("微軟雅黑", Font.PLAIN, 12)); break;
						case 1:   txt.setFont(new Font(arr3[1], Font.PLAIN, 12)); break;
						case 2:   txt.setFont(new Font(arr3[2], Font.PLAIN, 12)); break;
						case 3:  txt.setFont(new Font(arr3[3], Font.PLAIN, 18)); break;
						case 4:    txt.setFont(new Font(arr3[4], Font.PLAIN, 12)); break;
						
						}
					}
				});
				fontbox.setEditable(true);
				fontbox.setBounds(20, 35, 85, 21);
				contentPane.add(fontbox);
				for(int i=0;i<arr3.length;i++){
					   fontbox.addItem(arr3[i]);
					    }
			}
		}
	
	//運作窗體,主方法
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Notepad frame = new Notepad();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	 
	public Notepad() {
		setTitle(" 記事本 - 版本 1.0");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(300,200,600,400);
		//pack();
		setJMenuBar(menuBar);
		
		
		file.setFont(new Font("黑體", Font.PLAIN, 13));
		file.setForeground(SystemColor.infoText);
		file.setMnemonic('F');
		file.setBackground(SystemColor.desktop);
		menuBar.add(file);
		
		bianji.setFont(new Font("黑體", Font.PLAIN, 13));
		bianji.setForeground(SystemColor.infoText);
		bianji.setMnemonic('X');
		bianji.setBackground(SystemColor.desktop);
		menuBar.add(bianji);
		
		geshi.setFont(new Font("黑體", Font.PLAIN, 13));
		geshi.setForeground(SystemColor.infoText);
		geshi.setMnemonic('U');
		geshi.setBackground(SystemColor.desktop);
		menuBar.add(geshi);
		
		ziti.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		ziti.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK));
		geshi.add(ziti);
		ziti.addActionListener(jian);
		
		//BIANJI按鈕
		cexiao.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		cexiao.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK));
		bianji.add(cexiao);
		cexiao.addActionListener(jian);
		
		huifu.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		huifu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK));
		bianji.add(huifu);
		huifu.addActionListener(jian);
		
		jianqie.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		jianqie.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
		bianji.add(jianqie);
		jianqie.addActionListener(jian);
		
		copy.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
		bianji.add(copy);
		copy.addActionListener(jian);
		
		paste.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
		bianji.add(paste);
		paste.addActionListener(jian);
		
		del.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		del.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_DELETE));
		bianji.add(del);
		del.addActionListener(jian);
		
		chazhao.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		chazhao.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK));
		bianji.add(chazhao);
		chazhao.addActionListener(jian);
		
		tihuan.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		tihuan.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK));
		bianji.add(tihuan);
		tihuan.addActionListener(jian);
		
		allsel.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		allsel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));
		bianji.add(allsel);
		allsel.addActionListener(jian);
		
		time.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		time.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_MASK));
		bianji.add(time);
		time.addActionListener(jian);
		//BIANJI按鈕
		
		newfile.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		newfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
		file.add(newfile);
		newfile.addActionListener(jian);
		
		inpic.setFont(new Font("微軟雅黑", Font.PLAIN, 13));//插入圖檔
		inpic.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK));
		file.add(inpic);
		inpic.addActionListener(jian);
		
		open.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
		file.add(open);
		open.addActionListener(jian);
		
		openhun.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		openhun.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK));
		file.add(openhun);
		openhun.addActionListener(jian);
		
		savehun.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		savehun.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_MASK));
		file.add(savehun);
		savehun.addActionListener(jian);
		
		save.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
		file.add(save);
		save.addActionListener(jian);
		
		osave.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		file.add(osave);
		osave.addActionListener(jian);
		
		end.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		file.add(end);
		end.addActionListener(jian);
		
		help.setFont(new Font("微軟雅黑", Font.PLAIN, 12));
		help.setForeground(SystemColor.infoText);
		menuBar.add(help);
		
		lookhelp.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		help.add(lookhelp);
		lookhelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(lookhelp, "哈哈,沒有幫助哦!", "幫助", JOptionPane.INFORMATION_MESSAGE);


				
			}
		});
		
		money.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		help.add(money);
		money.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				picture p =new picture();
				//p.setAlwaysOnTop(true);
				p.setVisible(true);

				
			}
		});
		
		
		about.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(about, "版本 1.0\n作者  韓哥哥", "關于", JOptionPane.INFORMATION_MESSAGE);


				
			}
		});
		about.setFont(new Font("微軟雅黑", Font.PLAIN, 13));
		help.add(about);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		
		txt.setRows(txtlength);
		txt.getDocument().addUndoableEditListener(um);
		txt.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e6) {
				char onekey= e6.getKeyChar();
				txt.setLineWrap(true);        //激活自動換行功能
				txt.setWrapStyleWord(true);//不斷字
				if(onekey=='\n') {
					//按下回車實作什麼操作
					um.undo();
					txt.append("\r\n");
					
					
				}
			}
		});
		contentPane.add(txt, BorderLayout.NORTH);
		
		JScrollPane jspane=new JScrollPane(txt);
		contentPane.add(jspane);
		
	}//初始化結束
	
public void showicon(byte[] b,int x,int y) {
	      ImageIcon	icon1=new ImageIcon(b);
            int iconhigh=icon1.getIconHeight();
            int iconweidth=icon1.getIconWidth();
            JLabel iconlable=new JLabel("");
//            int x=txt.getCaretPosition();//guangbiao
//            int y=txt.getLineCount();
//            iconx=x*8;
//            icony=y*16;
            iconlable.setBounds(x, y, iconweidth, iconhigh);
            iconlable.setIcon(icon1);
                if(iconweidth<600&&iconhigh<400){
	                 setBounds(300,200,600, 400);
                 }else{
	                 setBounds(300,200,iconweidth, iconhigh);
                     }
                txt.add(iconlable);
               }

//數組複制方法
public byte[] copy(byte[] a,byte[] b)
 {
	byte[] data1=a;
	byte[] data2=b;
	byte[] data3 = new byte[data1.length+data2.length];
	System.arraycopy(data1,0,data3,0,data1.length);
	System.arraycopy(data2,0,data3,data1.length,data2.length);
	return data3;
 }
public byte[] add(byte[] a)
 {  int ad=txt.getText().length();
     byte ad1=(byte)ad;
     changdu[0]=ad1;
     
     int ad2=iconx;
    byte ad21=(byte)(ad2);
     changdu[1]=ad21;
     
     int ad3=icony;
     byte ad31=(byte)(ad3);
      changdu[2]=ad31;
     
     
     
     
	byte[] d=copy(changdu,a);
	
	
	return d;
 }
public int[] cutarr(byte[] a)//得到資訊3位元組
  { 
	byte[] ad=new byte[3];
	System.arraycopy(a,0,ad,0,3);
	int t=ad[0]>0?ad[0]:ad[0]+256;
	int t1=ad[1]>0?ad[1]:ad[1]+256;
	int t2=ad[2]>0?ad[2]:ad[2]+256;
	int[] f={t,t1,t2};
	return f;
  }
public byte[] noone(byte[] a)
 {  
	byte[] b=new byte[a.length-3];
	System.arraycopy(a,3,b,0,a.length-3);
	return b;
 }



class jiantin implements ActionListener{
	FileDialog openFile = new FileDialog(th, "打開檔案",FileDialog.LOAD);
   
	@SuppressWarnings("deprecation")
	public void  actionPerformed(ActionEvent e) {
		

		if(e.getSource()==open) {
			openFile.setVisible(true);
			// 擷取檔案路徑
			filePath = openFile.getDirectory() + openFile.getFile();
		try {
			fis = new FileInputStream(filePath);
			File fname=new File(filePath);
			content = new byte[fis.available()];
			fis.read(content);		
			
			//圖檔判斷
			//String si=new String(content);
			//int kai=si.indexOf(iconstart);
			//kai=kai+3;
			//int end=si.indexOf(iconend);
           // String  iconstrin=si.substring(kai, end);	
            
            //byte[] image = new byte[content.length-kai-3];
            
           //System.arraycopy(content, kai, image, 0, 44136);//+1
            //System.out.println(kai+"   "+end);
			//String txttxt=si.substring(0, kai-3);
			//byte[] iconarr=iconstrin.getBytes();
			//iconarr.decode(iconstrin);
//			for(int i=0;i<iconarr.length;i++)
//			    System.out.print(iconarr[i]);
			
			//圖檔打開
//			ImageIcon icon1=new ImageIcon(image);
//			int iconhigh=icon1.getIconHeight();
//			int iconweidth=icon1.getIconWidth();
//			JLabel iconlable=new JLabel("");
//			iconlable.setBounds(20, 20, iconweidth, iconhigh);
//			iconlable.setIcon(icon1);
//			if(iconweidth<600&&iconhigh<400){
//				setBounds(300,200,600, 400);
//				System.out.println(" 顯示圖檔  "+iconweidth+"    "+iconhigh);
//				System.out.println(" 圖檔數組  "+image.length);
//			}else{
//				setBounds(300,200,iconweidth, iconhigh);
//			}
			
			
			//txt.add(iconlable);
			
			txt.setText(new String(content));
			//圖檔判斷結尾
			fis.close();
			setTitle(fname.getName()+" - 記事本");
		    }
		catch(IOException e3)
		    {
			 System.out.println("輸入輸出異常");
			}
	                             }
		else if(e.getSource()==newfile) {
			 setTitle("建立文本文檔 - 記事本");
		     txt.setText("");
		     }
		else if(e.getSource()==osave) {
		     FileDialog saveFile = new FileDialog(th, "儲存檔案",FileDialog.SAVE);
			 saveFile.setVisible(true);
			 sp = saveFile.getDirectory() + saveFile.getFile();
		try    {//String si=null;
				FileWriter fos = new FileWriter(sp);
				//if(iconx!=0&&icony!=0){
					
				//	si=iconstart+iconstr+iconend;
				//}
				
				
				
				fos.write(txt.getText());//iconstr211  如果加上圖檔
				fos.close();
				}
		catch (IOException ex) {
				ex.printStackTrace();
				}
		                              }
		else if(e.getSource()==save) {
					
					String pathdir=openFile.getDirectory();
					String pathfile=openFile.getFile();
					String	sp1 = pathdir+pathfile;
					System.out.println(sp1);
					try {
					   if(pathdir==null&&pathfile==null) {
						   FileDialog saveF = new FileDialog(th, "儲存檔案",FileDialog.SAVE);
							 saveF.setVisible(true);

								FileWriter fos = new FileWriter(saveF.getDirectory() + saveF.getFile());
								fos.write(txt.getText());
								fos.close();
					   }else {
					  
							FileWriter fos = new FileWriter(sp1);
							fos.write(txt.getText());
							fos.close();
							  }
							}
					catch (IOException ex) {
							ex.printStackTrace();
							}}
		else if(e.getSource()==end)
		       {
			     System.exit(0);
		       }
		else if(e.getSource()==allsel)
	       {
		     txt.selectAll();
	       }
		else if(e.getSource()==copy)
	       {
		     txt.copy();
	       }
		else if(e.getSource()==paste)
	       {
		     txt.paste();
	       }
		else if(e.getSource()==jianqie)
	       {
		     txt.cut();
	       }
		else if(e.getSource()==cexiao&&um.canUndo())
	       {
			um.undo();
	       }
		else if(e.getSource()==huifu&&um.canRedo())
	       {
			um.redo();
	       }
		else if(e.getSource()==del&&!txt.getSelectedText().isEmpty())
	       {
			txt.cut();
	       }
		else if(e.getSource()==time)
	       {
			SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String Time = time.format(new java.util.Date());
			txt.append(Time);
	       }
		else if(e.getSource()==chazhao)
	       {
			//查找方法,隻能查到第一個
			int weizhi=0;int wzend=0;
			String lookinfo=JOptionPane.showInputDialog(chazhao,"請輸入:","查找",JOptionPane.INFORMATION_MESSAGE);
			String txtinfo=txt.getText();
			weizhi=txtinfo.indexOf(lookinfo);
			if(weizhi==-1) {
				JOptionPane.showMessageDialog(chazhao, "沒有查找到此結果", "查找結果", JOptionPane.INFORMATION_MESSAGE);
			}else {
			wzend=weizhi+lookinfo.length();
			txt.setSelectionStart(weizhi);
			txt.setSelectionEnd(wzend);
			txt.setRequestFocusEnabled(true);
			
		
	       }}
		else if(e.getSource()==tihuan)
	       {
			//替換方法
			if(txt.getText().isEmpty())
		       {
				JOptionPane.showMessageDialog(tihuan, "文本為空,無法替換!", "結果", JOptionPane.INFORMATION_MESSAGE);
		       }
			else {
				
				td=new TDialog();
				td.show();
				String tempend=txt.getText().replaceAll(td.jt1,td.jt2);
				txt.setText(tempend);
			}
	       }else if(e.getSource()==inpic){
	    	   //按下插圖實作
	    	   openFile.setVisible(true);
				// 擷取檔案路徑
				iconpath = openFile.getDirectory() + openFile.getFile();
				icon1=new ImageIcon(iconpath);
				int iconhigh=icon1.getIconHeight();
				int iconweidth=icon1.getIconWidth();
				JLabel iconlable=new JLabel("");
				int x=txt.getCaretPosition();//guangbiao
				int y=txt.getLineCount();
				iconx=x*8;
				icony=y*16;
				iconlable.setBounds(x*8, y*16, iconweidth, iconhigh);
				iconlable.setIcon(icon1);
				if(iconweidth<600&&iconhigh<400){
					setBounds(300,200,600, 400);
				}else{
					setBounds(300,200,iconweidth, iconhigh);
				}
				
				
				txt.add(iconlable);
				System.out.println("光标:("+x+"  ,  "+y+")");
				try {
					iconin=new FileInputStream(iconpath);
					conicon=new byte[iconin.available()];
					iconin.read(conicon);
					iconstr=new String(conicon);
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
//				for(int i=0;i<iconhigh+1;i++)//插入圖檔光标下移
//				{
//				   txt.append("\r\n");
//				}
 catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
	       }else if(e.getSource()==ziti){
	    	   FJF1 fontjf=new FJF1();
	    	   fontjf.setVisible(true);
	       }else if(e.getSource()==openhun)
	         {
	    	   //打開混合檔案
	    	   openFile.setVisible(true);
				// 擷取檔案路徑
				filePath = openFile.getDirectory() + openFile.getFile();
			try {
				fis = new FileInputStream(filePath);
				File fname=new File(filePath);
				content = new byte[fis.available()];
				fis.read(content);
				
				int[] f=new int[3];
				f=cutarr(content);
				int txtchangdu =f[0];
				int ix=f[1];
				int iy=f[2];
				content=noone(content);
				
				//分開文字圖檔
				byte[] txtby=new byte[txtchangdu];
				byte[] iconby=new byte[content.length-txtchangdu];
				System.arraycopy(content,0,txtby,0,txtby.length);
				System.arraycopy(content,txtby.length,iconby,0,iconby.length);
			  showicon(iconby,ix,iy);
				
				txt.setText(new String(txtby));
				//圖檔判斷結尾
				fis.close();
				setTitle(fname.getName()+" - 記事本");
			    }
			catch(IOException e3)
			    {
				 System.out.println("輸入輸出異常");
				}
		                             }
			else if(e.getSource()==newfile) {
				 setTitle("建立文本文檔 - 記事本");
			     txt.setText("");
			     }
			else if(e.getSource()==osave) {
			     FileDialog saveFile = new FileDialog(th, "儲存檔案",FileDialog.SAVE);
				 saveFile.setVisible(true);
				 sp = saveFile.getDirectory() + saveFile.getFile();
			try    {//String si=null;
					FileWriter fos = new FileWriter(sp);
					//if(iconx!=0&&icony!=0){
						
					//	si=iconstart+iconstr+iconend;
					//}
					
					
					
					fos.write(txt.getText());//iconstr211  如果加上圖檔
					fos.close();
					}
			catch (IOException ex) {
					ex.printStackTrace();
					}
			                             
	    	   
	    	   
	         }else if(e.getSource()==savehun) 
	           {
	        	 //儲存混合檔案
	        	 FileDialog saveFile = new FileDialog(th, "儲存混合檔案",FileDialog.SAVE);
				 saveFile.setVisible(true);
				 sp = saveFile.getDirectory() + saveFile.getFile();
				 
			try    {
					FileOutputStream fos = new FileOutputStream(sp);
					
					FileInputStream icsin = new FileInputStream(iconpath);
					
					byte[] txtby=txt.getText().getBytes();
					byte[] iconby=new byte[icsin.available()];//儲存圖檔數組
					icsin.read(iconby);
					byte[] data3 =copy(txtby,iconby);
					
					data3=add(data3);
					
					fos.write(data3);
					fos.close();
					icsin.close();
					}
			catch (IOException ex) {
					ex.printStackTrace();
					}
			                              }
	        	 
	           }
		
		
	
	
  }//jiantin1結尾


}//第一類結尾


class TDialog extends JDialog{
	
	private static final long serialVersionUID = 1L;
	JTextField jtf1;
	JTextField jtf2;
	String jt1="";
	String jt2="";
	TDialog jf2=this;
	public TDialog() {
		setTitle("替換");
        setModal(true);
        setSize(200, 140);//對話框的大小
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);//關閉對話框
        setLocationRelativeTo(null);
        JLabel jl1 = new JLabel("替換:");
        final JTextField jtf1 = new JTextField(8);
        JLabel jl2 = new JLabel("替換為:");
        final JTextField jtf2 = new JTextField(8);
     
       
        final JPanel jp = new JPanel(new GridLayout(2, 2));
        jp.add(jl1);
        jp.add(jtf1);
        jp.add(jl2);
        jp.add(jtf2);
       
        JButton jb = new JButton("确認替換");
       
        jb.addActionListener(new ActionListener() {
             
            public void actionPerformed(ActionEvent e) {
               jt1=jtf1.getText();
               jt2=jtf2.getText();
                jp.setVisible(false);
                jf2.setVisible(false);  
            }
        });
         
        add(jp);
        add(jb,BorderLayout.SOUTH);
         
	}
}


class picture extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	String path1=".\\wei3.jpg";
	String path2=".\\ali3.jpg";
	ImageIcon pwei=new ImageIcon(path1);
	ImageIcon pali=new ImageIcon(path2);



	public picture() {
		setTitle("\u8D5E   \u8D4F");
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel biaoqian1 = new JLabel("\u5FAE   \u4FE1");
		biaoqian1.setFont(new Font("宋體", Font.PLAIN, 14));
		biaoqian1.setBounds(84, 203, 79, 15);
		contentPane.add(biaoqian1);
		
		JLabel biaoqian2 = new JLabel("\u652F \u4ED8 \u5B9D");
		biaoqian2.setFont(new Font("宋體", Font.PLAIN, 14));
		biaoqian2.setBounds(269, 203, 68, 15);
		contentPane.add(biaoqian2);
		
		JLabel wechat = new JLabel("");
		wechat.setBounds(31, 30, 150, 150);
		contentPane.add(wechat);
		
		JLabel alipay = new JLabel("");
		alipay.setBounds(230, 30, 150, 150);
		contentPane.add(alipay);
		wechat.setIcon(pwei);
		alipay.setIcon(pali);
		
	}



}