适用于多个屏幕的Java GUI全屏

Sep*_*lia 5 java user-interface awt fullscreen monitors

我希望我不会发一个重复的问题,但我无法找到这样的问题,所以也许我很安全?无论如何...

对于我正在制作的应用程序,我将同时打开两个应用程序(两个独立的进程和窗口).运行这些应用程序的计算机将具有多个监视器.我希望第一个应用程序/窗口全屏显示并占用我的一个显示器(简单部分),另一个应用程序/全屏显示在第二个显示器上.如果可能的话,我希望他们以这种方式初始化.

目前,我使用此代码使我的窗口全屏:

this.setVisible(false);
this.setUndecorated(true);
this.setResizable(false);
myDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
myDevice.setFullScreenWindow(this);
Run Code Online (Sandbox Code Playgroud)

它所在的类是JFrame类的扩展,myDevice的类型为"GraphicsDevice".当然有可能有一个更好的方法来使我的窗口全屏,以便我可以在两个不同的显示器上全屏显示两个不同的应用程序.

如果我不清楚,请说明,我会尝试编辑澄清!

Gui*_*let 5

首先,您需要在每个屏幕设备上放置框架.

frame1.setLocation(pointOnFirstScreen);
frame2.setLocation(pointOnSecondScreen);
Run Code Online (Sandbox Code Playgroud)

然后要最大化一个帧,只需在您的JFrame上调用它:

frame.setExtendedState(Frame.MAXIMIZED_BOTH);
Run Code Online (Sandbox Code Playgroud)

这是一个工作示例,说明:

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 Test {
    protected void initUI() {
        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() {

            @Override
            public void run() {
                new Test().initUI();
            }
        });
    }

}
Run Code Online (Sandbox Code Playgroud)