5

我必须以双屏模式运行应用程序。如何在两个屏幕上作为独立窗口运行应用程序但共享相同的应用程序模型?

4

2 回答 2

8

如果我没记错的话,这个例如可以帮助你。首先将您的框架放置在每个屏幕设备上。

frame1.setLocation(pointOnFirstScreen);
frame2.setLocation(pointOnSecondScreen);

最大化:

frame.setExtendedState(Frame.MAXIMIZED_BOTH);

工作示例:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class GuiApp1 {
protected void twoscreen() {
    Point p1 = null;
    Point p2 = null;
    for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment ().getScreenDevices()) {
        if (p1 == null) {
            p1 = gd.getDefaultConfiguration().getBounds().getLocation();
        } else if (p2 == null) {
            p2 = gd.getDefaultConfiguration().getBounds().getLocation();
        }
    }
    if (p2 == null) {
        p2 = p1;
    }
    createFrameAtLocation(p1);
    createFrameAtLocation(p2);
 }

 private void createFrameAtLocation(Point p) {
    final JFrame frame = new JFrame();
    frame.setTitle("Test frame on two screens");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new BorderLayout());
    final JTextArea textareaA = new JTextArea(24, 80);
    textareaA.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
    panel.add(textareaA, BorderLayout.CENTER);
    frame.setLocation(p);
    frame.add(panel);
    frame.pack();
    frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    frame.setVisible(true);
 }

 public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {


        public void run() {
            new GuiApp1().twoscreen();
        }
    });
 }

}
于 2012-11-15T11:46:13.800 回答
2

你需要看看GraphicsDevice API,你可以在那里找到一个很好的例子。

来自甲骨文:

在多屏幕环境中,GraphicsConfiguration 对象可用于在多个屏幕上呈现组件。以下代码示例演示了如何为 GraphicsEnvironment 中每个屏幕设备上的每个 GraphicsConfiguration 创建一个 JFrame 对象:

GraphicsEnvironment ge = GraphicsEnvironment.
   getLocalGraphicsEnvironment();
   GraphicsDevice[] gs = ge.getScreenDevices();
   for (int j = 0; j < gs.length; j++) { 
      GraphicsDevice gd = gs[j];
      GraphicsConfiguration[] gc =
        gd.getConfigurations();
      for (int i=0; i < gc.length; i++) {
         JFrame f = new
         JFrame(gs[j].getDefaultConfiguration());
         Canvas c = new Canvas(gc[i]); 
         Rectangle gcBounds = gc[i].getBounds();
         int xoffs = gcBounds.x;
         int yoffs = gcBounds.y;
           f.getContentPane().add(c);
           f.setLocation((i*50)+xoffs, (i*60)+yoffs);
         f.show();
      }
}
于 2012-11-15T11:19:55.657 回答