JSlider无法正确更新鼠标事件

时间:2013-11-12 00:03:14

标签: java swing mouse jslider changelistener

我有一个applet,它从JFileChooser中选择一个音频文件。创建或更新两个JSliders以匹配剪辑的长度,标题为“点A”和“点B”。按下JButton,然后文件播放到A点。当它到达A点时,剪辑开始用交叉渐变将音频从A点循环到B点。

问题在于我必须确保A点永远不会超出B点,原因显而易见,但我的方法并不适用于此。

添加一个在源JSlider更新或不更新时更新的changelistener是我尝试过的两件事。对于!source.getValueIsAdjusting(),它适用于使用箭头键,但不适用于鼠标;对于source.getValueIsAdjusting(),它可以以延迟的方式工作:它似乎是比较或重置事件后期的值。< / p>

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SliderExample extends JApplet{

    private MyPanel panel;
    private final Random rand = new Random();


    public SliderExample() {
        panel = new MyPanel();
        add(panel);
    }

    private class MyPanel extends JPanel implements 
    ActionListener{

        private JSlider sliderA, sliderB;
        private final Timer t = new Timer(20, this);

        public MyPanel()
        {
            super(false);
            sliderA = new JSlider(JSlider.HORIZONTAL, 0, 20, 5);
            sliderB = new JSlider(JSlider.HORIZONTAL, 0, 20, 15);
            sliderA.setBorder(BorderFactory.createTitledBorder("Point A"));
            sliderB.setBorder(BorderFactory.createTitledBorder("Point B"));
            sliderA.setMajorTickSpacing(5);
            sliderB.setMajorTickSpacing(5);
            sliderA.setMinorTickSpacing(1);
            sliderB.setMinorTickSpacing(1);
            sliderA.setPaintTicks(true);
            sliderB.setPaintTicks(true);
            sliderA.setPaintLabels(true);
            sliderB.setPaintLabels(true);
            sliderA.addChangeListener(new SliderHandler());
            sliderB.addChangeListener(new SliderHandler());

            add(sliderA);
            add(sliderB);

            t.start();
        }

        @Override
        public void actionPerformed(ActionEvent e) {

//          uncomment these to see that the slider set value does work
//          sliderA.setValue(rand.nextInt(sliderA.getMaximum()));  
//          sliderB.setValue(rand.nextInt(sliderB.getMaximum()));


            int A = sliderA.getValue();
            int B = sliderB.getValue();
            if (A > B)
            {
                System.out.println("ERROR: slider A is: " + A + " and slider B is : " + B);
                sliderA.setValue(B);
            }
        }

        private class SliderHandler implements ChangeListener
        {
            private SliderHandler() 
            {
            }

            public void stateChanged(ChangeEvent e) 
            {
                JSlider source = (JSlider)e.getSource();
                if (!source.getValueIsAdjusting())
                {
                    if (source == sliderA) 
                    {
                        if (source.getValue() > sliderB.getValue())
                        {
                            source.setValue(sliderB.getValue());
                        }

                    }
                    else if (source == sliderB)
                    {
                        if (source.getValue() < sliderA.getValue())
                        {
                            source.setValue(sliderA.getValue());
                        }
                    }
                }
            }
        }   
    }
}

1 个答案:

答案 0 :(得分:3)

对我来说似乎没问题。我已经将我的滑块更改为+ 1或-1,形成了我想要离开的值。例如:

import javax.swing.*;
import javax.swing.event.*;

@SuppressWarnings("serial")
public class SliderExample2 extends JPanel {
   private JSlider sliderA = new JSlider(JSlider.HORIZONTAL, 0, 20, 5);
   private JSlider sliderB = new JSlider(JSlider.HORIZONTAL, 0, 20, 15);
   private JSlider[] sliders = {sliderA, sliderB};
   private String[] titles = {"Point A", "Point B"};

   private ChangeListener[] changeListeners = {new ChangeListenerA(), 
           new ChangeListenerB()};

   public SliderExample2() {
      for (int i = 0; i < sliders.length; i++) {
         sliders[i].setBorder(BorderFactory.createTitledBorder(titles[i]));
         sliders[i].setMajorTickSpacing(5);
         sliders[i].setMinorTickSpacing(1);
         sliders[i].setPaintTicks(true);
         sliders[i].setPaintLabels(true);             
         sliders[i].addChangeListener(changeListeners[i]);             
         add(sliders[i]);             
      }
   }

   private class ChangeListenerA implements ChangeListener {
      @Override
      public void stateChanged(ChangeEvent cEvt) {
         if (sliderA.getValue() >= sliderB.getValue()) {
            sliderB.setValue(sliderA.getValue() + 1);
         }
      }
   }

   private class ChangeListenerB implements ChangeListener {
      @Override
      public void stateChanged(ChangeEvent cEvt) {
         if (sliderA.getValue() >= sliderB.getValue()) {
            sliderA.setValue(sliderB.getValue() - 1);
         }
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("SliderExample2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new SliderExample2());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

修改

哦,我看到了你的目标和问题 - 你想限制移动滑块的范围。您忘记设置setSnapToTicks(true)

import javax.swing.*;
import javax.swing.event.*;

@SuppressWarnings("serial")
public class SliderExample3 extends JPanel {
   private JSlider sliderA = new JSlider(JSlider.HORIZONTAL, 0, 20, 5);
   private JSlider sliderB = new JSlider(JSlider.HORIZONTAL, 0, 20, 15);
   private JSlider[] sliders = {sliderA, sliderB};
   private String[] titles = {"Point A", "Point B"};

   private ChangeListener[] changeListeners = {new ChangeListenerA(), 
           new ChangeListenerB()};

   public SliderExample3() {
      for (int i = 0; i < sliders.length; i++) {
         sliders[i].setBorder(BorderFactory.createTitledBorder(titles[i]));
         sliders[i].setMajorTickSpacing(5);
         sliders[i].setMinorTickSpacing(1);
         sliders[i].setPaintTicks(true);
         sliders[i].setPaintLabels(true);
         sliders[i].setSnapToTicks(true);
         sliders[i].addChangeListener(changeListeners[i]);             
         add(sliders[i]);             
      }
   }

   private class ChangeListenerA implements ChangeListener {
      @Override
      public void stateChanged(ChangeEvent cEvt) {
         if (sliderA.getValue() >= sliderB.getValue()) {
            sliderA.setValue(sliderB.getValue());
         }
      }
   }

   private class ChangeListenerB implements ChangeListener {
      @Override
      public void stateChanged(ChangeEvent cEvt) {
         if (sliderA.getValue() >= sliderB.getValue()) {
            sliderB.setValue(sliderA.getValue());
         }
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("SliderExample3");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new SliderExample3());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}