java记事本

package notepad;
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
import java.io.*;

public class Notepad extends JFrame implements ActionListener{
	JMenuBar menu=null;
	JMenu wenjian,bianji;
	JMenuItem a1,a2,a3,a4,b1,b2,b3,b4;
	JTextArea wbk=null;
	public static void main(String[] args) {
		Notepad pad=new Notepad();
	}
	
	public Notepad(){
		menu=new JMenuBar();
		wenjian=new JMenu("文件");
		a1=new JMenuItem("打开");
		a2=new JMenuItem("保存");
		a3=new JMenuItem("另存为");
		a4=new JMenuItem("新建");
		bianji=new JMenu("编辑");
		b1=new JMenuItem("剪切");
		b2=new JMenuItem("复制");
		b3=new JMenuItem("粘贴");
		b4=new JMenuItem("清空");
		wbk=new JTextArea();
		
		wenjian.add(a4);
		wenjian.add(a1);
		wenjian.add(a2);
		wenjian.add(a3);
		bianji.add(b1);
		bianji.add(b2);
		bianji.add(b3);
		bianji.add(b4);
		menu.add(wenjian);
		menu.add(bianji);
		
		this.setVisible(true);
		this.setSize(500, 600);
		this.setLocation(400, 60);
		this.setTitle("记事本");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setJMenuBar(menu);
		this.add(wbk);
		
		a1.addActionListener(this);
		a1.setActionCommand("a1");
		a2.addActionListener(this);
		a2.setActionCommand("a2");
		a3.addActionListener(this);
		a3.setActionCommand("a3");
		a4.addActionListener(this);
		a4.setActionCommand("a4");
		b3.addActionListener(this);
		b3.setActionCommand("b3");
		b4.addActionListener(this);
		b4.setActionCommand("b4");
		
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("a1"))
		{
			JFileChooser wjxz=new JFileChooser();
			wjxz.setDialogTitle("选择文件");
			wjxz.showOpenDialog(null);
			wjxz.setVisible(true);
			String lujing=wjxz.getSelectedFile().getAbsolutePath();
			FileReader read=null;
			BufferedReader read1=null;
			try
			{
				read=new FileReader(lujing);
				read1=new BufferedReader(read);
				String s="",d="";
				while((s=read1.readLine())!=null)
					d+=(s+"\n");
				wbk.setText(d);
			}
			catch(Exception e1){}
			finally
			{
				try{
				read.close();
				read1.close();
				}
				catch(Exception e2){}
			}
		}
		
		else if(e.getActionCommand().equals("a3"))
		{
			JFileChooser wjbc=new JFileChooser();
			wjbc.setDialogTitle("另存为");
			wjbc.showSaveDialog(null);
			wjbc.setVisible(true);
			String lujing2=wjbc.getSelectedFile().getAbsolutePath();
			PrintStream p1=null;
			try
			{
				p1=new PrintStream(lujing2);
				System.setOut(p1);
				System.out.println(this.wbk.getText());
			}
			catch(Exception e3){}
			finally
			{
				try
				{
					p1.close();
				}
				catch(Exception e4){}
			}
		}
		
		else if(e.getActionCommand().equals("a4"))
		{
			if(this.wbk.getText().equals(""))
			{	
				this.wbk.setText(null);
			}
			else
			{
				JOptionPane jo;
				jo = new JOptionPane();
				jo.showMessageDialog(null,"提示:"+"\n"+"请先保存文件再清空!");
			}
		}
		else if(e.getActionCommand().equals("a2"))
		{
			String lujing="C:\\Users\\kangbiao\\Desktop";
			PrintStream p1=null;
			try
			{
				p1=new PrintStream(lujing);
				System.setOut(p1);
				System.out.println(this.wbk.getText());
			}
			catch(Exception e3){}
			finally
			{
				try
				{
					p1.close();
				}
				catch(Exception e4){}
			}
		}
		
		else if(e.getActionCommand().equals("b4"))
		{
			this.wbk.setText(null);
		}
		
	}
	
}




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值