问题描述
对不起,我经常搜索这三个函数(paint,repaint,paintComponent)如何在它们之间进行交互,但我不知道。你可以在他们被叫的时候准确地解释我(因为有时候java会在没有我问他的情况下调用它)他们究竟做了什么,他们之间有什么区别。谢谢
excuse me i search a lot to find how those 3 functions (paint, repaint, paintComponent) interact between them but i have no idea. Can you explain me exactly when they are called ( because sometimes java call it without me asking him) what they do exactly and what is the difference between them. Thank you
推荐答案
我不确定画,但我可以解释一下repaint()和paintComponent()之间的关系。
I am not sure about "paint", but I can explain the relationship between repaint() and paintComponent().
在我对java的有限经验中,paintComponent()方法是JPanel类中的一个方法,是swing的成员。
In my limited experience with java, the paintComponent() method is a method in the JPanel class and is a member of "swing".
paintComponent()方法处理所有绘画。从本质上讲,它使用Graphic对象将您想要的任何内容绘制到JPanel中。
The paintComponent() method handles all of the "painting". Essentially, it draws whatever you want into the JPanel usings a Graphic object.
repaint()是所有JPanel对象的继承实例方法。调用[your_JPanel_object] .repaint()会调用paintComponent()方法。
repaint() is an inherited instance method for all JPanel objects. Calling [your_JPanel_object].repaint() calls the paintComponent() method.
每次要更改JPanel的外观时,都必须调用repaint()。
Every time you wish to change the appearance of your JPanel, you must call repaint().
某些操作自动调用repaint()方法:
Certain actions automatically call the repaint() method:
- 重新调整大小您的窗口
- 最小化并最大化您的窗口
仅举几例。
IN SHORT paintComponent()是在JPanel中定义的方法或您自己的扩展JPanel的自定义类。 repaint()是在另一个类(例如JFrame)中调用的方法,它最终调用paintComponent()。
IN SHORT paintComponent() is a method defined in JPanel or your own custom class that extends JPanel. repaint() is a method called in another class (such as JFrame) that eventually calls paintComponent().
这里有一个例子:
public class MyPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
g.draw([whatever you want]);
...
...
}
}
public class MyFrame extends JFrame{
public MyFrame(){
MyPanel myPanel = new MyPanel();
myPanel.repaint();
}
}
这篇关于油漆,重漆,油漆组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!