Java:双击 JSlider 进行重置

发布于 2025-01-03 05:14:23 字数 1014 浏览 3 评论 0原文

我有一个 JSlider,可以设置节拍器的速度,从 40 到 200,其中 120 是默认值,位于中间。

当用户单击节拍器按钮时,节拍器将以 JSlider 上显示的速度播放 - 用户向右拖动滑块,节拍器的速度会增加,如果向左滑动,节拍器的速度会减小。

如何添加功能,以便如果用户双击 JSlider 按钮,它会默认返回到 120 - 中间?

这是我的代码:

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);
    }
}

提前致谢!

I have a JSlider that sets the speed of my metronome, from 40 - 200, where 120 is the default, in the middle.

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.

How do I add functionality so that if the user double-clicks on the JSlider button, it defaults back to 120 - in the middle?

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我很坚强 2025-01-10 05:14:23

这应该可以帮助您: http://docs.oracle.com/javase /tutorial/uiswing/events/mouselistener.html

您需要阅读并实现 MouseListener。您可以使用 int getClickCount() 来统计用户点击的次数,这将有助于您读取双击次数。

希望这有帮助!

This should help you out: http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html

You need to read up on that and implement MouseListener. You can use int getClickCount() to count how many times the user has clicked, which will help you read double clicks.

Hope this helps!

梦太阳 2025-01-10 05:14:23

尽管我没有看到问题,但我的猜测是您正在寻找 MouseListener

Even though I dont see a question, my gues is you are looking for MouseListener.

御弟哥哥 2025-01-10 05:14:23

这不是简单的工作,您必须添加 javax.swing.Timer 并监听在固定时间段内鼠标点击一次或两次,例如

Not simple job, you have to add javax.swing.Timer and listening if during fixed period Mouse cliked once or twice times, for example

他是夢罘是命 2025-01-10 05:14:23

我最近写了一些类似的东西,这样我就可以区分单次和双次鼠标左键单击:

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 */
        }
    }
}

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 */
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文