paintComponent not painting onto JPanel

一曲冷凌霜 提交于 2019-12-02 08:20:45

Your Main class extends JPanel, has a paintComponent method -- but you never add an instance of Main to the GUI, instead you add a plain-vanilla JPanel, drawBox to the GUI, and so of course paintComponent will never be called.

Solution: add Main or this to the GUI, not a plain JPanel.

The panel I would like to draw on is the Draw Box JPanel naturally.

drawBox = new JPanel();

Well you didn't override the paintComponent(...) method of your "drawBox" panel so no painting will happen.

Also, whenever you always need to invoke super.paintComponent(...) at the start of your paintComponent(...) method.

Check out Custom Painting Approaches for working examples of the two common ways to do custom painting.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!