Java Swing - 如何在任何 JOptionPane 之前发出蜂鸣声?
每当我在 Swing 应用程序中显示 JOptionPane 时,我都会在它之前发出蜂鸣声,如下所示:
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog( myFrame, "Message", "Title", JOptionPane.INFORMATION_MESSAGE );
是否有办法将第一行自动应用于任何 JOptionPane,以防我忘记在代码中编写它?
Whenever i show a JOptionPane in my Swing application i fire a beep before it like this :
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog( myFrame, "Message", "Title", JOptionPane.INFORMATION_MESSAGE );
Is there a way to apply the first line automatically to any JOptionPane in case i forgot to write it in code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建自己的类,该类具有静态方法
showMessageDialogAndBeep()
,该方法调用JOptionPane.showMessageDialog
并在之前发出蜂鸣声。You could create your own class which has a static method
showMessageDialogAndBeep()
which callsJOptionPane.showMessageDialog
and beeps before.void showMessageDialog(Component pC, Object m, String t, int mT)
{
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(pC, m,t,mT);
}
void showMessageDialog(Component pC, Object m, String t, int mT)
{
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog( pC, m,t,mT);
}
我同意 org.life.java 和 atamanroman 的观点。
此外,我可以向您提出以下建议。创建 AWTListener 并使用注册它
我认为这个侦听器在很多情况下都会被调用,包括打开对话框。因此,您只需识别这种情况并调用
beep()
即可。我没有尝试过,但我相信它会起作用。I agree with org.life.java and atamanroman.
Additionally I can suggest you the following. Create AWTListener and register it using
I think that this listener will be called in many cases including dialog opening. So you just have to recognize the case and call
beep()
. I did not try this but I believe it will work.