我创建了一个静态类,可以立即将常规弹出菜单添加到文本字段中...剪切,复制,粘贴和选择所有工作,只需撤消不...
任何人都可以帮我修复那些无效的部分(撤消)吗?
这是班级:
import javax.swing.*;
import java.awt.event.ActionEvent;
import javax.swing.JPopupMenu;
import javax.swing.undo.*;
public class JTextFieldRegularPopupMenu {
public static void addTo(JTextField txtField)
{
JPopupMenu popup = new JPopupMenu();
Action undoAction = new AbstractAction("Undo") {
@Override
public void actionPerformed(ActionEvent ae) {
UndoManager undoManager = new UndoManager();
txtField.getDocument().addUndoableEditListener(undoManager);
if (undoManager.canUndo()) {
undoManager.undo();
}
else {
JOptionPane.showMessageDialog(null,
"Undoable: " + undoManager.canUndo() ,
"Undo Status",
JOptionPane.INFORMATION_MESSAGE);
}
}
};
Action copyAction = new AbstractAction("Copy") {
@Override
public void actionPerformed(ActionEvent ae) {
txtField.copy();
}
};
Action cutAction = new AbstractAction("Cut") {
@Override
public void actionPerformed(ActionEvent ae) {
txtField.cut();
}
};
Action pasteAction = new AbstractAction("Paste") {
@Override
public void actionPerformed(ActionEvent ae) {
txtField.paste();
}
};
Action selectAllAction = new AbstractAction("Select All") {
@Override
public void actionPerformed(ActionEvent ae) {
txtField.selectAll();
}
};
popup.add (undoAction);
popup.addSeparator();
popup.add (cutAction);
popup.add (copyAction);
popup.add (pasteAction);
popup.addSeparator();
popup.add (selectAllAction);
txtField.setComponentPopupMenu(popup);
}
}
希望找到解决这个看似简单问题的方法......