PaintComponent not being called with JPanel

后端 未结 1 469
鱼传尺愫
鱼传尺愫 2021-01-28 00:34

when I run this code PaintComponent is never called because the \"painted\" message is never printed and I do not know why? can anyone help?

public class Dis         


        
相关标签:
1条回答
  • 2021-01-28 01:35

    There are a couple of problems with the basic code:

    1. as already mentioned you need to add an instance of your DisplayManager class to a frame or panel.

    2. When you do custom painting you need to override the getPreferredSize() method of the component to return your desired size. Currently the preferred size of your component is (0, 0).

    The suggestion to add the DisplayManager to the frame only works because the default layout manager is a BorderLayout and by default is added to the CENTER of the layout which means it get all the available space in the frame.

    However if you use:

    frame.add(this, BorderLayout.PAGE_START);
    

    you won't see the component size it has a size of (0, 0);

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