java 通过 setVisible 隐藏/显示的 JFrame 的窗口事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/622814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:06:32  来源:igfitidea点击:

Window events for JFrames that are hidden/shown via setVisible?

javaswingevents

提问by schweerelos

Which kind of listener do I have to add to a JFrameto detect when it is being hidden or shown via setVisible?

我必须添加哪种侦听器JFrame来检测它何时被隐藏或通过 显示setVisible

I tried using a WindowListenerand the windowOpenedand windowClosedmethods, but they only work for the first timethat a window is opened (windowOpened) or, respectively, when the window is closed using the dispose method (windowClosed). That is not enough for me. I want to be notified every time the window is made visible and invisible on the screen using setVisible.

我尝试使用 aWindowListenerwindowOpenedandwindowClosed方法,但它们仅在第一次打开窗口时有效 ( windowOpened),或者分别在使用 dispose 方法关闭窗口时有效 ( windowClosed)。这对我来说还不够。我希望每次使用setVisible.

Is there a standard Swing way to achieve this, or do I need to make my own (by, say, overriding the setVisiblemethod)?

是否有标准的 Swing 方法来实现这一点,还是我需要自己制作(例如,覆盖该setVisible方法)?

回答by James

Try a java.awt.event.ComponentListener. You can add one using this code (where window is the name of the JFrame) :

尝试一个java.awt.event.ComponentListener. 您可以使用此代码添加一个(其中 window 是 的名称JFrame):

window.addComponentListener(new ComponentAdapter() {
   public void componentHidden(ComponentEvent e) {
      /* code run when component hidden*/
   }
   public void componentShown(ComponentEvent e) {
      /* code run when component shown */
   }
});

回答by Desphilboy

1- Create a class that implements ComponentListener Interface, Like the following example:

1- 创建一个实现 ComponentListener 接口的类,如下例所示:

    //---------------------
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;

    public class winlistenner implements ComponentListener {

        public void componentHidden(ComponentEvent arg0) {
            // TODO Auto-generated method stub
            System.out.print("Hided\r\n");

        }

        public void componentMoved(ComponentEvent arg0) {
            // TODO Auto-generated method stub
            System.out.print("Moved\r\n");

        }

        public void componentResized(ComponentEvent arg0) {
            // TODO Auto-generated method stub
            System.out.print("Resized\r\n");


        }

        public void componentShown(ComponentEvent arg0) {
            // TODO Auto-generated method stub

            System.out.print("Shown\r\n");

        }

}
//------------------------------------------------------------------------

2- Now create a getter for your JFrame like this:

2- 现在为您的 JFrame 创建一个 getter,如下所示:

public class JMain {

    private JFrame frmNetworkshareMoon;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;

    public JFrame getFrmNetworkshareMoon() {
        return frmNetworkshareMoon;
    }


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JMain window = new JMain();
                    winlistenner listenner= new winlistenner();
                    window.getFrmNetworkshareMoon().addComponentListener(listenner);
                    window.frmNetworkshareMoon.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
//......
// the rest of your class code:
//...
}

3- being your main like the above example, you will set JFrame listener the listener you created, and then run the program, you will see messages coming from the listener:

3- 像上面的例子一样,你将设置 JFrame 侦听器作为你创建的侦听器,然后运行程序,你将看到来自侦听器的消息:

Moved
Resized
Resized
Moved
Shown
Moved
Moved