Java:完全禁用任何 Swing 不需要的蜂鸣声的方法?

发布于 2024-08-21 18:46:13 字数 499 浏览 5 评论 0原文

我正在使用 Swing 开发一个相当复杂的 Java 应用程序。

在某些情况下,无需任何用户干预就会发出不需要的蜂鸣声。没有崩溃,应用程序保持正常工作,我尊重 EDT 规则等。

但在某些情况下,可以听到蜂鸣声:我可能做了一些愚蠢的事情,触发了蜂鸣声,但无论如何,这不是用户操作,因为它可能发生当用户离开时导入数据。

对于一个永远不应该发出任何声音的 Java 应用程序,是否有可能通过为整个应用程序设置一个属性来配置它:“永远不要发出蜂鸣声”?

我一直在谷歌上搜索这个问题,我发现了有同样问题但没有答案的人的消息:我发现的只是一些黑客说 JEditorPane 存在一个已知问题并且使用 putProperty("IgnoreCharsetDirective", Boolean.TRUE) 有助于减少不必要的蜂鸣声发生。然而,关于这个主题的信息非常匮乏。

这是一个真正的问题,因为该应用程序是在计算机上需要声音的环境中使用的,但这个 Java 应用程序发出的噪音是不可接受的。

I am working on a fairly complex Java application using Swing.

On some occasions there are unwanted beeps without any user intervention. No crash, application keeps working fine, I'm respecting the EDT rules etc.

Yet in some cases a beep can be heard: I may be doing something silly triggering that beep but in any case it s not a user action for it can happen upon importing data, when the user is away.

Is it possible, for a Java application that should never ever emit any sound to configure it, say by setting a property for the whole application that says: "dont' ever emit a beep"?

I ve been Googling for that issue and I ve found message by people having the same issue but no answer: all I found was some hack saying that there was a known issue with JEditorPane and that using a putProperty("IgnoreCharsetDirective", Boolean.TRUE) was helpful to make unwanted beeps happen less often. Yet information is very scarce on the subject.

It s a real issue because the application is used in an environment where the sound is needed on the computer, but this Java application emitting noise is unacceptable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤者何惧 2024-08-28 18:46:13

您的问题已在 Java 论坛上讨论:

// Write a custom toolkit
public class MyToolkit extends sun.awt.windows.WToolkit 
{
  public void beep() {
  }
}

// Set this property
System.setProperty("awt.toolkit", "MyPackage.MyToolkit");

注意: 不鼓励使用此解决方法。您仍然应该尝试找到问题的根源。


编辑:删除了一个链接,因为 Java 论坛上的线程现已离线。

Your problem is discussed on the Java Forum:

// Write a custom toolkit
public class MyToolkit extends sun.awt.windows.WToolkit 
{
  public void beep() {
  }
}

// Set this property
System.setProperty("awt.toolkit", "MyPackage.MyToolkit");

NOTE: The use of this workaround is discouraged. You should still try to find the root of the problem.


Edit: Removed a link, since the thread on Java Forum is now offline.

红颜悴 2024-08-28 18:46:13

在 Swing 中,您需要按如下方式重写 LookAndFeel

UIManager.setLookAndFeel(new NimbusLookAndFeel() {

  @Override
  public void provideErrorFeedback(Component component) {

    // Your beep decision goes here

    // You want error feedback 
    super.provideErrorFeedback(component);

  }
});

通常,您的蜂鸣决定将引用应用程序的某种外部配置/首选项标志。

In Swing you need to override the LookAndFeel as follows:

UIManager.setLookAndFeel(new NimbusLookAndFeel() {

  @Override
  public void provideErrorFeedback(Component component) {

    // Your beep decision goes here

    // You want error feedback 
    super.provideErrorFeedback(component);

  }
});

Typically your beep decision would reference some kind of external configuration/preferences flag for your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文