gpt4 book ai didi

java - 在 JTextField 和 setText 中撤消

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:55:29 25 4
gpt4 key购买 nike

JTextField 具有开箱即用的撤消支持。它适用于用户交互,但不幸的是,如果调用方法 setText(String str),结果是两个 UndoableEdits 而不是一个。所以这段代码看起来和感觉都很好但不起作用:

UndoManager undoManager = new UndoManager();
JTextField tf = new JTextField();
tf.setText("initial value");
tf.getDocument().addUndoableEditListener(undoManager);
tf.setText("new value");
undoManager.undo();
System.out.println(tf.getText()); // Prints empty string
undoManager.undo();
System.out.println(tf.getText()); // Prints "initial value" as expected

JTextField 能否以某种方式处理 setText() 作为唯一一个 UndoableEdit?

最佳答案

另一种选择是覆盖 Document#replace(...)

import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.undo.*;
import javax.swing.event.*;

public class ReplaceUndoableEditDemo {
private final UndoManager um = new UndoManager();
private final JTextField tf = new JTextField(24);
private final UndoManager undoManager = new UndoManager();
private final JTextField field = new JTextField(24);
private final Document doc = new PlainDocument() {
@Override public void replace(
int offset, int length, String text, AttributeSet attrs)
throws BadLocationException {
undoManager.undoableEditHappened(new UndoableEditEvent(
this, new ReplaceUndoableEdit(offset, length, text)));
replaceIgnoringUndo(offset, length, text, attrs);
}
private void replaceIgnoringUndo(
int offset, int length, String text, AttributeSet attrs)
throws BadLocationException {
for(UndoableEditListener uel: getUndoableEditListeners()) {
removeUndoableEditListener(uel);
}
super.replace(offset, length, text, attrs);
for(UndoableEditListener uel: getUndoableEditListeners()) {
addUndoableEditListener(uel);
}
}
class ReplaceUndoableEdit extends AbstractUndoableEdit {
private final String oldValue;
private final String newValue;
private int offset;
public ReplaceUndoableEdit(int offset, int length, String newValue) {
String txt;
try {
txt = getText(offset, length);
} catch(BadLocationException e) {
txt = null;
}
this.oldValue = txt;
this.newValue = newValue;
this.offset = offset;
}
@Override public void undo() throws CannotUndoException {
try {
replaceIgnoringUndo(offset, newValue.length(), oldValue, null);
} catch(BadLocationException ex) {
throw new CannotUndoException();
}
}
@Override public void redo() throws CannotRedoException {
try {
replaceIgnoringUndo(offset, oldValue.length(), newValue, null);
} catch(BadLocationException ex) {
throw new CannotUndoException();
}
}
@Override public boolean canUndo() {
return true;
}
@Override public boolean canRedo() {
return true;
}
}
};
public JComponent makeUI() {
tf.getDocument().addUndoableEditListener(um);
doc.addUndoableEditListener(undoManager);
field.setDocument(doc);
field.setText("aaaaaaaaa");
tf.setText("default");
JPanel p = new JPanel();
p.add(tf);
p.add(field);
p.add(new JButton(new AbstractAction("undo") {
@Override public void actionPerformed(ActionEvent e) {
try {
undoManager.undo();
um.undo();
} catch(Exception ex) {
java.awt.Toolkit.getDefaultToolkit().beep();
}
}
}));
p.add(new JButton(new AbstractAction("redo") {
@Override public void actionPerformed(ActionEvent e) {
try {
undoManager.redo();
um.redo();
} catch(Exception ex) {
java.awt.Toolkit.getDefaultToolkit().beep();
}
}
}));
p.add(new JButton(new AbstractAction("setText") {
@Override public void actionPerformed(ActionEvent e) {
String str = new Date().toString();
tf.setText(str);
field.setText(str);
}
}));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new ReplaceUndoableEditDemo().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

关于java - 在 JTextField 和 setText 中撤消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12844520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com