我用这种方式用SpinnerNumberModel创建了JSpinner:
public class SpinnerFrameDoesntWork extends JFrame {
public SpinnerFrameDoesntWork () {
super("Spinner doesnt work");
setSize(400, 200);
init();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
}
private void init() {
final SpinnerNumberModel countSpinnerModel = new SpinnerNumberModel();
final JSpinner countSpinner = new JSpinner(countSpinnerModel);
countSpinnerModel.setValue((short)6);
countSpinnerModel.setMinimum((short)6);
countSpinnerModel.setMaximum((short)12);
countSpinnerModel.setStepSize((short)1);
add(countSpinner);
}
}
我注意到,尽管微调器显示正确,但它不允许更改它的值,它是堆叠在" 6"
在排除故障时,我注意到,以下这两项工作正常:
用int而不是短裤:
public class SpinnerDoesWork1 extends JFrame {
public SpinnerDoesWork1 () {
super("Spinner does work 1");
setSize(400, 200);
init();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
}
private void init() {
final SpinnerNumberModel countSpinnerModel = new SpinnerNumberModel();
final JSpinner countSpinner = new JSpinner(countSpinnerModel);
countSpinnerModel.setValue(6);
countSpinnerModel.setMinimum(6);
countSpinnerModel.setMaximum(12);
countSpinnerModel.setStepSize(1);
add(countSpinner);
}
}
更改对象初始化顺序:
public class SpinnerDoesWork2 extends JFrame {
public SpinnerDoesWork2 () {
super("Spinner does work 2");
setSize(400, 200);
init();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
}
private void init() {
final SpinnerNumberModel countSpinnerModel = new SpinnerNumberModel();
countSpinnerModel.setValue((short)6);
countSpinnerModel.setMinimum((short)6);
countSpinnerModel.setMaximum((short)12);
countSpinnerModel.setStepSize((short)1);
final JSpinner countSpinner = new JSpinner(countSpinnerModel);
add(countSpinner);
}
}
我设法让它发挥作用,但我不知道第一个例子有什么问题?为什么它也不能正常工作?
感谢您的帮助!