Using paintComponent() to draw rectangle in JFrame

前端 未结 2 482
一整个雨季
一整个雨季 2021-01-18 22:02

I\'m trying to create a program that draws shapes (a rectangle on the example below) using JPanel\'s paintComponent(), but I can\'t get it to work and can\'t spot what is wr

2条回答
  •  有刺的猬
    2021-01-18 23:00

    Your class DrawPanel is confined to the scope of your main method and is not visible to your constructor.

    You need to move DrawPanel out of your main method, then add it to your JFrame:

    frame.add(panel);
    

    Also, better to call frame.setVisible(true) after all components have been added.

提交回复
热议问题