- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
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/
//java swing 新手,需要帮助获取第一个 Jtextfield 中的文本以显示在//第二个 jtextfield 中???我很年轻,刚刚开始接触java,需要一些帮助。下面是我已经完成的代码
我正在制作一个用户界面,其中有两个 jtextfields。我想要的是当用户输入第一个jtextfield 它应该以 1 的增量镜像在第二个 jtextfield 中,并且如果用户在第二个 Jtext
我正在设计许可证激活部分。它在 Java Swing 中有两个文本字段。 Java代码: jTextField1.setDocument(new JTextFieldLimit(8)); jTex
我需要将文本从 JFrame 中的 JTextField 传递到另一个 JFrame 中的第二个 JTextField。 GUI 是使用 GUI Editor netbeans 6.9 创建的。 最佳
在从另一个 JTextField 读取一个 String 后,在另一个类中更新一个 JTextField 时遇到一些问题。这是有问题的方法: public JTextField buyVowel(pl
我正在制作身份验证 GUI,它应该包含 2 个文本字段,用户名 JTextField 和密码 JPasswordField。我想让密码字段位于用户名字段下方。我目前的代码如下: public clas
当鼠标在该文本字段中单击时,我需要让该程序清除文本字段中的文本。我尝试了一些方法,但还没有一个对我有用。 完整代码如下: public class TimerClassPanel extends JF
我目前正在学习如何在 Java 中使用 Swing。将 JTextFields、JLabels 或 JButtons 添加到 JPanel 时,我通常用 4 行来完成,如下所示: gbc.gridx
我不确定之前是否已经回答过这个问题;我尝试搜索看看是否有,但没有成功。 我正在做一项家庭作业,并且必须有两个 JTextField:一个接受输入,第二个反转显示给定的输入。例如,如果有人输入 1234
如何将内容从一个 JTextField 逐个字符复制到另一个不可编辑的 JTextField? 第一个 JTextField 中输入的每个字符都应动态显示在第二个 JTextField 中。 最佳答案
我想找到jtextfields(sht1到sht13)的总和并将结果放入llog jtextfield中,但如果我不在每个jtextfield(sht1到sht13)中放入值,它就不起作用 我如何修复
如何验证文本字段在 Swing 中仅输入 10 位数字的手机号码? 我有三个文本字段。 姓名 联系方式 没有。对于电子邮件 我想点击提交按钮,然后检查姓名、联系方式和电子邮件是否正确。我还想设置字符限
我已将 JPopupMenu 添加到 JTextField。不幸的是,当我从 JTextField 更改焦点时,或者当我“最小化”窗口时,JPopupMenu 仍然可见。如果 JTextField 失
我的 JTextField 遇到问题。我不知道如何将变量从 JTextField (位于 JFrame A 中)传递到另一个 JTextField (位于 JFrame B 中)。我尝试这样做,但它没
我正在用java创建一个小程序,用户在四个不同的四个不同文本字段中输入一些数据,单击一个按钮,然后从这些字段中获取数据并将其存储在旁边的文本字段中。 我制作了一个按钮,它实现了 ActionListe
以下是多文件项目的部分代码。我只发布这段代码,因为我不清楚为什么 setText 方法不起作用。并未包含此项目的所有代码。 import javax.swing.*;
大家好,我有一个连接到 Oracle 数据库的 Swing 应用程序,我希望一旦我在 JTextField 中键入一个值,JFrame 上的其他 JTextfield 就会加载来自数据库的后续数据,但
我有一个名为 Name 的 JTextField 和一个名为 Address 的 JTextArea。我的要求是,当用户在 JTextField 中输入名称并按 Enter 键时,光标应转到下一个文本
在 Netbeans 中,每当 JTextField、JButton 获得焦点时,边框就会被蓝色突出显示覆盖,我如何更改其颜色或完全删除它? 最佳答案 您可以通过右键单击设计 View 中的组件,选择
double B=Double.parseDouble(emp_txt2.getText()); double C=Double.parseDouble(nopay_txt3.getText(
我是一名优秀的程序员,十分优秀!