不同的JFrame在不同的显示器上

时间:2013-05-03 09:59:18

标签: java swing jframe multiple-monitors

我有一个中央数据库,我用JDBC连接它,在对数据进行一些准备之后,我生成了6个不同的JFrame,我喜欢在建筑物的不同墙壁上的不同显示器(显示器)上显示每个JFrame。我只能同时通过IP(通过WiFi)到达。我可以用GraphicsEnvironment以某种方式解决它吗?

我会很高兴任何建议!

1 个答案:

答案 0 :(得分:0)

如果操作系统可以将每个屏幕视为单独的图形设备,您应该可以使用类似的东西......

import java.awt.EventQueue;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestGC {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }
                GraphicsDevice[] sds = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
                for (GraphicsDevice sd : sds) {
                    System.out.println(sd.getIDstring());
                    GraphicsConfiguration gc = sd.getDefaultConfiguration();
                    JFrame f = new JFrame(gc);
                    f.add(new JLabel(sd.getIDstring()));
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.pack();
                    centerOn(f, gc);
                    f.setVisible(true);
                }
            }
        });
    }

    private static void centerOn(JFrame f, GraphicsConfiguration gc) {
        Rectangle bounds = gc.getBounds();
        int x = bounds.x + ((bounds.width - f.getWidth()) / 2);
        int y = bounds.y + ((bounds.height - f.getHeight()) / 2);
        f.setLocation(x, y);
    }
}