Java 在JFormattedTextField中禁用蜂鸣音
我想在JFormattedTextField中禁用蜂鸣音,但我有一些限制Java 在JFormattedTextField中禁用蜂鸣音,java,swing,jformattedtextfield,Java,Swing,Jformattedtextfield,我想在JFormattedTextField中禁用蜂鸣音,但我有一些限制 我只能访问JFormattedTextField实例。我无法创建新的子类 我无法更改系统设置。程序的其他部分可能需要嘟嘟声 您应该能够使用本期提出的相同解决方案禁用它: 即使目标事件不同,目标对象的beed操作也将被完全禁用 我想在JFormattedTextField中禁用蜂鸣音 不太具体。您试图禁用“嘟嘟”的原因是什么?例如,我知道在一个文本字段中,当你按下退格按钮时,你会听到“嘟嘟”的声音,而你已经在文本字段的开头了
您应该能够使用本期提出的相同解决方案禁用它: 即使目标事件不同,目标对象的beed操作也将被完全禁用 我想在JFormattedTextField中禁用蜂鸣音 不太具体。您试图禁用“嘟嘟”的原因是什么?例如,我知道在一个文本字段中,当你按下退格按钮时,你会听到“嘟嘟”的声音,而你已经在文本字段的开头了 在这种情况下,文本组件使用键绑定来执行操作。所以backspace键调用“backspace”动作。有关列出所有键绑定的程序,请参阅 因此,如果您想禁用“嘟嘟”声,那么您需要自定义操作。下面我复制了(从DefaultEditorKit)用于删除文本组件中的字符的操作。我做了一些更改以禁用蜂鸣音:
static class MyDeletePrevCharAction extends TextAction {
/**
* Creates this object with the appropriate identifier.
*/
MyDeletePrevCharAction() {
//super(deletePrevCharAction);
super(DefaultEditorKit.deletePrevCharAction);
}
/**
* The operation to perform when this action is triggered.
*
* @param e the action event
*/
public void actionPerformed(ActionEvent e) {
JTextComponent target = getTextComponent(e);
boolean beep = true;
if ((target != null) && (target.isEditable())) {
try {
Document doc = target.getDocument();
Caret caret = target.getCaret();
int dot = caret.getDot();
int mark = caret.getMark();
if (dot != mark) {
doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
beep = false;
} else if (dot > 0) {
int delChars = 1;
if (dot > 1) {
String dotChars = doc.getText(dot - 2, 2);
char c0 = dotChars.charAt(0);
char c1 = dotChars.charAt(1);
if (c0 >= '\uD800' && c0 <= '\uDBFF' &&
c1 >= '\uDC00' && c1 <= '\uDFFF') {
delChars = 2;
}
}
doc.remove(dot - delChars, delChars);
beep = false;
}
} catch (BadLocationException bl) {
}
}
if (beep) {
//UIManager.getLookAndFeel().provideErrorFeedback(target);
System.out.println("beep");
}
}
}
你试过这个问题的第二个答案吗?我想禁用无效输入的蜂鸣音(主要是)和可能导致蜂鸣音的任何其他操作。如果我按照您的方式操作,是否需要对每个无效输入执行操作?这将是大量的代码…@user2628641
我想禁用无效输入的蜂鸣音(主要)
-我想您需要查看DefaultEditorKit.insertcontentation
。我不知道您还需要覆盖哪些其他操作。
static class MyDeletePrevCharAction extends TextAction {
/**
* Creates this object with the appropriate identifier.
*/
MyDeletePrevCharAction() {
//super(deletePrevCharAction);
super(DefaultEditorKit.deletePrevCharAction);
}
/**
* The operation to perform when this action is triggered.
*
* @param e the action event
*/
public void actionPerformed(ActionEvent e) {
JTextComponent target = getTextComponent(e);
boolean beep = true;
if ((target != null) && (target.isEditable())) {
try {
Document doc = target.getDocument();
Caret caret = target.getCaret();
int dot = caret.getDot();
int mark = caret.getMark();
if (dot != mark) {
doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
beep = false;
} else if (dot > 0) {
int delChars = 1;
if (dot > 1) {
String dotChars = doc.getText(dot - 2, 2);
char c0 = dotChars.charAt(0);
char c1 = dotChars.charAt(1);
if (c0 >= '\uD800' && c0 <= '\uDBFF' &&
c1 >= '\uDC00' && c1 <= '\uDFFF') {
delChars = 2;
}
}
doc.remove(dot - delChars, delChars);
beep = false;
}
} catch (BadLocationException bl) {
}
}
if (beep) {
//UIManager.getLookAndFeel().provideErrorFeedback(target);
System.out.println("beep");
}
}
}
textField.getActionMap()
.put(DefaultEditorKit.deletePrevCharAction, new MyDeletePrevCharAction());