简体   繁体   English

Java:双击要重置的JSlider

[英]Java: Double-Click a JSlider to Reset

I have a JSlider that sets the speed of my metronome, from 40 - 200, where 120 is the default, in the middle. 我有一个JSlider设置我的节拍器的速度,从40 - 200,其中120是默认,在中间。

When the user clicks the metronome button, the metronome plays at the speed displayed on the JSlider - the user drags the slider to the right, the speed of the metronome increases, and it decreases if they slide it to the left. 当用户点击节拍器按钮时,节拍器以JSlider上显示的速度播放 - 用户将滑块向右拖动,节拍器的速度增加,如果它们向左滑动则减小。

How do I add functionality so that if the user double-clicks on the JSlider button, it defaults back to 120 - in the middle? 如何添加功能,以便用户双击JSlider按钮时,它会默认返回120 - 中间?

Here is my code: 这是我的代码:

public Metronome() {
    tempoChooser = new JSlider();
    metronomeButton = new JToggleButton();

    JLabel metText = new JLabel("Metronome:");
    add(metText);

    ...

    tempoChooser.setMaximum(200);
    tempoChooser.setMinimum(40);
    tempoChooser.addChangeListener(new javax.swing.event.ChangeListener() {
        public void stateChanged(javax.swing.event.ChangeEvent evt) {
            tempoChooserStateChanged(evt);
        }
    });
    add(tempoChooser);
    ...
    }

private void tempoChooserStateChanged(javax.swing.event.ChangeEvent evt) {
    final int tempo = tempoChooser.getValue();
    if (((JSlider) evt.getSource()).getValueIsAdjusting()) {
        setMetronomeButtonText(tempo);
    } else {
        processTempoChange(tempo);
    }
}

thanks in advance! 提前致谢!

This should help you out: http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html 这应该可以帮到你: http//docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html

You need to read up on that and implement MouseListener. 您需要阅读并实现MouseListener。 You can use int getClickCount() to count how many times the user has clicked, which will help you read double clicks. 您可以使用int getClickCount()来计算用户点击的次数,这将有助于您阅读双击。

Hope this helps! 希望这可以帮助!

即使我没有看到一个问题,我的观点是你正在寻找MouseListener

不是简单的工作,你必须添加javax.swing.Timer并监听,如果在固定期间鼠标clike一次或两次, 例如

I recently wrote something similar so I could differentiate between single and double left mouse-button clicks: 我最近写了类似的东西,所以我可以区分单键和双键鼠标按钮点击:

private Timer timer;
@Override
public void mouseClicked(MouseEvent e) {
    if(e.getButton() == MouseEvent.BUTTON1){
        if (timer == null) {
            timer = new Timer();
            timer.schedule(new TimerTask() {

                @Override
                public void run() { // timer expired before another click received, therefore = single click
                    this.cancel();
                    timer = null;
                    /* single-click actions in here */
                }

            }, (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"));
        }
        else { // received another click before previous click (timer) expired, therefore = double click
            timer.cancel();
            timer = null;
            /* double-click actions in here */
        }
    }
}

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

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