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

查看:134
本文介绍了用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.

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

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 开始使用。持久的文件菜单将允许您的应用程序在其他人关闭时打开一个新窗口。这个答案链接到一个简单的示例

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天全站免登陆