简体   繁体   English

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

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

Which kind of listener do I have to add to a JFrame to detect when it is being hidden or shown via setVisible ? 我必须将哪种监听器添加到JFrame以检测它何时被隐藏或通过setVisible

I tried using a WindowListener and the windowOpened and windowClosed methods, but they only work for the first time that a window is opened ( windowOpened ) or, respectively, when the window is closed using the dispose method ( windowClosed ). 我尝试使用WindowListenerwindowOpenedwindowClosed方法,但它们仅在第一次打开窗口( windowOpened )时工作,或者分别在使用dispose方法( 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 . 我希望每次使用setVisible使窗口在屏幕上可见和不可见时收到通知。

Is there a standard Swing way to achieve this, or do I need to make my own (by, say, overriding the setVisible method)? 是否有标准的Swing方法来实现这一点,或者我是否需要自己创建(通过,比如覆盖setVisible方法)?

Try a java.awt.event.ComponentListener . 试试java.awt.event.ComponentListener You can add one using this code (where window is the name of the JFrame ) : 您可以使用此代码添加一个(其中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 */
   }
});

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

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

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