import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainFrame extends JFrame {
JPanel panel = new JPanel(new BorderLayout());
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
JLabel label = new JLabel(“Value : “, JLabel.CENTER);
public MainFrame(String name) {
super(name);
this.add(panel);
panel.add(slider, BorderLayout.CENTER);
panel.add(label, BorderLayout.SOUTH);
// using anonymous class
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider s = (JSlider)e.getSource();
label.setText(“Value : ” + s.getValue());
}
});
this.setSize(250, 150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame(“SliderTest_5_1_1”);
}
}
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
// using implements ChangeListener
public class MainFrame extends JFrame implements ChangeListener {
JPanel panel = new JPanel(new BorderLayout());
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
JLabel label = new JLabel(“Value : “, JLabel.CENTER);
public MainFrame(String name) {
super(name);
this.add(panel);
panel.add(slider, BorderLayout.CENTER);
panel.add(label, BorderLayout.SOUTH);
slider.addChangeListener(this); // implements ChangeListener
this.setSize(250, 150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame(“SliderTest-5_1_2”);
}
@Override
public void stateChanged(ChangeEvent e) {
JSlider s = (JSlider)e.getSource();
label.setText(“Value : ” + s.getValue());
}
}
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainFrame extends JFrame {
JPanel panel = new JPanel(new BorderLayout());
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
JLabel label = new JLabel(“Value : “, JLabel.CENTER);
// using private inner class
private class SliderChangeListener implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
JSlider s = (JSlider)e.getSource();
label.setText(“Value : ” + s.getValue());
}
}
public MainFrame(String name) {
super(name);
this.add(panel);
panel.add(slider, BorderLayout.CENTER);
panel.add(label, BorderLayout.SOUTH);
slider.addChangeListener(new SliderChangeListener()); // private class
this.setSize(250, 150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame(“SliderTest-5_1_3”);
}
}