首页javajspinnerJava Swing - 如何禁用JSpinner中的键盘编辑

Java Swing - 如何禁用JSpinner中的键盘编辑

我们想知道如何禁用JSpinner中的键盘编辑。
import java.awt.Color;

import javax.swing.JFormattedTextField;
import javax.swing.JSpinner;

public class Main {
  public static void main(String[] argv) throws Exception {
    JSpinner spinner = new JSpinner();

    // Disable keyboard edits in the spinner
    JFormattedTextField tf = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
    tf.setEditable(false);
    tf.setBackground(Color.white);

    // The value of the spinner can still be programmatically changed
    spinner.setValue(new Integer(100));
  }
}