JEditorPane插入很多字符和撤销 恢复操作
来源:互联网 发布:caffe源码解析 编辑:程序博客网 时间:2024/05/01 14:18
JEditorPane插入很多字符和撤销 恢复操作, 下面是代码, NewFrame类中 调用 UndoWrapper类
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import work.UndoWrapper;
public class NewJFrame extends javax.swing.JFrame implements ActionListener ...{
private JPanel jp1;
private JTextPane jep;
private JScrollPane jsp;
private JButton font;
private JButton redo;
private JButton cancel;
private JButton replace;
private static ArrayList<Document> beifenDoc = new ArrayList<Document>();
private static int cancelIndex = 0;
private static int docIndex = 0;
private static Document yyy;
private static UndoWrapper undo;
/**//**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) ...{
NewJFrame inst = new NewJFrame();
inst.setVisible(true);
}
public NewJFrame() ...{
super();
initGUI();
}
private void initGUI() ...{
try ...{
BorderLayout thisLayout = new BorderLayout();
getContentPane().setLayout(thisLayout);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
...{
jp1 = new JPanel();
getContentPane().add(jp1, BorderLayout.NORTH);
...{
font = new JButton();
font.addActionListener(this);
jp1.add(font);
font.setText("font");
}
...{
redo = new JButton();
jp1.add(redo);
redo.addActionListener(this);
redo.setText("redo");
}
...{
cancel = new JButton();
jp1.add(cancel);
cancel.addActionListener(this);
cancel.setText("cancel");
}
...{
replace = new JButton();
jp1.add(replace);
replace.addActionListener(this);
replace.setText("replace");
}
}
...{
jsp = new JScrollPane();
getContentPane().add(jsp, BorderLayout.CENTER);
...{
jep = new JTextPane();
jep.setText(openFile("g:/spring_mass.adm.txt"));
jep.setEditable(false);
yyy = jep.getDocument();
undo = new UndoWrapper(jep);
//(Sundo.undoManager).undo();
beifenDoc.add(jep.getDocument());
jsp.setViewportView(jep);
// jep.setStyledDocument(new DefaultStyledDocument());
}
}
pack();
setSize(400, 300);
} catch (Exception e) ...{
e.printStackTrace();
}
}
/**//**
*
* @param editor
* 因为 在 initGUI()中 已经将 本程序中的JEditorPane与UndoWrapper关联,即new UndoWrapper(jep);
* 所以,采用现成的实现 JEditorPane 撤销 恢复功能 的类 UndoWrapper,调用其中的undoManager属性,
* 进行响应的操作。
*
*/
public static void cancel(JEditorPane editor) ...{
try...{
undo.undoManager.undo();
}catch(CannotUndoException e)...{
System.out.println("无可撤销");
}
}
public static void redo(JEditorPane editor) ...{
try...{
undo.undoManager.redo();
}catch(CannotRedoException e)...{
System.out.println("无可恢复");
}
}
private String openFile(String filePath) ...{
File f = new File(filePath);
String ee = new String();
System.out.println("Opening file: " + filePath);
try ...{
InputStream is = new FileInputStream(f);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line; // 用来保存每行读取的内容
line = reader.readLine(); // 读取第一行
while (line != null) ...{ // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append(" "); // 添加换行符
line = reader.readLine(); // 读取下一行
}
// System.out.print(buffer.toString());
ee = buffer.toString();
}
catch (FileNotFoundException e) ...{
e.printStackTrace();
} catch (IOException e) ...{
e.printStackTrace();
}
return ee;
}
public static void replace(JEditorPane editor, String objective) ...{
try ...{
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setBackground(attr, Color.BLUE);// 理解为,先将颜色改变赋予属性,
Document docs = editor.getDocument();
String fileString = editor.getText();
int size = fileString.length();
String selectedString = editor.getSelectedText();
if (selectedString == null)...{
return;
}
int s0 = fileString.indexOf(selectedString)
+ selectedString.length();
int s1 = fileString.indexOf(",", s0);
int s2 = fileString.indexOf(" ", s0);
int s3 = fileString.indexOf(" ", s0);
int s4 = fileString.indexOf(" ", s0);
if (s1 < 0)
s1 = size;
if (s2 < 0)
s2 = size;
if (s3 < 0)
s3 = size;
if (s4 < 0)
s4 = size;
int endIndex = Math.min(Math.min(Math.min(s1, s2), s3), s4);
if (endIndex > s0) ...{
docs.remove(s0, endIndex - s0);
docs.insertString(s0, objective, attr);
beifenDoc.add(docs);
} else ...{
int enter = fileString.indexOf(" ", s0);
if (enter == s0)
return;
int s00 = s0 + 1;
s1 = fileString.indexOf(",", s00);
s2 = fileString.indexOf(" ", s00);
s3 = fileString.indexOf(" ", s00);
s4 = fileString.indexOf(" ", s00);
if (s1 < 0)
s1 = size;
if (s2 < 0)
s2 = size;
if (s3 < 0)
s3 = size;
if (s4 < 0)
s4 = size;
endIndex = Math.min(Math.min(Math.min(s1, s2), s3), s4);
docs.remove(s00, endIndex - s00);
docs.insertString(s00, objective, attr);
beifenDoc.add(docs);
}
} catch (BadLocationException e) ...{
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) ...{
Object obj = e.getSource();
if (obj == cancel) ...{
JEditorPane editor = jep;
cancel(editor);
}
if (obj == redo) ...{
JEditorPane editor = jep;
redo(editor);
}
if (obj == replace) ...{
JEditorPane editor = jep;
replace(editor, "QQ");
}
}
}
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import work.UndoWrapper;
public class NewJFrame extends javax.swing.JFrame implements ActionListener ...{
private JPanel jp1;
private JTextPane jep;
private JScrollPane jsp;
private JButton font;
private JButton redo;
private JButton cancel;
private JButton replace;
private static ArrayList<Document> beifenDoc = new ArrayList<Document>();
private static int cancelIndex = 0;
private static int docIndex = 0;
private static Document yyy;
private static UndoWrapper undo;
/**//**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) ...{
NewJFrame inst = new NewJFrame();
inst.setVisible(true);
}
public NewJFrame() ...{
super();
initGUI();
}
private void initGUI() ...{
try ...{
BorderLayout thisLayout = new BorderLayout();
getContentPane().setLayout(thisLayout);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
...{
jp1 = new JPanel();
getContentPane().add(jp1, BorderLayout.NORTH);
...{
font = new JButton();
font.addActionListener(this);
jp1.add(font);
font.setText("font");
}
...{
redo = new JButton();
jp1.add(redo);
redo.addActionListener(this);
redo.setText("redo");
}
...{
cancel = new JButton();
jp1.add(cancel);
cancel.addActionListener(this);
cancel.setText("cancel");
}
...{
replace = new JButton();
jp1.add(replace);
replace.addActionListener(this);
replace.setText("replace");
}
}
...{
jsp = new JScrollPane();
getContentPane().add(jsp, BorderLayout.CENTER);
...{
jep = new JTextPane();
jep.setText(openFile("g:/spring_mass.adm.txt"));
jep.setEditable(false);
yyy = jep.getDocument();
undo = new UndoWrapper(jep);
//(Sundo.undoManager).undo();
beifenDoc.add(jep.getDocument());
jsp.setViewportView(jep);
// jep.setStyledDocument(new DefaultStyledDocument());
}
}
pack();
setSize(400, 300);
} catch (Exception e) ...{
e.printStackTrace();
}
}
/**//**
*
* @param editor
* 因为 在 initGUI()中 已经将 本程序中的JEditorPane与UndoWrapper关联,即new UndoWrapper(jep);
* 所以,采用现成的实现 JEditorPane 撤销 恢复功能 的类 UndoWrapper,调用其中的undoManager属性,
* 进行响应的操作。
*
*/
public static void cancel(JEditorPane editor) ...{
try...{
undo.undoManager.undo();
}catch(CannotUndoException e)...{
System.out.println("无可撤销");
}
}
public static void redo(JEditorPane editor) ...{
try...{
undo.undoManager.redo();
}catch(CannotRedoException e)...{
System.out.println("无可恢复");
}
}
private String openFile(String filePath) ...{
File f = new File(filePath);
String ee = new String();
System.out.println("Opening file: " + filePath);
try ...{
InputStream is = new FileInputStream(f);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line; // 用来保存每行读取的内容
line = reader.readLine(); // 读取第一行
while (line != null) ...{ // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append(" "); // 添加换行符
line = reader.readLine(); // 读取下一行
}
// System.out.print(buffer.toString());
ee = buffer.toString();
}
catch (FileNotFoundException e) ...{
e.printStackTrace();
} catch (IOException e) ...{
e.printStackTrace();
}
return ee;
}
public static void replace(JEditorPane editor, String objective) ...{
try ...{
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setBackground(attr, Color.BLUE);// 理解为,先将颜色改变赋予属性,
Document docs = editor.getDocument();
String fileString = editor.getText();
int size = fileString.length();
String selectedString = editor.getSelectedText();
if (selectedString == null)...{
return;
}
int s0 = fileString.indexOf(selectedString)
+ selectedString.length();
int s1 = fileString.indexOf(",", s0);
int s2 = fileString.indexOf(" ", s0);
int s3 = fileString.indexOf(" ", s0);
int s4 = fileString.indexOf(" ", s0);
if (s1 < 0)
s1 = size;
if (s2 < 0)
s2 = size;
if (s3 < 0)
s3 = size;
if (s4 < 0)
s4 = size;
int endIndex = Math.min(Math.min(Math.min(s1, s2), s3), s4);
if (endIndex > s0) ...{
docs.remove(s0, endIndex - s0);
docs.insertString(s0, objective, attr);
beifenDoc.add(docs);
} else ...{
int enter = fileString.indexOf(" ", s0);
if (enter == s0)
return;
int s00 = s0 + 1;
s1 = fileString.indexOf(",", s00);
s2 = fileString.indexOf(" ", s00);
s3 = fileString.indexOf(" ", s00);
s4 = fileString.indexOf(" ", s00);
if (s1 < 0)
s1 = size;
if (s2 < 0)
s2 = size;
if (s3 < 0)
s3 = size;
if (s4 < 0)
s4 = size;
endIndex = Math.min(Math.min(Math.min(s1, s2), s3), s4);
docs.remove(s00, endIndex - s00);
docs.insertString(s00, objective, attr);
beifenDoc.add(docs);
}
} catch (BadLocationException e) ...{
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) ...{
Object obj = e.getSource();
if (obj == cancel) ...{
JEditorPane editor = jep;
cancel(editor);
}
if (obj == redo) ...{
JEditorPane editor = jep;
redo(editor);
}
if (obj == replace) ...{
JEditorPane editor = jep;
replace(editor, "QQ");
}
}
}
使用的时候,只需要将你创建的JEditorPane作为对象传入UndoWrapper中即可。使用方式如下
new UndoWrapper(editorPane);
OK这样你的编辑器就具有了Undo Redo功能,而且是次数不收限制的。
我在使用的时候,除了UndoWrapper undo = new UndoWrapper(editorPane); 这一步必不可少之外,在按钮的相应程序段, actionPerformend()中
undo.undoManager.undo(),这样才实现了像记事本,word等的撤销功能
package work;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JEditorPane;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.JTextComponent;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
/**//**
* UndoWrapper is responsible for adding undo and redo support to text
* components.
*
* @author Antonio Vieiro (antonio@antonioshome.net), $Author: $
* @version $Revision: $
*/
public class UndoWrapper implements UndoableEditListener ...{
public UndoManager undoManager;
private UndoAction undoAction;
private RedoAction redoAction;
private JEditorPane textComponent;
/**//**
* Creates a new instance of UndoWrapper
*/
public UndoWrapper(JEditorPane aComponent) ...{
textComponent = aComponent;
undoManager = new UndoManager();
undoAction = new UndoAction();
redoAction = new RedoAction();
textComponent.getDocument().addUndoableEditListener(this);
textComponent.getInputMap()
.put((KeyStroke) undoAction.getValue(Action.ACCELERATOR_KEY),
"undo");
textComponent.getInputMap()
.put((KeyStroke) redoAction.getValue(Action.ACCELERATOR_KEY),
"redo");
textComponent.getActionMap().put("undo", undoAction);
textComponent.getActionMap().put("redo", redoAction);
}
public void undoableEditHappened(UndoableEditEvent e) ...{
undoManager.addEdit(e.getEdit());
undoAction.updateUndoState();
redoAction.updateRedoState();
}
/**//**
* UndoAction is the Action responsible for handling the undo operation.
*/
class UndoAction extends AbstractAction ...{
public UndoAction() ...{
super("Cannot undo"); // TODO: I18N
setEnabled(false);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl Z"));
}
public void actionPerformed(ActionEvent e) ...{
try ...{
undoManager.undo();
} catch (CannotUndoException cue) ...{
// TODO: Use logging?
cue.printStackTrace(System.err);
}
updateUndoState();
redoAction.updateRedoState();
}
void updateUndoState() ...{
if (undoManager.canUndo()) ...{
setEnabled(true);
putValue(Action.NAME, "Undo"); // TODO I18N
} else ...{
setEnabled(false);
putValue(Action.NAME, "Cannot undo"); // TODO I18N
}
}
}
/**//**
* RedoAction is the Action responsible for handling the redo operation.
*/
class RedoAction extends AbstractAction ...{
public RedoAction() ...{
super("Cannot redo"); // TODO I18N
setEnabled(false);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl Y"));
}
public void actionPerformed(ActionEvent e) ...{
try ...{
undoManager.redo();
} catch (CannotRedoException cre) ...{
// TODO: Use logging?
cre.printStackTrace(System.err);
}
updateRedoState();
undoAction.updateUndoState();
}
void updateRedoState() ...{
if (undoManager.canRedo()) ...{
setEnabled(true);
putValue(Action.NAME, "Redo"); // TODO I18N
} else ...{
setEnabled(false);
putValue(Action.NAME, "Cannot redo"); // TODO I18N
}
}
}
UndoAction getUndoAction() ...{
return undoAction;
}
RedoAction getRedoAction() ...{
return redoAction;
}
}
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JEditorPane;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.JTextComponent;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
/**//**
* UndoWrapper is responsible for adding undo and redo support to text
* components.
*
* @author Antonio Vieiro (antonio@antonioshome.net), $Author: $
* @version $Revision: $
*/
public class UndoWrapper implements UndoableEditListener ...{
public UndoManager undoManager;
private UndoAction undoAction;
private RedoAction redoAction;
private JEditorPane textComponent;
/**//**
* Creates a new instance of UndoWrapper
*/
public UndoWrapper(JEditorPane aComponent) ...{
textComponent = aComponent;
undoManager = new UndoManager();
undoAction = new UndoAction();
redoAction = new RedoAction();
textComponent.getDocument().addUndoableEditListener(this);
textComponent.getInputMap()
.put((KeyStroke) undoAction.getValue(Action.ACCELERATOR_KEY),
"undo");
textComponent.getInputMap()
.put((KeyStroke) redoAction.getValue(Action.ACCELERATOR_KEY),
"redo");
textComponent.getActionMap().put("undo", undoAction);
textComponent.getActionMap().put("redo", redoAction);
}
public void undoableEditHappened(UndoableEditEvent e) ...{
undoManager.addEdit(e.getEdit());
undoAction.updateUndoState();
redoAction.updateRedoState();
}
/**//**
* UndoAction is the Action responsible for handling the undo operation.
*/
class UndoAction extends AbstractAction ...{
public UndoAction() ...{
super("Cannot undo"); // TODO: I18N
setEnabled(false);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl Z"));
}
public void actionPerformed(ActionEvent e) ...{
try ...{
undoManager.undo();
} catch (CannotUndoException cue) ...{
// TODO: Use logging?
cue.printStackTrace(System.err);
}
updateUndoState();
redoAction.updateRedoState();
}
void updateUndoState() ...{
if (undoManager.canUndo()) ...{
setEnabled(true);
putValue(Action.NAME, "Undo"); // TODO I18N
} else ...{
setEnabled(false);
putValue(Action.NAME, "Cannot undo"); // TODO I18N
}
}
}
/**//**
* RedoAction is the Action responsible for handling the redo operation.
*/
class RedoAction extends AbstractAction ...{
public RedoAction() ...{
super("Cannot redo"); // TODO I18N
setEnabled(false);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl Y"));
}
public void actionPerformed(ActionEvent e) ...{
try ...{
undoManager.redo();
} catch (CannotRedoException cre) ...{
// TODO: Use logging?
cre.printStackTrace(System.err);
}
updateRedoState();
undoAction.updateUndoState();
}
void updateRedoState() ...{
if (undoManager.canRedo()) ...{
setEnabled(true);
putValue(Action.NAME, "Redo"); // TODO I18N
} else ...{
setEnabled(false);
putValue(Action.NAME, "Cannot redo"); // TODO I18N
}
}
}
UndoAction getUndoAction() ...{
return undoAction;
}
RedoAction getRedoAction() ...{
return redoAction;
}
}
- JEditorPane插入很多字符和撤销 恢复操作
- 撤销和恢复
- 【Git】Git撤销操作、删除文件和恢复文件
- Git 撤销操作、删除文件和恢复文件
- 小白学tkinter(Text组件的‘撤销’和‘恢复’操作)
- 【vim】撤销和恢复撤销快捷键
- mysql 回滚,撤销操作,恢复数据
- Android EditText 的撤销和恢复(反撤销)
- jtabbedpane.jsplitpane和jeditorpane
- Git学习之路(4)- 撤销操作、删除文件和恢复文件
- java文本的撤销和恢复
- vim小技巧中恢复/撤销/复制部分字符
- JEditorPane和JTextPane学习总结
- JEditorPane和JTextPane学习总结
- MySql授权和撤销权限操作
- Git撤销提交和修改相关操作
- vim中的删除,复制和撤销操作
- 字符的替换及撤销(Undo操作)
- 2008年4月26日,CCNP 642-901 977分 pass
- XML客户端中文问题解决
- Java谜题3——循环谜题
- 软件研发领域的特征-探讨
- 关于消息队列这种设计模式的利弊
- JEditorPane插入很多字符和撤销 恢复操作
- 2007最经典语录
- 母亲一生撒的8个谎言
- 在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接
- [JSP][Servlet][监听]
- 求助。。。
- System Design with SystemC——学习笔记3
- 汇编学习一: 汇编传参例子
- XML Schema 基本知识