Java:在按键时防止发出哔哔声

时间:2010-12-01 20:16:42

标签: java beep

我有一个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 个答案:

答案 0 :(得分:0)

使用Swing组件代替AWT组件。