当我调用 getGraphics() 时,它经常返回 null,即使我设置了 xxx.getGraphics(); xxx 可见(如谷歌搜索显示...)
但这行不通,这让我很沮丧,因为在 C-Sharp 中很容易做到。
有谁知道比使用 getGraphics() 更好的方法吗??
最佳答案
不要使用 getGraphics()。您所做的任何绘制都将是临时的,并且会在下次 Swing 确定需要重新绘制组件时丢失。
而是重写 JComponent 或 JPanel 的 paintComponent()
方法来进行自定义绘制。参见 Custom Painting了解更多详情和示例。
关于java - 调用返回 null 的 getGraphics() 的任何替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8184913/