gpt4 book ai didi

java - Java 中可撤消的文本区域

转载 作者:行者123 更新时间:2023-12-01 21:12:10 24 4
gpt4 key购买 nike

我刚刚接触java编程...我想创建一个小型文本编辑器(Windows记事本克隆...)。

我正在寻找一个扩展 JTextArea 的类,实现撤消和重做操作。我找到了符合我需求的代码,并尝试将其调整为适合我的目的。

代码如下:

import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JTextArea;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;

class UndoableTextArea extends JTextArea implements UndoableEditListener, FocusListener, KeyListener

{

private static final long serialVersionUID = 1L;
private UndoManager m_undoManager;

public UndoableTextArea() {
this(new String());
}

public UndoableTextArea(String text) {
super(text);
getDocument().addUndoableEditListener(this);
this.addKeyListener(this);
this.addFocusListener(this);
}

private void createUndoMananger() {
m_undoManager = new UndoManager();
m_undoManager.setLimit(50);
}

private void removeUndoMananger() {
m_undoManager.end();
}

public void focusGained(FocusEvent fe) {
createUndoMananger();
}

public void focusLost(FocusEvent fe) {
removeUndoMananger();
}

public void undo()
{
try {
m_undoManager.undo();
} catch (CannotUndoException cue) {
Toolkit.getDefaultToolkit().beep();
}
}

public void redo()
{
try {
m_undoManager.redo();
} catch (CannotRedoException cue) {
Toolkit.getDefaultToolkit().beep();
}
}

public void undoableEditHappened(UndoableEditEvent e) {
m_undoManager.addEdit(e.getEdit());
}

public void keyPressed(KeyEvent e) {
if ((e.getKeyCode() == KeyEvent.VK_Z) && (e.isControlDown())) {
try {
m_undoManager.undo();
} catch (CannotUndoException cue) {
Toolkit.getDefaultToolkit().beep();
}
}

if ((e.getKeyCode() == KeyEvent.VK_Y) && (e.isControlDown())) {
try {
m_undoManager.redo();
} catch (CannotRedoException cue) {
Toolkit.getDefaultToolkit().beep();
}
}
}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
}
}

在输入对象时使用加速键CTRL+ZCTRL+Y,没有问题....但是我添加的用于使用 menuItem 对象中的此操作的方法 undo()redo() 不起作用...

(...)       
menuItem15 = new JMenuItem("Undo");
menuItem16 = new JMenuItem("Redo");
(...)



public void actionPerformed(ActionEvent e)
{

//Undo
if (e.getSource() == menuItem15)
{
textArea1.undo();
}


//Redo
if (e.getSource() == menuItem16)
{
textArea1.redo();
}
}

有什么建议吗?抱歉我的英语不好......

谢谢!

最佳答案

问题可能是由于在 focusGained 和 focusLost 事件上添加和删除 UndoManager 的代码所致:

public void focusGained(FocusEvent fe) {
createUndoMananger();
}

public void focusLost(FocusEvent fe) {
removeUndoMananger();
}

当您单击菜单时,文本区域暂时失去焦点,随后 UndoManager 被丢弃,然后创建一个新实例。新实例不知道以前的可撤消编辑事件,因此撤消和重做不起作用。

我建议创建一次 UndoManager 并将其附加到文档,直到 GUI 被处理(即直到对话框/窗口关闭)。

关于java - Java 中可撤消的文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40908044/

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