Java:防止按键时发出蜂鸣声

发布于 2024-10-05 06:55:26 字数 2320 浏览 9 评论 0原文

我有一个从浏览器启动的 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;
}

如何阻止按键时发生默认蜂鸣声?

I have a Java program which is launched from a browser, running with various java.awt.Button buttons. When clicked, it's fine. But when a key is pressed that corresponds to that button, Windows makes the "Default Beep" sound.

The buttons are created like this:

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();
    }

The key presses are handled like this:

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;
}

How can I stop the default beep from happening on key presses?

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

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

发布评论

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

评论(1

绝對不後悔。 2024-10-12 06:55:26

使用 Swing 组件而不是 AWT 组件。

Use Swing components instead of AWT components.

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