// Stephen Gilbert // Show how to use the JSlider import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class JSliderApp extends JPanel implements ChangeListener { JSlider s1 = new JSlider( JSlider.HORIZONTAL, 0, // Miniumum 150, // Maximum 25 // Initial value ); JLabel output = new JLabel("25", JLabel.CENTER); public JSliderApp() { // 1. Initialize the JSlider s1.setMajorTickSpacing(25); // Default is 0 s1.setMinorTickSpacing(5); // Default is 0 s1.setPaintTicks(true); // false by default s1.setPaintLabels(true); // false by default // 2. Hook up the JSlider s1.addChangeListener(this); // 3. Change the display of the output label output.setFont(new Font("SansSerif", Font.BOLD, 36)); output.setBackground(Color.white); output.setForeground(Color.red); // 4. Construct the Panel setLayout(new BorderLayout()); add(s1, BorderLayout.NORTH); add(output, BorderLayout.SOUTH); } // 5. Handle messages from the Slider public void stateChanged(ChangeEvent e) { output.setText("" + s1.getValue()); } // 6. Main-launch GApp with a JSliderApp() object public static void main(String[] args) { new GApp("The JSliderApp", new JSliderApp()).show(); } }