Using paintComponent() to draw rectangle in JFrame

前端 未结 2 480
一整个雨季
一整个雨季 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.

    0 讨论(0)
  • 2021-01-18 23:04

    you're never actually adding the panel to the frame, so it is never visible. you need something like

    frame.getContentPane().add( panel );
    

    why are you defining the drawpanel class inside the main method? that's rather odd.

    0 讨论(0)
提交回复
热议问题