Java 让窗口在显示屏2显示(Java swing)
当我们有两个显示屏时,我们想让窗口在另一个显示屏出现,可以使用GraphicsEnvironment。
以下是代码:
import javax.swing.*;
import java.awt.*;
public class test {
static int w = 500;
static int h = 300;
public static void main(String[] arg0){
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gd = ge.getScreenDevices();
//选择显示屏,0是显示屏1,1是显示屏2
Rectangle bounds = gd[1].getDefaultConfiguration().getBounds();
//swing窗口代码示例
JFrame frame = new JFrame();
frame.setSize(w, h);
frame.setLocation(bounds.x + bounds.width / 2 - w / 2, bounds.y + bounds.height / 2 - h / 2);
frame.setVisible(true);
}
}