记事本 demo
博客专区 > Fly_f 的博客 > 博客详情
记事本 demo
Fly_f 发表于9个月前
记事本 demo
  • 发表于 9个月前
  • 阅读 34
  • 收藏 0
  • 点赞 0
  • 评论 2

新睿云服务器60天免费使用,快来体验!>>>   

摘要: by cwg
package demo;

import java.awt.event.*;
import java.io.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;


public class TXT extends JFrame  {
	private JMenuBar JMB;
	private JMenu m1;
	private JMenu m2;
	private JMenu m3;
	private JMenuItem mi1;    // 变量名字不能用 123, 后期没法维护 , 重新定义
	private JMenuItem mi2;
	private JMenuItem mi3;
	private JMenuItem mi4;
	private JMenuItem mi5;
	private JMenuItem mi6;
	private JMenuItem mi7;
	private JMenuItem mi8;
	
	//窗口组件
	JFileChooser filechooser = new JFileChooser();
	JScrollPane sp = new JScrollPane();
	JTextArea ta = new JTextArea();
	protected Object desc;
	public TXT(){
		this.setDefaultCloseOperation(TXT.EXIT_ON_CLOSE);
		sp.getViewport().add(ta);
		this.getContentPane().add(sp);
		ta.getText();
		
		
		JMB = new JMenuBar();
		this.setJMenuBar(JMB);
		
	 	 //按钮
		 m1 = new JMenu("文件");
		 m2 = new JMenu("功能");
		 m3 = new JMenu("帮助");
		JMB.add(m1);
		JMB.add(m2);
		JMB.add(m3);
		
		 mi1 = new JMenuItem("新建");
		 mi2 = new JMenuItem("保存");
		 mi3 = new JMenuItem("打开");
		 mi4 = new JMenuItem("复制");
		 mi5 = new JMenuItem("粘贴");
		 mi6 = new JMenuItem("帮助");
		 mi7 = new JMenuItem("另存为");
		 mi8 = new JMenuItem("退出");
		m1.add(mi1);
		m1.add(mi2);
		m1.add(mi3);
		m1.add(mi7);
		m1.add(mi8);
		m2.add(mi4);
		m2.add(mi5);
		m3.add(mi6);
		
		//添加事件处理
		mi1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent mi1) {
				if(mi1.getActionCommand().equals("新建")){   //事件监听器是内部类了 不需要这个判断
					demo d = new demo();
					Thread t = new Thread(d);
					t.start();
				}	
			}
		});
		mi2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent mi2) {
				if(mi2.getActionCommand().equals("保存")){
					int i = filechooser.showSaveDialog(TXT.this);
					if(i == JFileChooser.APPROVE_OPTION){
						File f = filechooser.getSelectedFile();
						FileOutputStream out;
						try {
							out = new FileOutputStream(f);
							out.write(ta.getText().getBytes());  // 试试输入内容很多会不会有问题, 1MB
							out.close();
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
		});
		mi3.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent mi3) {
				if(mi3.getActionCommand().equals("打开")){
					int i = filechooser.showOpenDialog(TXT.this);
					if(i == JFileChooser.APPROVE_OPTION){
						File f = filechooser.getSelectedFile();
						try {
							FileReader fr = new FileReader(f);
							BufferedReader br = new BufferedReader(fr);
							String aline = br.readLine();
							while(aline != null);
						    ta.append(aline+"/r/n");
						    fr.close();
						    br.close();
						} catch (FileNotFoundException e1) {
							e1.printStackTrace();
						} catch (IOException e1) {
							e1.printStackTrace();
						}
					}
				}
				
			}
		});
		mi7.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent mi7) {
				if(mi7.getActionCommand().equals("另存为")){
					int i = filechooser.showSaveDialog(TXT.this);
					if(i == JFileChooser.APPROVE_OPTION){
						File f = filechooser.getSelectedFile();
						FileOutputStream out;
						try {
							out = new FileOutputStream(f);
							out.write(ta.getText().getBytes());
							out.close();
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
				
			}
		});
		mi8.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent mi8) {   //是不是要检测下是否保存过。 弹出提示框
			   if(mi8.getActionCommand().equals("退出")){ 
				   System.exit(0);
			   }
				
			}
		});
		
		
	}
	public static void main(String[] args){
		JFrame t = new TXT();
		t.setTitle("记事本");
		t.setSize(500, 500);
		t.setVisible(true);
		
	}
}

//新建窗口端口
class demo implements Runnable{
	public void run(){
		TXT t = new TXT();
		t.setTitle("记事本");
		t.setSize(500, 500);
		t.setVisible(true);
	}
}

// 请添加上 其他菜单事件  复制 黏贴...

2017-5-15 18:06:01

 

package demo;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.*;
import java.io.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

// 这个有必要继承 ClipboardOwner  吗?
public class TXT extends JFrame implements ClipboardOwner    {
	private JMenuBar JMB;
	private JMenu File;    // 用fileMenu, 变量首字母不要用大写,下面同理.你这个File 更系统文件File 都重名了
	private JMenu Edit;    // 去学习些java 命名规范。 (百度可以查到)
	private JMenu Help;
	private JMenu View;
	private JMenu Ormat;
	private JMenuItem FileNew;
	private JMenuItem FileSave;
	private JMenuItem FileOpen;
	private JMenuItem EditCopy;
	private JMenuItem EditPaste;
	private JMenuItem HelpHelp;
	private JMenuItem FileSaveAs;
	private JMenuItem FileQuit;
	private JMenuItem ViewStatusBar;
	private JMenuItem OrmatTypeface;
	Clipboard clb =null;
	//窗口组件
	JFileChooser filechooser = new JFileChooser();
	JScrollPane sp = new JScrollPane();
	JTextArea ta = new JTextArea();
	protected Object desc;
	public TXT(){
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		sp.getViewport().add(ta);
		this.getContentPane().add(sp);
		ta.getText();
		
		
		JMB = new JMenuBar();
		this.setJMenuBar(JMB);
		
	 	 //按钮
		 File = new JMenu("文件");
		 Edit = new JMenu("功能");
		 Help = new JMenu("帮助");
		 View = new JMenu("查看");
		 Ormat = new JMenu("格式");
		JMB.add(File);
		JMB.add(Edit);
		JMB.add(Help);
		JMB.add(View);
		JMB.add(Ormat);
		
		 FileNew = new JMenuItem("新建");
		 FileSave = new JMenuItem("保存");
		 FileOpen = new JMenuItem("打开");
		 EditCopy = new JMenuItem("复制");
		 EditPaste = new JMenuItem("粘贴");
		 HelpHelp = new JMenuItem("帮助");
		 FileSaveAs = new JMenuItem("另存为");
		 FileQuit = new JMenuItem("退出");
		 ViewStatusBar = new JMenuItem("状态栏");
		 OrmatTypeface = new JMenuItem("字体");
		 
		 File.add(FileNew);
		 File.add(FileSave);
		 File.add(FileOpen);
		 File.add(FileSaveAs);
		 File.add(FileQuit);
		 Edit.add(EditCopy);
		 Edit.add(EditPaste);
		 Help.add(HelpHelp);
		 View.add(ViewStatusBar);
		 Ormat.add(OrmatTypeface);
		 
		 
		 
		//添加事件处理
		 addWindowListener(new WindowListener() {
			@Override
			public void windowIconified(WindowEvent arg0) {
			}
			
			@Override
			public void windowDeiconified(WindowEvent arg0) {
			}
			
			@Override
			public void windowDeactivated(WindowEvent arg0) {
			}
			@Override
			public void windowClosed(WindowEvent arg0) {
			}
			
			@Override
			public void windowActivated(WindowEvent arg0) {
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
			}
			@Override
			public void windowClosing(WindowEvent arg0) {
                // 下面代码跟后面的exit 方法里面的代码重复了,  抽成一个方法,调用. 不要写两个代码一模一样的
				if(ta.getText().equals("")){
					 System.exit(0);
				}
				 int it = JOptionPane.showConfirmDialog(TXT.this, "是否保存到D://java程序//实验文件//新建文本文档.txt","结束程序",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
				 if(JOptionPane.YES_OPTION == it){
						File f = new File("D:\\java程序\\实验文件\\新建文本文档.txt");
						try {
							FileWriter fw = new FileWriter(f);
							BufferedWriter bw = new BufferedWriter(fw);
							PrintWriter pw = new PrintWriter(bw); // 为什么加一个这个writer
							pw.print(ta.getText());               // BufferedWriter.write ("xxxx") 就可以了
							
							bw.close();
							fw.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}else if(JOptionPane.NO_OPTION == it) {
						System.exit(0);
					}else if(JOptionPane.CANCEL_OPTION == it){
						return;
					}
				}
		});
		 
		 
		 
		 
		 
		 
		 FileNew.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent mi1) {
					demo d = new demo();
					Thread t = new Thread(d);
					t.start();
				}	
		});
		 FileSave.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent mi2) {
					int i = filechooser.showSaveDialog(TXT.this);
					if(i == JFileChooser.APPROVE_OPTION){
						File f = filechooser.getSelectedFile();
						FileOutputStream out;
						try {
							out = new FileOutputStream(f);
							out.write(ta.getText().getBytes());
							out.close();
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
		});
		 FileOpen.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent mi3) {
					int i = filechooser.showOpenDialog(TXT.this);
					if(i == JFileChooser.APPROVE_OPTION){
						File f = filechooser.getSelectedFile();
						try {
							FileReader fr = new FileReader(f);
							BufferedReader br = new BufferedReader(fr);
							String aline = br.readLine();
							while(aline != null);
						    ta.append(aline+"/r/n");
						    fr.close();
						    br.close();
						} catch (FileNotFoundException e1) {
							e1.printStackTrace();
						} catch (IOException e1) {
							e1.printStackTrace();
						}
					}
				}
		});
		 FileSaveAs.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent mi7) {
					int i = filechooser.showSaveDialog(TXT.this);
					if(i == JFileChooser.APPROVE_OPTION){
						File f = filechooser.getSelectedFile();
						FileOutputStream out;
						try {
							out = new FileOutputStream(f);
							out.write(ta.getText().getBytes());
							out.close();
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
		});
		 FileQuit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent mi8) {
				if(ta.getText().equals("")){
					 System.exit(0);
				}
				 int it = JOptionPane.showConfirmDialog(TXT.this, "是否保存到D://java程序//实验文件//新建文本文档.txt","结束程序",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
				 if(JOptionPane.YES_OPTION == it){
						File f = new File("D:\\java程序\\实验文件\\新建文本文档.txt");
						try {
							OutputStream out = new FileOutputStream(f);
							String str = ta.getText();
							byte[] buf = new byte[1024*5];  // 这个不对, 你确定有把内容保存到文件?
							out.write(buf);
							out.close();
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}else if(JOptionPane.NO_OPTION == it) {
						System.exit(0);
					}else if(JOptionPane.CANCEL_OPTION == it){
						return;
					}
				}
		});
		EditCopy.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				Clipboard clb = Toolkit.getDefaultToolkit().getSystemClipboard();
				StringSelection ss = new StringSelection(ta.getText());
				clb.setContents(ss, null);
			}
		});
		EditPaste.addActionListener(new ActionListener() {
			
			@Override
			public  void actionPerformed(ActionEvent arg0) {
				Clipboard clb = Toolkit.getDefaultToolkit().getSystemClipboard();
				Transferable clbt = clb.getContents(null);
				if(clbt!=null){
					if(clbt.isDataFlavorSupported(DataFlavor.stringFlavor));
					String str;
					try {
						str = (String)clbt.getTransferData(DataFlavor.stringFlavor);
						ta.append(str);
					} catch (UnsupportedFlavorException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}	
			}
		});
		
	}
	public static void main(String[] args){
		JFrame t = new TXT();
		t.setTitle("记事本");
		t.setSize(500, 500);
		t.setVisible(true);
		
	}
	@Override
	public void lostOwnership(Clipboard clipboard, Transferable contents) {
	}
}

//新建窗口端口
class demo implements Runnable{
	public void run(){
		TXT t = new TXT();
		t.setTitle("记事本");
		t.setSize(500, 500);
		t.setVisible(true);
	}
}

 

 

 

 

 

 

 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
Fly_f
粉丝 9
博文 34
码字总数 14633
评论 (2)
Fly_f
还有的菜单功能?
Fly_f
review
×
Fly_f
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: