简体   繁体   English

请解释java中repaint()方法的工作原理?

[英]Please explain the working of repaint() method in java?

I have seen many questions similar to this But I am not able to understand why the repaint do not work properly我见过很多类似的问题但我不明白为什么重绘不能正常工作

In my program Before exiting the Frame I called repaint() use Thread.sleep to delay the exit then also the msg "Mouse Exited" does not get displayed.在我的程序中,在退出框架之前,我调用了 repaint() 使用 Thread.sleep 来延迟退出,然后消息“鼠标退出”也不会显示。

Can you please explain why this happens or provide the link for the same你能解释一下为什么会发生这种情况或提供相同的链接吗

 public void mouseExited(MouseEvent me)
            {
                msg="Mouse Exited";
                repaint();
                try {
                    Thread.sleep(1000);

                    
                } catch (InterruptedException e) {
                    
                }
                System.exit(0);
            }

Complete Program完整的程序

/**Write a program to create a frame using AWT. Implement mouseClicked( ),
mouseEntered( ) and mouseExited( ) events. Frame should become visible when
mouse enters it. */

import java.awt.*;
import java.awt.event.*;




class Question8 extends Frame{

           static Dimension original;
           String msg;

         Question8(String s)
        {
           msg=s;
           addMouseListener(new MouseAdapter()
           {
            public void mouseClicked(MouseEvent me)
            {
                
                msg="Mouse Clicked";
                setSize(original);
                repaint();

                
            }
            public void mouseEntered(MouseEvent me)
            {
                msg="Mouse Entered";
                setSize(original.height*3,original.width*3);
                repaint();

            }
            public void mouseExited(MouseEvent me)
            {
                msg="Mouse Exited";
                repaint();
                try {
                    Thread.sleep(1000);

                    
                } catch (InterruptedException e) {
                    
                }
                System.exit(0);
            }

            });

            addWindowListener(new WindowAdapter()
            {
                public void windowClosing(Window ev)
                {
                    System.exit(0);
                }

            });
        }

        public void paint(Graphics g)
        {
            g.drawString(msg, 50, 50);
        }

        public static void main(String agrs[])
        {
            Question8 obj=new Question8("Hello");
            original=new Dimension(300,300);
            obj.setSize(original);
            obj.setTitle("Question 8");
            obj.setVisible(true);
        }

    
}

As maloomeister pointed out, the Thread.sleep() is blocking the event thread, so you need to create a new thread.正如maloomeister 所指出的, Thread.sleep()正在阻塞事件线程,因此您需要创建一个新线程。

If you replace your mouseExited method with this it will behave as you expect:如果你用这个替换你的mouseExited方法,它会像你期望的那样工作:

public void mouseExited(MouseEvent me) {
                msg = "Mouse Exited";
                repaint();
                new Thread(() -> {
                    try {
                        Thread.sleep(1000);
                        System.exit(0);
                    } catch (InterruptedException e) {
                    }
                }
                ).start();
            }

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

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