Java: 如何禁用 JSpinner 蜂鸣声

Java: How to disable JSpinner beeping

当在 JSpinner 中输入无效输入时,会发出哔哔声,我不知道如何禁用它。

我正在使用不允许输入无效输入的数字微调器,如下所示:

public class SpinnerTester {

    public static void main(String[] args) {

        JSpinner spinner = new JSpinner(new SpinnerNumberModel(1, 0, 100, 1));


        //disable invalid input from being typed into spinner
        JFormattedTextField textField = ((JSpinner.NumberEditor) spinner.getEditor()).getTextField();
        ((NumberFormatter) textField.getFormatter()).setAllowsInvalid(false);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(spinner);
        frame.setVisible(true);
        frame.pack();
    }

}

我不知道是否有更好的方法,但一种方法是制作自定义外观以完全禁用蜂鸣声。这达到了预期的效果,但也禁用了整个程序的蜂鸣声,而不仅仅是微调器。

public class SpinnerTester {

    public static void main(String[] args) {

        JSpinner spinner = new JSpinner(new SpinnerNumberModel(1, 0, 100, 1));


        //disable invalid input from being typed into spinner
        JFormattedTextField textField = ((JSpinner.NumberEditor) spinner.getEditor()).getTextField();
        ((NumberFormatter) textField.getFormatter()).setAllowsInvalid(false);

        /**
         * Change look and field
         */
        try {
            UIManager.setLookAndFeel(new MyLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(spinner);
        frame.setVisible(true);
        frame.pack();

    }

    /**
     * Create Look and Feel without beeps
     */
    public static class MyLookAndFeel extends NimbusLookAndFeel {
        @Override
        public void provideErrorFeedback(Component component) {
            //super.provideErrorFeedback(component);
        }
    }
}

基于对 this question 的回答。