当在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();
}
}
答案 0 :(得分:0)
我不知道是否有更好的方法,但一种方法是制作一个自定义的外观和感觉,完全禁用哔哔声。这样可以达到预期的效果,但也可以禁用整个程序的蜂鸣声,而不仅仅是微调器。
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的答案。