import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class RegisterChangeListenerToJSlider {
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL, 10, 100, 20);
framesPerSecond.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider) e.getSource();
if (!source.getValueIsAdjusting()) {
int fps = (int) source.getValue();
System.out.println(fps);
}
}
});
frame.add(framesPerSecond,"North");
frame.setSize(300, 200);
frame.setVisible(true);
}
}
|