Java: when calling repaint() on a JPanel, the components in the same JFrame redraw themselves in the top left corner

StackOverflow https://stackoverflow.com/questions/21842492

  •  13-10-2022
  •  | 
  •  

Domanda

I have a Graph class that extends JPanel. It gets repainted on mouseMoved. When this happens, basically any other component that was added to a layout (whether it be the same JPanel or a seperate JFrame) will get pained on the top left corner. I have a screen shot to go with the following code. If you look closely in the top left, there is a small textarea that is appearing. This will happen with any component.

public class GUI extends JFrame {

    private JTextArea txtPoints = new JTextArea(20, 40);

    GUI() {
        setSize(800, 1000);
        setLayout(new MigLayout());
        add(new Graph(), "wrap");
        add(txtPoints);
        setVisible(true);
   }
}

enter image description here

È stato utile?

Soluzione

Welcome to the wonderful world of broken paint chain.

Without further evidence, you've overridden some paintXxx method and failed to call super.paintXxx meaning that what ever was previously painted to the Graphics context still remains...

Take a look at Performing Custom Painting and Painting in AWT and Swing for more details

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top