Java 如何使用Swing实现撤消和重做操作
我编写了撤消和重做操作的逻辑。但是,它工作不正常。当我打开一个文件,然后单击“回滚”时,整个文件内容被删除。当我使用replace MenuItem替换任何单词时,然后单击“回滚”替换文本不是回滚,整个文本被删除。请帮助我。谢谢 我的代码:Java 如何使用Swing实现撤消和重做操作,java,swing,undo-redo,Java,Swing,Undo Redo,我编写了撤消和重做操作的逻辑。但是,它工作不正常。当我打开一个文件,然后单击“回滚”时,整个文件内容被删除。当我使用replace MenuItem替换任何单词时,然后单击“回滚”替换文本不是回滚,整个文本被删除。请帮助我。谢谢 我的代码: public class UndoAndRedoAction extends javax.swing.JFrame { JTextArea text; int i=0; UndoManager undoManager = new UndoManager();
public class UndoAndRedoAction extends javax.swing.JFrame {
JTextArea text;
int i=0;
UndoManager undoManager = new UndoManager();
public UndoAndRedoAction() {
initComponents();
text = new JTextArea();
rollback.setEnabled(false);
redo.setEnabled(false);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
tp = new javax.swing.JTabbedPane();
jMenuBar1 = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
open = new javax.swing.JMenuItem();
rollback = new javax.swing.JMenuItem();
redo = new javax.swing.JMenuItem();
replace = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
fileMenu.setText("File");
open.setText("Open");
open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openActionPerformed(evt);
}
});
fileMenu.add(open);
rollback.setText("Rollback");
rollback.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rollbackActionPerformed(evt);
}
});
fileMenu.add(rollback);
redo.setText("Redo");
redo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
redoActionPerformed(evt);
}
});
fileMenu.add(redo);
replace.setText("Replace");
replace.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
replaceActionPerformed(evt);
}
});
fileMenu.add(replace);
jMenuBar1.add(fileMenu);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tp, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tp, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void openActionPerformed(java.awt.event.ActionEvent evt) {
final JFileChooser jc = new JFileChooser();
int returnVal= jc.showOpenDialog(UndoAndRedoAction.this);
String title;
File file=null;
if(returnVal == JFileChooser.APPROVE_OPTION)
file = jc.getSelectedFile();
if (jc.getSelectedFile()!= null) {
BufferedReader br = null;
StringBuffer str = new StringBuffer("");
try {
br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
str.append(line + "\n");
}
}
catch (IOException ex) {
Logger.getLogger(UndoAndRedoAction.class.getName()).log(Level.SEVERE, null, ex);
}
String t = str.toString();
final JInternalFrame internalFrame = new JInternalFrame("",true,true);
title=file.getName();
text.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
internalFrame.add(text);
i+=1;
internalFrame.setName("Doc "+i);
JScrollPane scrollpane=new JScrollPane(text);
internalFrame.setTitle(title);
tp.add(internalFrame);
internalFrame.add(scrollpane);
internalFrame.setVisible(true);
text.setText(t);
text.setCaretPosition(0);
text.getDocument().addUndoableEditListener(new UndoableEditListener() {
@Override
public void undoableEditHappened(UndoableEditEvent e) {
undoManager.addEdit(e.getEdit());
rollback.setEnabled(undoManager.canUndo());
redo.setEnabled(undoManager.canRedo());
}
});
}
}
private void rollbackActionPerformed(java.awt.event.ActionEvent evt) {
rollback.setEnabled(undoManager.canUndo());
if (undoManager.canUndo()) {
undoManager.undo();
}
rollback.setEnabled(undoManager.canUndo());
redo.setEnabled(undoManager.canRedo());
}
private void redoActionPerformed(java.awt.event.ActionEvent evt) {
redo.setEnabled(undoManager.canRedo());
if (undoManager.canRedo()) {
undoManager.redo();
}
rollback.setEnabled(undoManager.canUndo());
redo.setEnabled(undoManager.canRedo());
}
private void replaceActionPerformed(java.awt.event.ActionEvent evt) {
JDialog replace_dialog=new JDialog(UndoAndRedoAction.this);
replace_dialog.setTitle("Replace");
JLabel find_label=new JLabel("Find what");
final JTextField find_tf=new JTextField(10);
JLabel replace_label=new JLabel("Replace With");
final JTextField replace_tf=new JTextField(10);
JButton replaceAll=new JButton("Replace All");
replace_dialog.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.WEST;
replace_dialog.add(find_label, c);
c.gridx++;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
replace_dialog.add(find_tf, c);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
replace_dialog.add(replace_label, c);
c.gridx++;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
replace_dialog.add(replace_tf, c);
c.gridx++;
c.gridx++;
c.gridwidth = 1;
c.fill = GridBagConstraints.HORIZONTAL;
replace_dialog.add(replaceAll, c);
replace_dialog.setSize(400,400);
replace_dialog.setLocationRelativeTo(null);
replace_dialog.pack();
replace_dialog.setVisible(true);
replaceAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
text.setText(text.getText().replaceAll(find_tf.getText(), replace_tf.getText()));
}
});
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(UndoAndRedoAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(UndoAndRedoAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(UndoAndRedoAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(UndoAndRedoAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new UndoAndRedoAction().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenu fileMenu;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem open;
private javax.swing.JMenuItem redo;
private javax.swing.JMenuItem replace;
private javax.swing.JMenuItem rollback;
private javax.swing.JTabbedPane tp;
// End of variables declaration
}
公共类UndoAndedAction扩展了javax.swing.JFrame{
JTextArea文本;
int i=0;
UndoManager UndoManager=新的UndoManager();
公共行动{
初始化组件();
text=新的JTextArea();
rollback.setEnabled(错误);
redo.setEnabled(false);
}
@抑制警告(“未选中”)
//
私有组件(){
tp=newjavax.swing.JTabbedPane();
jMenuBar1=newjavax.swing.JMenuBar();
fileMenu=newjavax.swing.JMenu();
open=newjavax.swing.JMenuItem();
rollback=newjavax.swing.JMenuItem();
redo=newjavax.swing.JMenuItem();
replace=newjavax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
fileMenu.setText(“文件”);
open.setText(“open”);
open.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行的开放操作(evt);
}
});
文件菜单。添加(打开);
rollback.setText(“rollback”);
rollback.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行回滚操作(evt);
}
});
fileMenu.add(回滚);
重做。设置文本(“重做”);
redo.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
重做(evt);
}
});
fileMenu.add(重做);
替换。setText(“替换”);
replace.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行的替换操作(evt);
}
});
文件菜单。添加(替换);
jMenuBar1.add(文件菜单);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tp,javax.swing.GroupLayout.Alignment.TRAILING,javax.swing.GroupLayout.DEFAULT\u SIZE,400,Short.MAX\u值)
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tp,javax.swing.GroupLayout.Alignment.TRAILING,javax.swing.GroupLayout.DEFAULT\u SIZE,279,Short.MAX\u值)
);
包装();
}//
私有void openActionPerformed(java.awt.event.ActionEvent evt){
final JFileChooser jc=new JFileChooser();
int returnVal=jc.showOpenDialog(undoardoaction.this);
字符串标题;
File=null;
if(returnVal==JFileChooser.APPROVE_选项)
file=jc.getSelectedFile();
如果(jc.getSelectedFile()!=null){
BufferedReader br=null;
StringBuffer str=新的StringBuffer(“”);
试一试{
br=新的BufferedReader(新的文件读取器(文件));
弦线;
而((line=br.readLine())!=null){
str.append(第+行“\n”);
}
}
捕获(IOEX异常){
Logger.getLogger(undoardoaction.class.getName()).log(Level.SEVERE,null,ex);
}
字符串t=str.toString();
最终JInternalFrame internalFrame=新JInternalFrame(“”,true,true);
title=file.getName();
setFont(新java.awt.Font(“Miriam Fixed”,0,13));
添加(文本);
i+=1;
internalFrame.setName(“Doc”+i);
JScrollPane scrollpane=新的JScrollPane(文本);
internalFrame.setTitle(title);
tp.add(内部框架);
添加(滚动窗格);
internalFrame.setVisible(true);
text.setText(t);
text.setCaretPosition(0);
text.getDocument().addUndoableEditListener(新的UndoableEditListener()){
@凌驾
公共无效UndoableEditOccessed(UndoableEditEvent e){
undoManager.addEdit(例如getEdit());
rollback.setEnabled(undoManager.canUndo());
setEnabled(undoManager.canRedo());
}
});
}
}
private void rollbackActionPerformed(java.awt.event.ActionEvent evt){
rollback.setEnabled(undoManager.canUndo());
if(undoManager.canUndo()){
undoManager.undo();
}
rollback.setEnabled(undoManager.canUndo());
setEnabled(undoManager.canRedo());
}
private void redoActionPerformed(java.awt.event.ActionEvent evt){
setEnabled(undoManager.canRedo());
if(undoManager.canRedo()){
undoManager.redo();
}
rollback.setEnabled(undoManager.canUndo());
setEnabled(undoManager.canRedo());
}
private void replaceActionPerformed(java.awt.event.ActionEvent evt){
JDialog replace_dialog=newjdialog(undoardoaction.this);
替换对话框。设置标题(“替换”);
JLabel find_label=新JLabel(“查找内容”);
最终JTextField find_tf=新JTextField(10);
JLabel replace_标签=新JLabel(“替换为”);
最终JTextField替换_tf=新的JTextField(10);
JButton replaceAll=新JButton(“全部替换”);
替换_dialog.setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
c、 插图=新插图(5,5,5,5);
c、 gridx=0;
c、 gridy=0;
c、 非国大
public class UndoAndRedo extends javax.swing.JFrame {
int i = 0;
UndoManager undoManager = new UndoManager();
public UndoAndRedo() {
initComponents();
rollback.setEnabled(false);
redo.setEnabled(false);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
tp = new javax.swing.JTabbedPane();
jMenuBar1 = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
open = new javax.swing.JMenuItem();
rollback = new javax.swing.JMenuItem();
redo = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
fileMenu.setText("File");
open.setText("Open");
open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openActionPerformed(evt);
}
});
fileMenu.add(open);
rollback.setText("Rollback");
rollback.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rollbackActionPerformed(evt);
}
});
fileMenu.add(rollback);
redo.setText("Redo");
redo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
redoActionPerformed(evt);
}
});
fileMenu.add(redo);
jMenuBar1.add(fileMenu);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tp, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tp, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void openActionPerformed(java.awt.event.ActionEvent evt) {
final JFileChooser jc = new JFileChooser();
int returnVal = jc.showOpenDialog(UndoAndRedo.this);
String title;
File file = null;
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = jc.getSelectedFile();
}
JTextArea text = new JTextArea();
if (jc.getSelectedFile() != null) {
BufferedReader br = null;
StringBuffer str = new StringBuffer("");
try {
br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
str.append(line + "\n");
}
} catch (IOException ex) {
Logger.getLogger(UndoAndRedo.class.getName()).log(Level.SEVERE, null, ex);
}
String t = str.toString();
final JInternalFrame internalFrame = new JInternalFrame("", true, true);
title = file.getName();
text.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
i += 1;
internalFrame.setName("Doc " + i);
JScrollPane scrollpane = new JScrollPane(text);
internalFrame.setTitle(title);
tp.add(internalFrame);
internalFrame.add(scrollpane);
internalFrame.setVisible(true);
text.setText(t);
text.setCaretPosition(0);
text.getDocument().addUndoableEditListener(new UndoableEditListener() {
@Override
public void undoableEditHappened(UndoableEditEvent e) {
undoManager.addEdit(e.getEdit());
rollback.setEnabled(undoManager.canUndo());
redo.setEnabled(undoManager.canRedo());
}
});
}
}
private void rollbackActionPerformed(java.awt.event.ActionEvent evt) {
rollback.setEnabled(undoManager.canUndo());
if (undoManager.canUndo()) {
undoManager.undo();
}
rollback.setEnabled(undoManager.canUndo());
redo.setEnabled(undoManager.canRedo());
}
private void redoActionPerformed(java.awt.event.ActionEvent evt) {
redo.setEnabled(undoManager.canRedo());
if (undoManager.canRedo()) {
undoManager.redo();
}
rollback.setEnabled(undoManager.canUndo());
redo.setEnabled(undoManager.canRedo());
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(UndoAndRedo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(UndoAndRedo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(UndoAndRedo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(UndoAndRedo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new UndoAndRedo().setVisible(true);
}
});
}
private javax.swing.JMenu fileMenu;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem open;
private javax.swing.JMenuItem redo;
private javax.swing.JMenuItem rollback;
private javax.swing.JTabbedPane tp;
}