0

我有几个看起来像这样的滑块:

//Create the slider
    JSlider speedSlider = new JSlider(JSlider.VERTICAL, MIN_SPEED, MAX_SPEED, initValue);
    speedSlider.addChangeListener(controller);
    speedSlider.setMajorTickSpacing(MAJOR_TICK_SPACING);
    speedSlider.setPaintTicks(true);

    panel.add(speedSlider);

在我的 View.java 类中

有Controller.java

public class Controller implements ControllerInterface {

    //...

    @Override
    public void stateChanged(ChangeEvent e) {
        JSlider source = (JSlider)e.getSource();
        if (!source.getValueIsAdjusting()) {
            int speed = (int)source.getValue();
            if (0 == speed) {
                // stop
            }
            else {
                model.setSpeedBody(speed);
            }
        }
    }
}

问题是我无法确定它到底是什么滑块事件,该怎么做?(ControllerInterface 扩展 ChangeListener)

4

1 回答 1

2

测试source的值:

JSlider source = (JSlider)e.getSource();
if (source == speedSlider) {
    ...
}
else if (source == gearSlider) {
    ...
}

或者为每个滑块添加一个特定的侦听器,以便每个侦听器只收到一个滑块的更改通知:

speedSlider.addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent e) {
        ...
    }
});
于 2013-05-20T06:27:22.513 回答