Java:防止按键时发出哔哔声
Posted
技术标签:
【中文标题】Java:防止按键时发出哔哔声【英文标题】:Java: prevent beep sound on key press 【发布时间】:2010-12-01 20:16:41 【问题描述】:我有一个从浏览器启动的 Java 程序,它使用各种 java.awt.Button 按钮运行。点进去之后就好了。但是当按下与该按钮对应的键时,Windows 会发出“默认哔”声。
按钮是这样创建的:
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.io.*;
import java.awt.Panel;
import java.awt.Button;
import java.awt.Font;
import java.awt.event.*;
import java.lang.Integer;
public class buttonPne extends Panel implements ActionListener, constants
private pClient parent = null;
private labelPne buttonLabel[] = new labelPne[8];
private Panel buttonPanel[] = new Panel[8];
public Button theButtons[] = new Button[8];
private boolean buttonStatus[] = new boolean[8];
public buttonPne(pClient c)
parent = c;
this.setLayout(new GridLayout(1, 8, 1, 0));
for (int i = 0; i < 8; i++)
buttonLabel[i] = new labelPne(parent);
buttonLabel[i].setSize(9, 9);
theButtons[i] = new Button();
theButtons[i].setFont(ButtonFont);
theButtons[i].setActionCommand(Integer.toString(i));
theButtons[i].addActionListener(this);
theButtons[i].addKeyListener(parent);
buttonPanel[i] = new Panel();
buttonPanel[i].setBackground(backgroundColor);
buttonPanel[i].setLayout(new BorderLayout());
buttonPanel[i].add("North", theButtons[i]);
buttonPanel[i].add("South", buttonLabel[i]);
this.add(buttonPanel[i]);
Deactivate();
按键的处理方式如下:
import java.awt.event.KeyEvent;
...
public class pClient extends Applet implements KeyListener, constants
...
f.addKeyListener(this);
...
public void keyPressed(KeyEvent evt)
int theKey;
theKey = evt.getKeyCode();
switch(theKey)
case KeyEvent.VK_1:
case KeyEvent.VK_F1:
buttons.DoButton(0);
break;
...
public void DoButton(int theNumber)
if (buttonStatus[theNumber])
if (parent.pollSendFlag(BUTTONS))
parent.chat.takeFocus();
parent.compass.Deactivate();
Deactivate();
return;
如何阻止按键时出现默认蜂鸣声?
【问题讨论】:
【参考方案1】:使用 Swing 组件而不是 AWT 组件。
【讨论】:
以上是关于Java:防止按键时发出哔哔声的主要内容,如果未能解决你的问题,请参考以下文章
在 removeAllRanges() 上 Chrome 发出哔哔声;