取消JSpinner的键盘编辑能力

  // Create a nummber spinner
    JSpinner spinner = new JSpinner();
    
    // Disable keyboard edits in the spinner
    JFormattedTextField tf =
        ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField();
    tf.setEditable(false);
    
    // The previous call sets the background to a disabled
    // color (usually gray). To change this disabled color,
    // reset the background color.
    tf.setBackground(Color.white);
    
    // The value of the spinner can still be programmatically changed
    spinner.setValue(new Integer(100));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSpinner是一个Swing组件,用于显示和编辑数字或日期值。它可以让用户通过鼠标或键盘输入来改变值,也可以通过代码来设置它的值。 以下是JSpinner的基本用法: 1. 创建JSpinner对象 使用JSpinner的构造函数来创建一个JSpinner对象,例如: ``` JSpinner spinner = new JSpinner(); ``` 2. 设置SpinnerModel 使用SpinnerModel来设置JSpinner的值范围和步长。可以使用SpinnerNumberModel、SpinnerListModel和SpinnerDateModel等现成的模型,也可以创建自定义的SpinnerModel。例如: ``` SpinnerModel model = new SpinnerNumberModel(0, 0, 100, 1); JSpinner spinner = new JSpinner(model); ``` 这里创建了一个初始值为0,最小值为0,最大值为100,步长为1的JSpinner。 3. 添加JSpinner到容器 将JSpinner添加到Swing容器中,例如: ``` JPanel panel = new JPanel(); panel.add(spinner); ``` 4. 获取和设置JSpinner的值 使用getValue()方法获取JSpinner的当前值,使用setValue(Object value)方法设置JSpinner的值,例如: ``` Object value = spinner.getValue(); spinner.setValue(10); ``` 5. 监听JSpinner的变化 可以添加ChangeListener来监听JSpinner值的变化,例如: ``` spinner.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { Object value = spinner.getValue(); System.out.println("JSpinner value changed to: " + value); } }); ``` 这里为JSpinner添加了一个ChangeListener,当JSpinner的值发生变化时,会输出一个消息。 以上就是JSpinner的基本用法。JSpinner还有很多高级用法,例如自定义Editor、Renderer和Model等,可以根据需要进行学习和使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值