java简易记事本


//包含功能:新建,打开,保存,复制,粘贴,查找,替换,版权信息等

java简易记事本_第1张图片java简易记事本_第2张图片java简易记事本_第3张图片java简易记事本_第4张图片


package java_test;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FileDialog;
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.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JDialog;
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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * 记事本
 * @author D_xiao
 *
 */
public class Test extends JFrame {
	
	int start=0;//查找开始位置  
    int end=0;//查找结束位置 
	JFrame jf = new JFrame();
	File f;
	FileDialog open = new FileDialog(jf,"打开",FileDialog.LOAD);
	FileDialog save = new FileDialog(jf,"保存",FileDialog.SAVE);
	
	JTextArea jta = new JTextArea();
	JPanel pUp = new JPanel();
	JScrollPane jp = new JScrollPane(jta);//新建一个滚动条界面,将文本框传入
	JMenuBar nm = new JMenuBar();
	JMenu m1 = new JMenu("文件");
	JMenu m2 = new JMenu("编辑");
	JMenu m3 = new JMenu("帮助");
	JMenuItem m11 = new JMenuItem("新建");
	JMenuItem m12 = new JMenuItem("打开");
	JMenuItem m13 = new JMenuItem("另存为");
	JMenuItem m14 = new JMenuItem("退出");
	JMenuItem m21 = new JMenuItem("复制");
	JMenuItem m22 = new JMenuItem("粘贴");
	JMenuItem m23 = new JMenuItem("查找");
	JMenuItem m24 = new JMenuItem("替换");
	JMenuItem m31 = new JMenuItem("关于");
	Container c;
	Clipboard clipboard = getToolkit().getSystemClipboard();
	private void init(){
		c=this.getContentPane();
		c.setLayout(new BorderLayout());
		c.add(jp,BorderLayout.CENTER);
		c.add(nm,BorderLayout.NORTH);
		nm.add(m1);nm.add(m2);nm.add(m3);
		m1.add(m11);m1.add(m12);m1.add(m13);m1.add(m14);
		m2.add(m21);m2.add(m22);m2.add(m23);m2.add(m24);
		m3.add(m31);
		//新建
		m11.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				Test xinjian = new Test("新建文本");
		}});
		//打开
		m12.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				open.setVisible(true);
				String path = open.getDirectory();
				String fileName = open.getFile();
				if(path == null||fileName == null)
					return;
				jta.setText("");
				File file = new File(path,fileName);
				try{
					BufferedReader buf = new BufferedReader(new FileReader(file));
					String line = null;
					while ((line=buf.readLine()) !=null){
						jta.append(line +"\r\n");
						setTitle(fileName);
				}
				buf.close();
				}catch(IOException io){
					throw new RuntimeException("读取失败");
				}
		}});
		//另存为
		m13.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				save.setVisible(true);
				String path = save.getDirectory();
				String fileName = save.getFile();
				if(path == null || fileName == null)
					return;
				f = new File(path,fileName);
				try {
					BufferedWriter bufw = new BufferedWriter(new FileWriter(f));
					String text = jta.getText();
					bufw.write(text);
					bufw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}});
		m14.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				System.exit(0);
		}});
		//复制
		m21.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				 //拖动选取文本
				String temp = jta.getSelectedText();
				//把获取的内容复制到连续字符器,这个类继承了剪贴板接口
				StringSelection text = new StringSelection(temp);
				//把内容放在剪贴板
				clipboard.setContents(text,null);
			}});
		//粘贴
		m22.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				 //Transferable接口,把剪贴板的内容转换成数据
		          Transferable contents = clipboard.getContents(this);
		          //DataFalvor类判断是否能把剪贴板的内容转换成所需数据类型
		          DataFlavor flavor = DataFlavor.stringFlavor;
		          //如果可以转换
		          if(contents.isDataFlavorSupported(flavor)){
		              String str;
		              try {//开始转换
		                 str=(String)contents.getTransferData(flavor);
		                 //如果要粘贴时,鼠标已经选中了一些字符
		                 if(jta.getSelectedText()!=null){
		                     //定位被选中字符的开始位置
		                     int start = jta.getSelectionStart();
		                     //定位被选中字符的末尾位置
		                     int end = jta.getSelectionEnd();
		                     //把粘贴的内容替换成被选中的内容
		                    jta.replaceRange(str, start, end);
		                 }else{
		                     //获取鼠标所在TextArea的位置
		                     int mouse = jta.getCaretPosition();
		                     //在鼠标所在的位置粘贴内容
		                     jta.insert(str, mouse);
		                 }
		              } catch(UnsupportedFlavorException e) {
		                 e.printStackTrace();
		              } catch (IOException e) {
		                 e.printStackTrace();
		              } catch(IllegalArgumentException e){
		                 e.printStackTrace();
		              }
		          }
			}});
		//查找
		m23.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				final JDialog search=new JDialog();  
                JPanel jp = new JPanel();
                JLabel lb1 = new JLabel("查找内容:");
                final JTextField jtf = new JTextField(10);
                JButton jbu = new JButton("查找下一个");
                JButton jbu2 = new JButton("取消");
                jp.add(lb1);jp.add(jtf);jp.add(jbu);jp.add(jbu2);
                search.add(jp);
                search.setSize(300, 200);  
                search.setLocation(450,350); 
                search.setVisible(true);
                search.setTitle("查找");
                jbu.addActionListener(new ActionListener(){
                	public void actionPerformed(ActionEvent ae){
                		String findText=jtf.getText();//查找的字符串  
                        String textArea=jta.getText();//当前文本框的内容  
                        start=textArea.indexOf(findText,end);  
                        end=start+findText.length();  
                        if(start==-1) {  //没有找到 
                            JOptionPane.showMessageDialog(null,"没找到"+findText,"记事本",JOptionPane.WARNING_MESSAGE);  
                            jta.select(start, end);  
                        }else{  
                            jta.select(start,end);  
                        }  
                     }});
               jbu2.addActionListener(new ActionListener(){
	               	public void actionPerformed(ActionEvent ae){
	               		search.dispose();
	               	}
               });
        }});
		//替换
		m24.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				final JDialog search=new JDialog();  
                JPanel jp = new JPanel();
                JLabel lb1 = new JLabel("查找内容:");
                JLabel lb2 = new JLabel("替换为:");
                final JTextField jtf = new JTextField(10);
                final JTextField jtf2 = new JTextField(10);
                JButton jbu = new JButton("替换");
                JButton jbu2 = new JButton("取消");
                JButton jbu3 = new JButton("查找下一个");
                jp.add(lb1);jp.add(jtf);jp.add(lb2);jp.add(jtf2);jp.add(jbu3);jp.add(jbu);jp.add(jbu2);
                search.add(jp);
                search.setSize(300, 200);  
                search.setLocation(450,350); 
                search.setVisible(true);
                search.setTitle("替换");
                jbu3.addActionListener(new ActionListener(){
                	public void actionPerformed(ActionEvent ae){
                		String findText=jtf.getText();//查找的字符串  
                        String textArea=jta.getText();//当前文本框的内容  
                        start=textArea.indexOf(findText,end);  
                        end=start+findText.length();  
                        if(start==-1) {  //没有找到 
                            JOptionPane.showMessageDialog(null,"没找到"+findText,"记事本",JOptionPane.WARNING_MESSAGE);  
                            jta.select(start, end);  
                        }else{  
                            jta.select(start,end);  
                        }  
                     }});
                jbu.addActionListener(new ActionListener(){
                	public void actionPerformed(ActionEvent ae){
                		String changeText=jtf2.getText();//替换的字符串  
                        jta.select(start, end);  
                        jta.replaceSelection(changeText);  
                        jta.select(start, end);      
                     }});
               jbu2.addActionListener(new ActionListener(){
	               	public void actionPerformed(ActionEvent ae){
	               		search.dispose();
	               	}
               });
        }});
		//版权信息
		m31.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				javax.swing.JOptionPane.showMessageDialog(null, "版权所有@D_xiao"); 
		}});
	}
		
	public Test(String title){
		super(title);
		init();
		this.setSize(585,450);
		this.setVisible(true);
		setLocationRelativeTo(null);
		jta.setLineWrap(true);//自动换行,同时把横向滚动条隐去
	}
	
	public static void main(String[] args) {
		Test chushi = new Test("记事本");
	}

}




你可能感兴趣的:(java,记事本)