代码之家  ›  专栏  ›  技术社区  ›  Yougesh

在Windows 10中,将JFrame移动到第二个显示器的最佳方法是什么

  •  -1
  • Yougesh  · 技术社区  · 6 年前

    我正在尝试在windows 10中将jframe自动移动到第二个显示器,但它不起作用,我使用的是JDK8,视频驱动程序是最新的,下面是我的代码:

    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class DualMonitor {
        public static void main(String... args) {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice[] gs = ge.getScreenDevices();
    
            javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null, "Found : " + gs.length, "screen detected ?",
                    javax.swing.JOptionPane.DEFAULT_OPTION);
    
            for (int j = 0; j < gs.length; j++) {
                GraphicsDevice gd = gs[j];
                JFrame frame = new JFrame(gd.getDefaultConfiguration());
                frame.setTitle("I'm on monitor #" + j);
                frame.setSize(400, 200);
                frame.add(new JLabel("hello world"));
                frame.setVisible(true);
            }
        }
    }
    
    1 回复  |  直到 5 年前
        1
  •  0
  •   Yougesh    6 年前

    通过使用解决问题 setLocationRelativeTo ,则, setUndecorated setExtendedState(JFrame.MAXIMIZED_BOTH) 检查以下内容:

    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class DualMonitor {
    
        public static void main(String... args) {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice[] gs = ge.getScreenDevices();
    
            javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null, "Found : " + gs.length, "screen detected ?",
                    javax.swing.JOptionPane.DEFAULT_OPTION);
    
            for (int j = 0; j < gs.length; j++) {
                GraphicsDevice gd = gs[j];
                JFrame dualview = new JFrame(gd.getDefaultConfiguration());
    
                JFrame frame = new JFrame();
                frame.setLocationRelativeTo(dualview);
                dualview.dispose();
    
                frame.setUndecorated(true);
                frame.setTitle("I'm on monitor #" + j);
                frame.setSize(400, 200);
                frame.add(new JLabel("hello world"));
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                frame.setVisible(true);
    
            }
        }
    }