swing中对于双屏的支持
public class Main{ public static void main(String[] args) { JFrame jf = new JFrame(); jf.setSize(400, 400); jf.setDefaultCloseOperation(3); jf.setVisible(true); Main.showOnScreen(0, jf);//主屏显示 JFrame jf2 = new JFrame(); jf2.setSize(200, 400); jf2.setDefaultCloseOperation(3); jf2.setVisible(true); Main.showOnScreen(1, jf2);//副屏显示 } /** * 指定显示屏幕相应内容 * screen 显示器序号 * / public static void showOnScreen(int screen, JFrame frame) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] gd = ge.getScreenDevices(); if (screen > -1 && screen < gd.length) {//一个或多个屏幕 frame.setLocation(gd[screen].getDefaultConfiguration().getBounds().x, frame.getY()); } else if (gd.length > 0) {//只有一个屏幕 frame.setLocation(gd[0].getDefaultConfiguration().getBounds().x, frame.getY()); } else {//未获取到屏幕信息 throw new RuntimeException("No Screens Found"); } } }
swt对于双屏的支持
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Monitor; import org.eclipse.swt.widgets.Shell; import cn.com.cordiality.UI.MonitorManager; public class Main { public static void main(String[] args) { Display display = Display.getDefault(); Shell shell = new Shell(display,SWT.CLOSE);//主屏显示 shell.setSize(600,400); //...//省略界面装配部分代码 Monitor[] monitors = MonitorManager.getInstance().getMonitors(); if (monitors.length >= 2) { Monitor monitor = monitors[0]; Monitor monitor1 = monitors[1]; if ((monitor != null) && (monitor1 != null)) {//第二个屏幕 Demo demo = new Demo(shell, monitor.getClientArea().width, 0, monitor1.getClientArea().width, monitor1.getClientArea().height); demo.open(); } } shell.open(); while (!shell.isDisposed()) if (!display.readAndDispatch()) display.sleep(); } } class Demo { Shell shell; Display display; public Demo(Shell parent, int left, int top, int maxWidth, int maxHeight) { shell = new Shell(parent, SWT.EMBEDDED); shell.setSize(maxWidth, maxHeight); shell.setLocation(left, top); shell.setLayout(new FormLayout()); display = this.shell.getDisplay(); // ...//省略界面装配部分代码 } public void open() { this.shell.open(); while (!this.shell.isDisposed()) if (!this.display.readAndDispatch()) this.display.sleep(); } }
MonitorManager 类
package cn.com.cordiality.UI; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Monitor; public class MonitorManager { private Monitor[] monitors; private static MonitorManager monitorManager; public static MonitorManager getInstance() { if (monitorManager == null) { try { monitorManager = new MonitorManager(Display.getCurrent().getMonitors()); } catch (Exception e) { e.printStackTrace(); } } return monitorManager; } public MonitorManager(Monitor[] monitors) { this.monitors = monitors; } public Monitor[] getMonitors() { return this.monitors; } }