简体   繁体   English

Paint方法执行多次

[英]Paint method execute many times

Why this paint function execute many times while I run my code?为什么这个绘制函数在我运行代码时执行多次?

I am trying to run this code only one time, but it execute many times, and I don't know why it does that!我试图只运行这段代码一次,但它执行了很多次,我不知道为什么会这样!

public class DrawFrame extends JFrame {

@Override
public void paint(Graphics g) {

    System.out.println("hello Frame");
}
}

public class NJFrame {
public static void main(String[] args) {


    DrawFrame NJFrame = new DrawFrame();
    NJFrame.setSize(1000, 1000);
    NJFrame.setVisible(true);
    NJFrame.setLocation(400, 150);
    NJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

在此处输入图片说明

Well, your code manipulates the JFrame a number of times:好吧,您的代码多次操作 JFrame:

DrawFrame NJFrame = new DrawFrame(); // (1) Create the frame
NJFrame.setSize(1000, 1000);         // (2) Resize the frame
NJFrame.setVisible(true);            // (3) Show the frame
NJFrame.setLocation(400, 150);       // (4) Move the frame

It would seem that each of these operations causes a paint event to be fired, which your paint method handles.似乎这些操作中的每一个都会触发一个绘制事件,您的paint方法会处理该事件。

paint method is automatically called whenever it is needed.需要时自动调用paint方法。 It's handled by Swing and is invoked when the content pane of your frame needs to be repainted, when the window size is resized, minimized and so on.它由 Swing 处理,并在需要重新绘制框架的内容窗格、调整窗口大小、最小化等时调用。

For more information, you can explore it.有关更多信息,您可以探索它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM