Swing - paintComponent method not being called

后端 未结 2 546
面向向阳花
面向向阳花 2020-12-21 00:42

i simply implemented class that inherits JPanel like below

public class Orpanel extends JPanel {

....
    @Override
    public void paintComponent(Graphics          


        
相关标签:
2条回答
  • 2020-12-21 00:50

    In your sample code, I see you have chosen NOT to use a LayoutManager, that's a very bad idea, but anyway, sicne you go this way, I see one reason for your Orpanel.paintComponent() not being called: it is probably not visible inside the frame!

    If you have no LayoutManager, then you must explicitly set the size and location (through setBounds()) of all components you add to the frame.

    It is likely you didn't do it, hence the size of Orpanel instance is probably 0 hence it never gets painted.

    0 讨论(0)
  • 2020-12-21 01:05

    Sounds like you're just using the wrong methods. You should be doing this when adding a panel to a frame:

    frame.getContentPane().add(panel) ;
    
    0 讨论(0)
提交回复
热议问题