I can not understand why, as a result of the execution of the code, the oval is not displayed, and when using the paint(Graphics g) method, the oval is displayed.

Painting class:

 public class Painting extends JFrame { Pan p = new Pan(); public Painting(String s){ super(s); add(p); } public static void main(String[] args) { Painting p = new Painting("First"); p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); p.setBounds(50, 50, 600, 600); p.setVisible(true); } } 

Pan class:

  public class Pan extends JPanel{ @Override public void paintComponents(Graphics g){ super.paintComponents(g); Graphics2D g2 = (Graphics2D) g; g2.setPaint(BLACK); g2.fillOval(100, 100, 200, 200); } } 
  • It is necessary to add a panel to the frame - Nikolay Belyakov
  • I have it added (add (p)) - Yura Bezludniy
  • you need something like setContentPane (p) or getContentPane (). add (p) - Nikolay Belyakov
  • I tried both methods - the result is the same. - Yura Bezludny

1 answer 1

Change method name instead of paintComponents - paintComponent (singular without 's')