Java Swing - Translucent Components causing Artifacts

前端 未结 3 1496
闹比i
闹比i 2021-01-21 02:18

I\'m currently working on a group project for a college course, and I\'ve hit a bit of a stumbling block. The program we have decided to implement is a Peer-to-Peer chat client

3条回答
  •  粉色の甜心
    2021-01-21 02:53

    You need to tell Swing that the components are translucent, so it does also paint the background when repainting the changed components. For this, your components have to return false from the isOpaque() method (this can be achieved by setOpaque(false) when no subclass overwrites isOpaque to do something else).

    With this, it should work without any manual repainting of everything, as the other answers proposed. (I already did this once.)

    Edit: The reason for this is that Swing uses an optimized repainting-algorithm, repainting only these components which really need repainting (for example, a JTextField after some input) or components in front of such ones, as long as they are opaque. When a component needing repainting is not opaque (= filling its whole space with non-translucent color), also repainting of the components behind them is necessary.

提交回复
热议问题