链接的JSliders具有最大组合值 [英] Linked JSliders With Maximum Combined Value

查看:119
本文介绍了链接的JSliders具有最大组合值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最好的方法是链接JSliders为合并值设置上限?

What's the best way to 'link' JSliders to put a cap on the combined value?

想象一下用户可以指定速度等角色特征的格斗游戏,敏捷,耐力和准确性。但是他给用户分配了100分,但所有滑块的总值不得超过100.也就是说,滑块不应允许这样的增加。

Imagine a fighting game where the user can specify character traits like speed, agility, endurance and accuracy. The user is given 100 points to allocate however he chooses, but the total value of all the sliders must not exceed 100. That is, the sliders should not allow such increases.

如果用户为速度,敏捷性和耐力分配了30分,那么精度滑块最多应允许10分(因为30 + 30 + 30 + 10 = 100)。尽管如此,每个滑块的范围应该保持不变(在这种情况下,0到100)。

If the user has allocated 30 points each to speed, agility and endurance, then the slider for accuracy should allow a maximum of 10 points (because 30 + 30 + 30 + 10 = 100). Notwithstanding, the range of the each slider should remain unchanged (in this case, 0 to 100).

我希望这是有道理的。感谢您的帮助。

I hope this makes sense. Thanks for the help.

推荐答案

nichts einfacher als das - dachte ...(插入名称: - )

"nichts einfacher als das" - dachte ... (insert name :-)

基本上,所有这些增强功能必须在模型中实现:对于JSlider,这是一个BoundedRangeModel。实施/扩展和强制执行其值以尊重组合最大值。类似

Basically, all such enhanced functionality must be implemented in the model: for JSlider, that's a BoundedRangeModel. Implement/extend and enforce its value to respect the "combined" max. Something like

public static class LimitedBoundedRangeModel extends DefaultBoundedRangeModel {

    BoundedRangeModel limit;

    public LimitedBoundedRangeModel(BoundedRangeModel limit) {
        this.limit = limit;
    }

    /** 
     * @inherited <p>
     */
    @Override
    public void setRangeProperties(int newValue, int newExtent, int newMin,
            int newMax, boolean adjusting) {
        if (limit != null) {
            int combined = newValue + limit.getValue();
            if (combined > newMax) {
                newValue = newMax - limit.getValue();
            }
        }
        super.setRangeProperties(newValue, newExtent, newMin, newMax, adjusting);
    }
}

// use
LimitedBoundedRangeModel firstModel = new LimitedBoundedRangeModel(null);
LimitedBoundedRangeModel secondModel = new LimitedBoundedRangeModel(firstModel);
firstModel.limit= secondModel;

JSlider first = new JSlider(firstModel);
JSlider second = new JSlider(secondModel);

虽然简单(仅限两个家属)和原始(直接双向耦合),因此不能真正使用在野外,它应该至少工作......但不是 - 不时给我打击的惊喜之一;-)视觉问题是拇指位置:

While simple (two dependents only) and crude (direct bidirectional coupling) and as such not really usable out in the wild, it should at least work ... but doesn't - one of those surprises which hit me from time to time ;-) The visual problem is the thumb-position:


  • 单击拇指右侧时,组合的最大值得到尊重:拇指从不移动到阈值

  • 拖动时,拇指可以由于ui不知道模型调整 - 它只知道拖拽结束时的本地最大值

  • ,因此拇指仍保留在该无效状态位置..它闻起来像一个臭虫,因为现在拇指与模型外同步

造成这种不当行为的原因是Handler中的changeListener:拖动时不会重新计算其拇指位置(这没关系)。微妙的错误是,内部拖动标志仅在模型的调整属性重置后重置,因此错过了关于最终值的最后一个通知...

The reason for this misbehaviour is that the changeListener in Handler: it doesn't recalculate its thumb position when dragging (which is okay). The subtle bug is, that the internal dragging flag is only reset after the model's adjusting property is reset, so missing the very last notification about the final value ...

如果调整标志从true变为false,则Hack是调用另一个changeEvent的触发器

Hack around is to invoke the firing of an additional changeEvent, if the adjusting flag changes from true to false

        boolean invoke =
               (adjusting != getValueIsAdjusting()) && !adjusting;
        super.setRangeProperties(newValue, newExtent, newMin, newMax, adjusting);
        if (invoke) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    fireStateChanged();   
                }
            });
        }

这篇关于链接的JSliders具有最大组合值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆