java - 双屏应用

标签 java swing multiple-monitors

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

最佳答案

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

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

}

关于java - 双屏应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13396426/

相关文章:

c# - AppBar 多显示器

linux - 我能够在 Virtualbox 中成功运行此 Ubuntu (linux) 设置吗?

java - 初始化数据库模式并添加测试数据

java - Java中的随机数组

java - 在继续之前检查 jtable 中的重复数据

java - 在鼠标单击上添加文本字段和标签不起作用 Java

java - JComponent JPanel 缩放、平移和坐标问题

windows - Ctrl Alt F8 断开显示?

java - 在 jar 中调用一个 exe 文件

java - Android Facebook 集成 - 预定义墙贴