Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java/Swing OS X全屏模式和虚拟桌面_Java_Macos_Swing_Jframe_Awt - Fatal编程技术网

Java/Swing OS X全屏模式和虚拟桌面

Java/Swing OS X全屏模式和虚拟桌面,java,macos,swing,jframe,awt,Java,Macos,Swing,Jframe,Awt,我的Java应用程序使用两个JFrames—第一个用于向用户提供有限数量选项中的一个选项(无全屏),第二个用于提供数据(具有全屏选项) 我使用了setVisible()在我的两个框架之间切换,这很好,因为它重用了以前的虚拟桌面并保留了全屏模式,但我不得不切换到dispose()以隐藏第二个JFrame,因为它有一个错误 当我在frame2中进入全屏模式时,会创建一个新的虚拟机-但如果我处置frame2,该虚拟机仍然作为一个空的灰色桌面存在。如果我再次切换回frame2,我们将不再处于全屏状态。。

我的Java应用程序使用两个JFrames—第一个用于向用户提供有限数量选项中的一个选项(无全屏),第二个用于提供数据(具有全屏选项)

我使用了setVisible()在我的两个框架之间切换,这很好,因为它重用了以前的虚拟桌面并保留了全屏模式,但我不得不切换到dispose()以隐藏第二个JFrame,因为它有一个错误

当我在frame2中进入全屏模式时,会创建一个新的虚拟机-但如果我处置frame2,该虚拟机仍然作为一个空的灰色桌面存在。如果我再次切换回frame2,我们将不再处于全屏状态。。。如果我这样做了。。。然后我现在有2个虚拟桌面。等等

在frame2上使用setVisible(false)也会保留空的虚拟机,尽管这比重新绘制/缓冲区问题要小,因为下次在frame2上使用setVisible(true)时,它至少会在相同的VD中打开,如上所述

我的问题是:在执行dispose()后,我是否可以使用相同的虚拟磁盘在frame2中保存和恢复全屏模式,或者至少可以强制虚拟磁盘关闭?还是我错过了问题的另一个解决方案?或者这仅仅是因为我没有严格遵守苹果的全屏指导原则

提前感谢您的帮助

更新:我回到使用设置可见(false)隐藏frame2,但每次显示时都使用新建()(以避免重新绘制/缓冲区问题),我再次遇到相同的重复VD问题。我还实现了com.apple.eawt.Application.requestToggleFullScreen以及com.apple.eawt.FullScreenListener在我处理之前退出全屏,但应用程序挂起。现在看来,我将不得不接受repaint.buffer问题,以允许全屏切换正常工作

SSCCE:

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SwingFramesFullScreenMacDemo extends javax.swing.JFrame {

    JFrame secondFrame;
    JPanel myPanel;

    public SwingFramesFullScreenMacDemo() {

        //Setup UI With Button to show secondFrame
        JPanel thisPanel = new JPanel();
        JButton jbtOpenFullScreenFrame = new JButton("Open Full Screen Frame");
        jbtOpenFullScreenFrame.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                secondFrame.setVisible(true);
                setVisible(false);
            }
        });

        thisPanel.add(jbtOpenFullScreenFrame);
        this.add(thisPanel);
        this.pack();

        // Setup Second Frame With Full Screen Support
        secondFrame = new JFrame();

        myPanel = new JPanel();

        //Dispose() works without residual image, but leaves open virtual desktop for each time
        //you go to full screen then dispose
        JButton jbtDispose = new JButton("Dispose");

        jbtDispose.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                setVisible(true);
                secondFrame.dispose();

            }
        });

        //SetVisible(false) gives residual image on reopening second frame despite clearing colour
        //works well for fullscreen, only one virtual desktop open for lifetime of app
        JButton jbtSetVisibleFalse = new JButton("Set Visible to False");

        jbtSetVisibleFalse.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                secondFrame.setVisible(false);
                setVisible(true);

            }
        });

        myPanel.add(jbtDispose);
        myPanel.add(jbtSetVisibleFalse);

        secondFrame.add(myPanel);
        secondFrame.pack();

        enableFullScreenMode(secondFrame);

        secondFrame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosed(WindowEvent e) {
                setVisible(true);
                secondFrame.setVisible(false);

            }
        });

    }

    public static void enableFullScreenMode(Window window) {

        String methodName = "setWindowCanFullScreen";

        try {
            Class<?> clazz = Class.forName(com.apple.eawt.FullScreenUtilities.class.getName());
            Method method = clazz.getMethod(methodName, new Class<?>[]{
                Window.class, boolean.class});
            method.invoke(null, window, true);
        } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException t) {
            System.err.println("Full screen mode is not supported");
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {

                javax.swing.JFrame fullScreenMacProblem = new SwingFramesFullScreenMacDemo();
                fullScreenMacProblem.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {

                        System.exit(0);
                    }
                });

                fullScreenMacProblem.setVisible(true);

            }
        });

    }

}
导入java.awt.Window;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.WindowAdapter;
导入java.awt.event.WindowEvent;
导入java.lang.reflect.InvocationTargetException;
导入java.lang.reflect.Method;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类SwingFramesFullScreenMacDemo扩展了javax.swing.JFrame{
JFrame-secondFrame;
JPanel-myPanel;
公共SwingFramesFullScreenMacDemo(){
//使用按钮设置UI以显示第二帧
JPanel thisPanel=新的JPanel();
JButton jbtopenfullscreensframe=新JButton(“打开全屏画面”);
jbtOpenFullScreenFrame.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
secondFrame.setVisible(true);
setVisible(假);
}
});
添加(jbtopenfullscreensframe);
添加(此面板);
这个包();
//设置具有全屏支持的第二帧
secondFrame=新的JFrame();
myPanel=newjpanel();
//Dispose()在没有剩余映像的情况下工作,但每次都保持虚拟机打开
//您进入全屏,然后进行处理
JButton jbtDispose=新JButton(“处置”);
jbtDispose.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
setVisible(真);
secondFrame.dispose();
}
});
//SetVisible(false)在重新打开第二帧时提供剩余图像,尽管颜色已清除
//适用于全屏,在应用程序的生命周期内只有一个虚拟桌面处于打开状态
JButton jbtSetVisibleFalse=新JButton(“设置为False可见”);
jbtSetVisibleFalse.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
secondFrame.setVisible(false);
setVisible(真);
}
});
myPanel.add(jbtDispose);
myPanel.add(jbtSetVisibleFalse);
secondFrame.add(myPanel);
secondFrame.pack();
启用全屏模式(第二帧);
secondFrame.addWindowListener(新的WindowAdapter(){
@凌驾
公共无效窗口关闭(WindowEvent e){
setVisible(真);
secondFrame.setVisible(false);
}
});
}
公共静态无效启用FullScreenMode(窗口){
String methodName=“setWindowCanFullScreen”;
试一试{
Class clazz=Class.forName(com.apple.eawt.FullScreenUtilities.Class.getName());
Method=clazz.getMethod(methodName,新类[]){
Window.class,boolean.class});
调用(null,window,true);
}catch(ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException t){
System.err.println(“不支持全屏模式”);
}
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
invokeLater(new Runnable()){
@凌驾
公开募捐{
javax.swing.JFrame fullScreenMacProblem=new SwingFramesFullScreenMacDemo();
fullScreenMacProblem.addWindowListener(新java.awt.event.WindowAdapter(){
@凌驾
公共无效窗口关闭(java.awt.event.WindowEvent e){
系统出口(0);
}
});
fullScreenMacProblem.setVisible(true);
}
});
}
}
我对这个“第22条军规”的解决方案是