Detecting a value change of my JSlider

Add a ChangeListener and add behavior to its method stateChanged.

Main.java:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
  
public class Main extends JFrame { 
   JSlider slider;
 
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });   
 
      slider = new JSlider(JSlider.HORIZONTAL, 0, 20, 1);
      slider.addChangeListener(new ChangeListener() {
         public void stateChanged(ChangeEvent ce) {
            repaint();
         }
      });
 
      slider.setMajorTickSpacing(1);
      slider.setPaintTicks(true);
      slider.setPaintLabels(true);
 
      getContentPane().setLayout(new BorderLayout());
      getContentPane().add(BorderLayout.NORTH, slider);
   } 
 
   public void paint(Graphics g) {
      super.paint(g);
  
      Graphics2D g2d = (Graphics2D) g;
 
      g2d.setFont(new Font("Serif", Font.BOLD, 36));
      g2d.drawString(""+slider.getValue(), 200, 130);
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(400, 150);
      main.setVisible(true);
   }
}