高级语言案例分析_文本编辑器

package wb;

import java.awt.BorderLayout;
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.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
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.JTextArea;
import javax.swing.JTextField;

public class Wb extends JFrame{
	
	private JTextArea jta=new JTextArea();
	private JMenuBar mb=new JMenuBar();
	private String url = null;//文件路径
	private String str=null;
	private StringSelection ss=null;
	private Clipboard clipboard=new Clipboard(str);
	private Transferable   transferable=null;
	private DataFlavor   dataflavor=null;
	
	private JMenu file=new JMenu("文件");
	private JMenuItem fileNew=new JMenuItem("新建");
	private JMenuItem fileOpen=new JMenuItem("打开");
	private JMenuItem fileSave=new JMenuItem("保存");
	private JMenuItem exit=new JMenuItem("退出");
	
	private JMenu edit=new JMenu("编辑");
	private JMenuItem copy=new JMenuItem("复制");
	private JMenuItem paste=new JMenuItem("粘贴");
	private JMenuItem search=new JMenuItem("查找");
	private JMenuItem replace=new JMenuItem("替换");
	
	private JMenu help=new JMenu("帮助");
	private JMenuItem aboutWb=new JMenuItem("关于");
	
	private JFileChooser jfc=new JFileChooser();
	File f=new File("D:\\Documents\\java.txt");


	BorderLayout bl=new BorderLayout();
	public Wb(){
		super("文本编辑器");
		init();
	}
	public void init(){
		file.add(fileNew);
		file.add(fileOpen);
		file.add(fileSave);
		file.add(exit);
		mb.add(file);
		edit.add(copy);
		edit.add(paste);
		edit.add(search);
		edit.add(replace);
		mb.add(edit);
		help.add(aboutWb);
		mb.add(help);
		add(mb);
		add(mb, BorderLayout.NORTH);
		add(jta, BorderLayout.CENTER);
	    setSize(400,400);
		setVisible(true);
		operate();
	} 
	public void operate(){
		fileNew.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent e) {
				jta.setText(null);	
			}
		});
		fileOpen.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(f!=null){
					jfc.setSelectedFile(f);
					int a=jfc.showOpenDialog(Wb.this);
					if(a==jfc.APPROVE_OPTION){
						f=jfc.getSelectedFile();
						try {
							FileReader fr=new FileReader(f);
							int length=(int) f.length();
							char[]reader=new char[length];
							fr.read(reader, 0, length);
							jta.setText(new String(reader));
							str=f.getName();
							str=str.substring(0,str.length()-4);
							setTitle();
						} catch (FileNotFoundException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
			}	
		});
		fileSave.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(f!=null){
					jfc.setSelectedFile(f);
					int b=jfc.showSaveDialog(Wb.this);
					if(b==JFileChooser.APPROVE_OPTION){
						f=jfc.getSelectedFile();
						try {
							FileWriter fw=new FileWriter(f);
							fw.write(jta.getText());
							fw.close();
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
			}
		});
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		exit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.exit(0);
			}
		});
		copy.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String str=jta.getSelectedText();
				ss=new   StringSelection(str); 
				clipboard.setContents(ss, null);
			}
		});
		paste.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				transferable=clipboard.getContents(this);
				dataflavor=DataFlavor.stringFlavor;
				if (transferable.isDataFlavorSupported(dataflavor)) {
					int   start=jta.getSelectionStart(); 
					int   end=jta.getSelectionEnd(); 
					jta.replaceRange( null,start,end); 
					int   pos=jta.getCaretPosition(); 
					try {
						String str=(String)transferable.getTransferData(dataflavor);
						jta.insert(str, pos);
					}catch (UnsupportedFlavorException e1) {
						e1.printStackTrace();
					} catch (IOException e1) {
						e1.printStackTrace();
					} 
				}
			}
		});
		search.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				JPanel searchPanel=new JPanel();
				JLabel searchLabel=new JLabel("查找内容:");
				JTextField searchjtf=new JTextField(10);
				searchPanel.add(searchLabel);
				searchPanel.add(searchjtf);
				JOptionPane.showMessageDialog(null,searchPanel);
				/*弹出要求用户提供值或向其发出通知的标准对话框*/
				String text=jta.getText();
				int fromIndex=jta.getCaretPosition();
				//取得当前的光标位置          
				int lastfromIndex=text.indexOf(searchjtf.getText(),0);
				//获得查找后的位置              
				jta.setCaretPosition(lastfromIndex);   
				jta.moveCaretPosition(lastfromIndex+searchjtf.getText().length());
			}
		});
		replace.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				JPanel replacePanel=new JPanel();        
				JLabel replaceLabel=new JLabel("要替换的内容"); 
				JTextField inputText=new JTextField(10);     
				JLabel swapLabel=new JLabel("替换为:");    
				JTextField changetoText=new JTextField(10);  
				replacePanel.add(replaceLabel);           
				replacePanel.add(inputText);         
				replacePanel.add(swapLabel);       
				replacePanel.add(changetoText);     
				JOptionPane.showMessageDialog(null,replacePanel);   
				String text=jta.getText();//获得整个文本内容       
				String changeText=text.replaceFirst(inputText.getText(),changetoText.getText());//获得替换后的内容        
				jta.setText(changeText);
			}
		});
		aboutWb.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				JPanel jp=new JPanel();
				JTextArea aboutWbjta=new JTextArea(5,5);
				aboutWbjta.setText("作者:阿伟;日期:2013.4.4");
				jp.add(aboutWbjta);
				JOptionPane.showMessageDialog(null,aboutWbjta);
				/*aboutWbjta.setVisible(true);
				aboutWbjta.show();*/
			}
		});
		
	}
	public void setTitle() {
		this.setTitle(str+" - 记事本");
		
	}
	public static void main(String[] args) {
		Wb wb=new Wb();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值