NotepadBaseOnJava项目总结与问题(3)

1.获得光标的行列数

//XXX:Add Update StateBar Info Event
		textArea.addCaretListener(new CaretListener() {
			@Override
			public void caretUpdate(CaretEvent e) {
				try{
				int offset=e.getDot();
				int row=textArea.getLineOfOffset(offset);
				int col=offset-textArea.getLineStartOffset(row);
					System.out.println("Line: " + (row + 1) + ", Column: " + (col+1));
					bar.setInfoString("","行: "+(row+1)+", 列: "+(col+1)); 
				}catch(Exception ex){
					ex.printStackTrace();
				}
			}
		});


2.实现撤销

专门负责Undo的类:

//declaration : Undo
private UndoManager um=new UndoManager();
为菜单项注册监听器

goBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
		System.out.println("Undo:"+um.getUndoPresentationName());
		um.undo();
		if(um.canUndo()==false)
			goBack.setEnabled(false);
}});
为Edit事件注册监听器

	//Add Edit Event//XXX:增加编辑监听器
	main.textArea.getDocument().addUndoableEditListener(new UndoableEditListener() {public void undoableEditHappened(UndoableEditEvent e) {
			um.addEdit(e.getEdit());
			goBack.setEnabled(true);
			FileChooser.setFlagToCHANGED();
			FileChooser.SetTitleWithFlag();
			main.setTitle(FileChooser.getTitle());
			System.out.println("EditListener:Content Changed");
		}});


3.实现剪切/复制/粘贴

可以手工new一个System ClipBoard

static private Clipboard clipboard=Toolkit.getDefaultToolkit().getSystemClipboard(); 
	
	public static void copytoClipboard(String content){
		StringSelection s=new StringSelection(content);
		clipboard.setContents(s,null);
	}
	
	public static String getfromClipboard(){
		String content="";
		if(clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)){
			try{
				content=(String)clipboard.getData(DataFlavor.stringFlavor);
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}
		return content;
	}

也可以直接用JTextArea的copy(),paste(),cut()。


4.输入的合法性检查

实现一个Document的子类

package GUI;

import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

class numOnlyDocument extends PlainDocument {
	static private boolean isValid=false;
	public void insertString(int offset,String s,AttributeSet attrSet) throws BadLocationException{
		isValid=false;
		try{
			Integer.parseInt(s);
			isValid=true;
		}
		catch(NumberFormatException ex){
			return;
		}
		super.insertString(offset,s,attrSet);		
	}
	public boolean isValid(){return isValid;}
}
设置Document到TextField

numOnlyDocument numDoc=new numOnlyDocument();
jdText.setDocument(numDoc);


5.打开文件/保存文件

try(FileReader fr=new FileReader(FileChooser.chooser.getSelectedFile().getPath())){
				char[] cbuf=new char[16];
				int hasRead=0;
				while((hasRead=fr.read(cbuf))>0)
					(textArea).append(String.copyValueOf(cbuf,0,hasRead));
				fileFlag=OPENFILE_UNCHANGED;
				SetTitleWithFlag();
				flag=true;
				System.out.println("OPEN:"+openFileFullName);
			}
			catch(IOException ex){
				ex.printStackTrace();
			}	
try (FileWriter fw = new FileWriter(FileChooser.chooser.getSelectedFile())) {
					fw.write(textArea.getText());
					flag=true;
				} catch (IOException e1) {
					e1.printStackTrace();
				}







你可能感兴趣的:(Java学习笔记)