Java - repaint(x, y, w, h) doesn't call paintComponent? (with SSCCE)

前端 未结 1 2024
难免孤独
难免孤独 2021-01-21 08:48

I asked this before, but only theoretically, without an SSCCE. Now, I\'ve created one, and the problem persists. I\'d like to know why paintComponent is not calle

相关标签:
1条回答
  • 2021-01-21 08:55

    You're calling this

    repaint(TLabel.this.getBounds());
    

    inside of the TLabel object. So repaint will try to paint a rectangle located relative to itself at the Bounds location, but getBounds() returns a rectangle located relative to this components containing object's location while repaint expects bounds relative to the component itself. So you're trying to paint a rectangle that has the width and height of your JLabel but which is located at x = 292 and y = 5 relative to the JLabel, when instead you want x and y to both be 0. In essence you're trying to draw way outside of this component.

    Instead try this:

            //!! repaint(TLabel.this.getBounds());
            Dimension d = TLabel.this.getSize();
            repaint(new Rectangle(0, 0, d.width, d.height));
    
    0 讨论(0)
提交回复
热议问题