在 Java 中创建屏幕外框架(或:当所有应用程序窗口关闭时,如何避免 Mac 上的空白菜单)? [英] Creating an offscreen frame in Java (or: how to avoid a blank menu on a Mac, when all application windows are closed)?

查看:20
本文介绍了在 Java 中创建屏幕外框架(或:当所有应用程序窗口关闭时,如何避免 Mac 上的空白菜单)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 Mac 应用程序,我希望我的菜单栏看起来正确.

I am making a Mac application, and I want my menu bar to look right.

任何 Mac 用户都知道菜单栏应该位于顶部屏幕菜单中.在属性列表文件中将apple.laf.useScreenMenuBar 设置为true 去掉框内菜单栏,将当前聚焦窗口的菜单栏移动到屏幕菜单.

Any Mac user knows the menu bar should be in the top screen menu. Setting apple.laf.useScreenMenuBar to true in the property list file gets rid of the in-frame menu bars and moves the menu bar of the current focused window to the screen menu.

然而,当所有窗口被隐藏或没有窗口时,没有菜单栏移动到顶部,你只会得到一个空白菜单.我听说一个解决方案是创建一个在没有其他人时聚焦的屏幕外窗口.它的唯一目的是它的菜单,以便在其他人离开时它可以填充.

However, when all windows are hidden or when there are no windows, there are no menu bars to move to the top, and you just get a blank menu. I heard a solution to this was to create an offscreen window that is focused when no others are. The only purpose of it would be its menu, so that it could fill in when the others are gone.

但是,我遇到了很多问题.我似乎无法将窗口移出屏幕,因为 Mac 不允许您将坐标设置为超出屏幕大小的值;它只是将其切断并将其定位在边缘.我还需要做些什么才能制作屏幕外窗口吗?

However, I've been getting loads of problems. I can't seem to move the window off the screen because Macs won't let you set the coordinates to something past the size of the screen; it just cuts it off and positions it at the edge instead. Is there something else I have to do to make an offscreen window?

推荐答案

您绝对应该考虑 WizardOfOdds 非常有用的答案.使用 "应用程序菜单" 正确会有所帮助,并且很容易设置最小的 Info.plist 开始.持久的 File 菜单将允许您的应用程序在其他窗口关闭时打开一个新窗口.这个答案链接到一个简单的示例.

You should definitely consider WizardOfOdds' very helpful answer. Using "The Application Menu" correctly will help, and it's easy to set up a minimal Info.plist to get started. A persistent File menu will allow your application to open a new window when others are closed. This answer links to a simple example.

虽然 Apple 的人机界面指南是了解用户期望的极好指南,您当然可以尝试在问题中建议的方法.特别是,您可以在不可见窗口上尝试 setLocation(Short.MIN_VALUE, Short.MIN_VALUE).此外,您可能想要响应 WindowEvent 以某种特殊方式表示最后一个可见窗口的关闭.

Although Apple's Human Interface Guidelines are an excellent guide to what your users will expect, you can certainly experiment with the approach you suggested in your question. In particular, you might try setLocation(Short.MIN_VALUE, Short.MIN_VALUE) on the invisible window. In addition, you might want to respond to a WindowEvent in some special way if it signals the close of the last visible window.

附录:当您的听众看到最后一个可见窗口关闭时,创建一个新的空应用程序窗口.或者,在屏幕上移动不可见窗口并使其可见,直到用户决定如何继续.

Addendum: When your listener sees the last visible window close, create a new, empty application window. Alternatively, move the invisible window onscreen and make it visible until the user decides how to proceed.

附录:Mac OS X 有助于防止可见 窗口窗体移出屏幕,但很容易将不可见 窗口置于不确定状态,如下所示.

Addendum: Mac OS X helpfully prevents a visible window form being moved offscreen, but it's easy to put an invisible window in limbo, as shown below.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

public class FrameTest extends JFrame {

    private static FrameTest marco;
    private static FrameTest polo;

    private static class MyPanel extends JPanel {

        public MyPanel() {
            super(true);
            final JToggleButton b = new JToggleButton("Test");
            b.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if (b.isSelected()) {
                        polo.setLocation(100, 100);
                        polo.setVisible(true);
                    }
                    else {
                        polo.setVisible(false);
                        polo.setLocation(Short.MIN_VALUE, Short.MIN_VALUE);
                    }
                }
            });
            this.add(b);
        }
    }

    public FrameTest(String title) {
        super(title);
        this.setLayout(new BorderLayout());
        this.add(new MyPanel());
        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(final String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                marco = new FrameTest("Marco");
                marco.setLocationRelativeTo(null);
                marco.setVisible(true);
                polo = new FrameTest("Polo");
                polo.setLocation(Short.MIN_VALUE, Short.MIN_VALUE);
            }
        });
    }
}

这篇关于在 Java 中创建屏幕外框架(或:当所有应用程序窗口关闭时,如何避免 Mac 上的空白菜单)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆