且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

paintComponent未绘制到JPanel上

更新时间:2023-12-05 18:16:04

您的Main类扩展了JPanel,具有paintComponent方法-但是您从未将Main的实例添加到GUI ,而是在GUI上添加一个普通的JPanel,drawBox,当然,paintComponent将永远不会被调用.

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.

解决方案:将Main或this添加到GUI,而不是普通的JPanel.

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