swing/swt 支持多屏幕显示

简介: swing/swt 支持多屏幕显示

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;
   }
}


相关文章
|
9月前
|
Shell API
【SWT】常用代码(三)
【SWT】常用代码(三)
92 0
|
9月前
|
Java
【SWT】常用代码(二)
【SWT】常用代码(二)
101 0
Java_Swing中让窗口居中显示的方法(三种方法)
Java_Swing中让窗口居中显示的方法(三种方法)
552 0
|
前端开发 Android开发
在SWT里显示AWT对象
今天遇到一个问题,就是要在一个Eclipse插件里显示JFreeChart的图形,因为后者是基于Java2D的,要把图形显示在SWT应用程序里需要利用SWT-AWT桥接器来实现,虽说桥接的方式多半会伴随着性能下降,但总归是一个解决方法。
1296 0

热门文章

最新文章