简体   繁体   English

disable int spinner plus(only)按钮

[英]disable int spinner plus (only) button

The code below works great for when the user assigns the total amount of stat points I allocate. 当用户分配我分配的统计点总数时,下面的代码非常有用。 However, I would love to only disable the plus button, so they could lower a stat value and then add again. 但是,我希望只禁用加号按钮,这样他们就可以降低统计值,然后重新添加。

if ((strengthModel.getNumber().intValue()) + (constitutionModel.getNumber().intValue()) + (dexterityModel.getNumber().intValue()) + (intelligenceModel.getNumber().intValue()) > 49){
     strengthSpinner.setEnabled(false);
     constitutionSpinner.setEnabled(false);
     dexteritySpinner.setEnabled(false);
     intelligenceSpinner.setEnabled(false);
}

Is that possible with int spinners? int旋转器有可能吗? I did not see it in the docs. 我没有在文档中看到它。

EDIT Bit more info: You can spread your stat points around or assign them all to one stat. 编辑更多信息:您可以传播您的统计点或将它们全部分配到一个统计。 The max on each of the models is all 10 of the un-used points. 每个模型的最大值都是10个未使用的点。

For anyone who finds this thread here is how I solved my issue: 对于在这里找到这个主题的人来说,我是如何解决我的问题的:

public void stateChanged(ChangeEvent e) {
    Component[] components = characterCreationPanel.getComponents();
    Component component = null; 
    strengthValue = strengthModel.getNumber().intValue();
    constitutionValue = constitutionModel.getNumber().intValue();
    dexterityValue = dexterityModel.getNumber().intValue();
    intelligenceValue = intelligenceModel.getNumber().intValue();
    for (int i = 0; i < components.length; i++)
    {
        component = components[i];
        if (component instanceof JLabel){
            if (((JLabel) component).getText().substring(0, 5).equals("Stat ")){
                ((JLabel) component).setText("Stat Points Left: " + Integer.toString(50 - (strengthValue + constitutionValue + dexterityValue + intelligenceValue)));
                if ((strengthValue + constitutionValue + dexterityValue + intelligenceValue) == 50){
                    System.out.println("Hit your cap.");
                }
            }       
        }
        strengthModel.setMaximum(50 - (constitutionValue + dexterityValue + intelligenceValue));
        constitutionModel.setMaximum(50 - (strengthValue + dexterityValue + intelligenceValue));
        dexterityModel.setMaximum(50 - (strengthValue + constitutionValue + intelligenceValue));
        intelligenceModel.setMaximum(50 - (strengthValue + constitutionValue + dexterityValue));
    }
}

Thanks "ziesemer" for the tip with the setMaximium. 感谢“ziesemer”获得了setMaximium的提示。

Create your JSpinner with a SpinnerNumberModel with the desired minimum and maximum values. 使用具有所需最小值和最大值的SpinnerNumberModel创建JSpinner。

Add a change listener to each model, such that as each spinner is changed, the sum of all your spinners is calculated, and then the maxiumum on each spinner set to its current value to disable the plus button if necessary. 为每个模型添加一个更改侦听器,以便在更改每个微调器时,计算所有微调器的总和,然后将每个微调器的maxiumum设置为其当前值,以便在必要时禁用加号按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM