简体   繁体   中英

Where is the full-screen window event in Java (Awt)?

I've checked the methods of the WindowListener interface but it doesn't have an event for this, or for resizing the window. (Actually there is a resized event inheritied from the Component class: ( link ) but thats all.. Can someone show me how to do something if the user triggers full-screen or leaves it?

//By full-screen I mean maximized :))

If by "triggers full-screen" you are referring to maximizing the window, you should not use a ComponentListener. What you want is a WindowStateListener :

private static boolean isMaximized(int state) {
    return (state & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH;
}

// ...

    frame.addWindowStateListener(new WindowStateListener() {
        public void windowStateChanged(WindowEvent event) {
            boolean isMaximized = isMaximized(event.getNewState());
            boolean wasMaximized = isMaximized(event.getOldState());

            if (isMaximized && !wasMaximized) {
                System.out.println("User maximized window.");
            } else if (wasMaximized && !isMaximized) {
                System.out.println("User unmaximized window.");
            }
        }
    });

If by "triggers full-screen" you mean a true full-screen window , a ComponentListener may work, though I have not tried it. Since a true full-screen window can only occur if your code calls GraphicsDevice.setFullScreenWindow , you would probably want to add your code right after that call:

if (userPressedFullScreenButton) {
    frame.getGraphicsConfiguration().getDevice().setFullScreenWindow(frame);
    System.out.println("User switched to full-screen.");
} else if (userPressedManagedWindowButton) {
    frame.getGraphicsConfiguration().getDevice().setFullScreenWindow(null);
    System.out.println("User switched out of full-screen.");
}

You can add a new ComponentListener to detect if the screen was resized, and then you could match if the new size equals to your screen resolution size.

Example

    JFrame frame = new JFrame();

    frame.addComponentListener(new ComponentListener() {

        @Override
        public void componentHidden(ComponentEvent arg0) {}

        @Override
        public void componentMoved(ComponentEvent arg0) {}

        @Override
        public void componentResized(ComponentEvent e) {
            Component c = e.getComponent();

            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            double width = screenSize.getWidth();
            double height = screenSize.getHeight();

            if (width == c.getWidth() - 16 && height == c.getHeight() - 16)
                System.out.println("User entered fullscreen");
        }

        @Override
        public void componentShown(ComponentEvent arg0) {}

    });

Explanation:

ComponenetListener#componentResized will be called everytime the user has resized the frame. According to your question, you want to detect if the user has entered full screen.

Therefore, in the componentResized method, I check if the new width and height of the window equals to the user's screen resolution. I had to subtract the new width and height by 16 because these are of offsets of the window border. However if you will use undecorated window, then you should not subtract I think.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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