问题描述
我花了最近两个小时浏览和阅读这些方法和Graphics类,也许我很蠢,哈哈,但我只是不理解它们。它们适用于什么?我知道他们应该在屏幕上重绘或更新组件,但我从未理解为什么这是必需的(我是新手)。例如,如果我在屏幕上移动JLabel,则调用setLocation()会将其移动得很好。这是不需要repaint()的场景吗?它在哪些情况下有用,为什么?
I've spent maybe the last two hours browsing and reading up on these methods and the Graphics class, and maybe I'm stupid, haha, but I'm just not understanding them. What are they for? I understand that they're supposed redraw or update components on the screen, but I have never understood why this is required (I'm new to this). For example, if I'm moving a JLabel around the screen, a call to setLocation() moves it just fine. Is that a scenario in which repaint() isn't required? In which scenarios is it useful, and why?
如果您认为这是一个可以使用搜索功能解决的问题,请道歉,但无论出于何种原因我都是没法得到它。
Apologies if you feel that this is a question that could be solved using the search function, but for whatever reason I'm not getting it.
推荐答案
Paint()和Repaint()方法之间的区别
Paint():
此方法包含绘制此组件的说明。实际上,在Swing中,你应该改变paintComponent()而不是paint(),因为paint调用paintBorder(),paintComponent()和paintChildren()。你不应该直接调用这个方法,你应该调用repaint()。
Difference between Paint() and Repaint() method
Paint():
This method holds instructions to paint this component. Actually, in Swing, you should change paintComponent() instead of paint(), as paint calls paintBorder(), paintComponent() and paintChildren(). You shouldn't call this method directly, you should call repaint() instead.
此方法无法覆盖。它控制update() - > paint()循环。您应该调用此方法来获取重新绘制自身的组件。如果您已经做了任何事情来改变组件的外观,而不是它的大小(比如更改颜色,动画等),那么请调用此方法。
This method can't be overridden. It controls the update() -> paint() cycle. You should call this method to get a component to repaint itself. If you have done anything to change the look of the component, but not its size ( like changing color, animating, etc. ) then call this method.
这篇关于Java中的paint()和repaint()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!