GraphicalEnvironment does not update screen devices after switching off second screen

后端 未结 1 1590
星月不相逢
星月不相逢 2021-01-24 19:42

I have two monitors
I write very small Swing Java code to collect info of all screen devices combine changing display mode with one or two display screen by setting Display

1条回答
  •  不要未来只要你来
    2021-01-24 20:21

    It may be tricky. But from a quick look at the source code, you might try some reflection.

    Disclaimer: Many things can go wrong when using reflection. You should be aware of the fact that you are relying on unspecified behavior here. If the underlying implementation changes, then the following example program might no longer work...

    ...although I consider this as "unlikely", at least

    The following is an example showing how this might work:

    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class GraphicsEnvironmentTest
    {
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    createAndShowGUI();
                }
            });
        }
    
        private static void createAndShowGUI()
        {
            JFrame frame = new JFrame("Demo get info screen devices");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JButton button = new JButton("Print info screen devices");
            button.addActionListener(new ActionListener()
            {
    
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    printInfoAllScreenDevices();
                }
            });
            frame.add(button);
            frame.setSize(500, 300);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        private static void printInfoAllScreenDevices() 
        {
            GraphicsDevice graphicsDevices[] = getGraphicsDevices();
            System.out.println("Found "+graphicsDevices.length+" devices:");
            for (int i=0; i c = graphicsEnvironment.getClass();
            Method getNumScreensMethod = null;
            boolean getNumScreensMethodWasAccessible = false; 
            Method makeScreenDeviceMethod = null;
            boolean makeScreenDeviceMethodWasAccessible = false;
            try
            {
                getNumScreensMethod = 
                    c.getDeclaredMethod("getNumScreens");
                getNumScreensMethodWasAccessible =
                    getNumScreensMethod.isAccessible();
                getNumScreensMethod.setAccessible(true);
    
                makeScreenDeviceMethod = 
                    c.getDeclaredMethod("makeScreenDevice", int.class);
                makeScreenDeviceMethodWasAccessible =
                    makeScreenDeviceMethod.isAccessible();
                makeScreenDeviceMethod.setAccessible(true);
    
                int numScreens = 
                    (Integer) getNumScreensMethod.invoke(graphicsEnvironment);
                GraphicsDevice graphicsDevices[] = new GraphicsDevice[numScreens];
                for (int i = 0; i < numScreens; i++)
                {
                    Object object = 
                        makeScreenDeviceMethod.invoke(graphicsEnvironment, i);
                    graphicsDevices[i] = (GraphicsDevice) object;
                }
                return graphicsDevices;
            }
            catch (NoSuchMethodException e)
            {
                throw new RuntimeException(e);
            }
            catch (SecurityException e)
            {
                throw new RuntimeException(e);
            }
            catch (IllegalAccessException e)
            {
                throw new RuntimeException(e);
            }
            catch (IllegalArgumentException e)
            {
                throw new RuntimeException(e);
            }
            catch (InvocationTargetException e)
            {
                throw new RuntimeException(e);
            }
            finally
            {
                if (getNumScreensMethod != null)
                {
                    getNumScreensMethod.setAccessible(
                        getNumScreensMethodWasAccessible);
                }
                if (makeScreenDeviceMethod != null)
                {
                    makeScreenDeviceMethod.setAccessible(
                        makeScreenDeviceMethodWasAccessible);
                }
            }
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题